bzr branch
http://gegoxaren.bato24.eu/bzr/loggerhead/trunk
|
183.2.1
by John Arbash Meinel
Add Copyright information to most files. |
1 |
#
|
2 |
# This program is free software; you can redistribute it and/or modify
|
|
3 |
# it under the terms of the GNU General Public License as published by
|
|
4 |
# the Free Software Foundation; either version 2 of the License, or
|
|
5 |
# (at your option) any later version.
|
|
6 |
#
|
|
7 |
# This program is distributed in the hope that it will be useful,
|
|
8 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 |
# GNU General Public License for more details.
|
|
11 |
#
|
|
12 |
# You should have received a copy of the GNU General Public License
|
|
13 |
# along with this program; if not, write to the Free Software
|
|
|
467.2.1
by Toshio Kuratomi
Change to the FSF address in license headers |
14 |
# Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
|
|
183.2.1
by John Arbash Meinel
Add Copyright information to most files. |
15 |
#
|
|
159.2.30
by Michael Hudson
trim down turbosimpletal and move it inside loggerhead package. |
16 |
"""Support for Zope Page Templates using the simpletal library."""
|
|
128.6.1
by Michael Hudson
add a lightly hacked copy of turbozpt (it's not much code) |
17 |
|
|
159.2.30
by Michael Hudson
trim down turbosimpletal and move it inside loggerhead package. |
18 |
import logging |
|
128.6.67
by Michael Hudson
tidy up turbosimpletal a bit, add recompilation templates that have changed on |
19 |
import os |
|
128.6.1
by Michael Hudson
add a lightly hacked copy of turbozpt (it's not much code) |
20 |
import pkg_resources |
|
287
by Michael Hudson
so it turns out that doing the whitespace compression when we load the template |
21 |
import re |
|
159.2.30
by Michael Hudson
trim down turbosimpletal and move it inside loggerhead package. |
22 |
import StringIO |
|
128.6.1
by Michael Hudson
add a lightly hacked copy of turbozpt (it's not much code) |
23 |
|
|
128.6.57
by Michael Hudson
all tests pass, apart from the revision size limit one |
24 |
from simpletal import simpleTAL, simpleTALES |
|
128.6.40
by Michael Hudson
remove the obscure here hack and tidy up turbozpt some more |
25 |
|
26 |
_zpt_cache = {} |
|
|
230.1.1
by Steve 'Ashcrow' Milner
Updated to follow pep8. |
27 |
|
28 |
||
|
128.6.40
by Michael Hudson
remove the obscure here hack and tidy up turbozpt some more |
29 |
def zpt(tfile): |
30 |
tinstance = _zpt_cache.get(tfile) |
|
|
128.6.67
by Michael Hudson
tidy up turbosimpletal a bit, add recompilation templates that have changed on |
31 |
stat = os.stat(tfile) |
32 |
if tinstance is None or tinstance.stat != stat: |
|
|
287
by Michael Hudson
so it turns out that doing the whitespace compression when we load the template |
33 |
text = open(tfile).read() |
34 |
text = re.sub(r'\s*\n\s*', '\n', text) |
|
35 |
text = re.sub(r'[ \t]+', ' ', text) |
|
|
128.6.58
by Michael Hudson
quiet simpletal's logging, unicode fixes |
36 |
tinstance = _zpt_cache[tfile] = TemplateWrapper( |
|
287
by Michael Hudson
so it turns out that doing the whitespace compression when we load the template |
37 |
simpleTAL.compileXMLTemplate(text), tfile, stat) |
|
128.6.40
by Michael Hudson
remove the obscure here hack and tidy up turbozpt some more |
38 |
return tinstance |
39 |
||
|
128.6.67
by Michael Hudson
tidy up turbosimpletal a bit, add recompilation templates that have changed on |
40 |
|
|
128.6.57
by Michael Hudson
all tests pass, apart from the revision size limit one |
41 |
class TemplateWrapper(object): |
42 |
||
|
128.6.67
by Michael Hudson
tidy up turbosimpletal a bit, add recompilation templates that have changed on |
43 |
def __init__(self, template, filename, stat): |
|
128.6.57
by Michael Hudson
all tests pass, apart from the revision size limit one |
44 |
self.template = template |
|
128.6.58
by Michael Hudson
quiet simpletal's logging, unicode fixes |
45 |
self.filename = filename |
|
128.6.67
by Michael Hudson
tidy up turbosimpletal a bit, add recompilation templates that have changed on |
46 |
self.stat = stat |
|
128.6.57
by Michael Hudson
all tests pass, apart from the revision size limit one |
47 |
|
|
128.6.59
by Michael Hudson
mostly more unicode fixes |
48 |
def expand(self, **info): |
|
128.6.57
by Michael Hudson
all tests pass, apart from the revision size limit one |
49 |
context = simpleTALES.Context(allowPythonPath=1) |
|
128.6.59
by Michael Hudson
mostly more unicode fixes |
50 |
for k, v in info.iteritems(): |
|
128.6.57
by Michael Hudson
all tests pass, apart from the revision size limit one |
51 |
context.addGlobal(k, v) |
52 |
s = StringIO.StringIO() |
|
|
128.6.59
by Michael Hudson
mostly more unicode fixes |
53 |
self.template.expandInline(context, s) |
|
128.6.57
by Michael Hudson
all tests pass, apart from the revision size limit one |
54 |
return s.getvalue() |
55 |
||
|
159.2.30
by Michael Hudson
trim down turbosimpletal and move it inside loggerhead package. |
56 |
def expand_into(self, f, **info): |
|
159.2.4
by Michael Hudson
more progress |
57 |
context = simpleTALES.Context(allowPythonPath=1) |
58 |
for k, v in info.iteritems(): |
|
59 |
context.addGlobal(k, v) |
|
60 |
self.template.expand(context, f, 'utf-8') |
|
61 |
||
|
128.6.57
by Michael Hudson
all tests pass, apart from the revision size limit one |
62 |
@property
|
63 |
def macros(self): |
|
64 |
return self.template.macros |
|
65 |
||
|
128.6.40
by Michael Hudson
remove the obscure here hack and tidy up turbozpt some more |
66 |
|
|
159.2.30
by Michael Hudson
trim down turbosimpletal and move it inside loggerhead package. |
67 |
def load_template(classname): |
68 |
"""Searches for a template along the Python path. |
|
69 |
||
70 |
Template files must end in ".pt" and be in legitimate packages.
|
|
71 |
Templates are automatically checked for changes and reloaded as
|
|
72 |
neccessary.
|
|
73 |
"""
|
|
74 |
divider = classname.rfind(".") |
|
75 |
if divider > -1: |
|
76 |
package = classname[0:divider] |
|
77 |
basename = classname[divider+1:] |
|
78 |
else: |
|
|
230.1.1
by Steve 'Ashcrow' Milner
Updated to follow pep8. |
79 |
raise ValueError("All templates must be in a package") |
|
159.2.30
by Michael Hudson
trim down turbosimpletal and move it inside loggerhead package. |
80 |
|
81 |
tfile = pkg_resources.resource_filename( |
|
82 |
package, "%s.%s" % (basename, "pt")) |
|
83 |
return zpt(tfile) |