Mercurial > p > roundup > code
annotate roundup/cgi/engine_zopetal.py @ 4587:a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
We now have two configurable templating engines, the old Zope TAL
templates (called zopetal in the config) and the new Chameleon (called
chameleon in the config). A new config-option "template_engine" under
[main] can take these config-options, the default is zopetal.
Thanks to Cheer Xiao for the idea of making this configurable *and*
for the actual implementation!
Cheer Xiao commit log:
- The original TAL engine ported from Zope is thereafter referred to as
"zopetal", in speech and in code
- A new option "template_engine" under [main] introduced
- Zopetal-specific code stripped from cgi/templating.py to form the new
cgi/engine_zopetal.py
- Interface to Chameleon in cgi/engine_chameleon.py
- Engines are supposed to provide a Templates class that mimics the
behavior of the old cgi.templating.Templates. The Templates class is
preferably subclassed from cgi.templating.TemplatesBase.
- New function cgi.templating.get_templates to get the appropriate engine's
Templates instance according to the engine name
| author | Ralf Schlatterbeck <rsc@runtux.com> |
|---|---|
| date | Thu, 23 Feb 2012 18:10:03 +0100 |
| parents | |
| children | 05fe39f1d823 |
| rev | line source |
|---|---|
|
4587
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
1 """Templating engine adapter for the legacy TAL implementation ported from |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
2 Zope. |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
3 """ |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
4 __docformat__ = 'restructuredtext' |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
5 |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
6 import errno |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
7 import mimetypes |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
8 import os |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
9 import os.path |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
10 |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
11 from roundup.cgi.templating import StringIO, context, translationService, find_template, TemplatesBase |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
12 from roundup.cgi.PageTemplates import PageTemplate, GlobalTranslationService |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
13 from roundup.cgi.PageTemplates.Expressions import getEngine |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
14 from roundup.cgi.TAL import TALInterpreter |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
15 |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
16 GlobalTranslationService.setGlobalTranslationService(translationService) |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
17 |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
18 class Templates(TemplatesBase): |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
19 templates = {} |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
20 |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
21 def __init__(self, dir): |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
22 self.dir = dir |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
23 |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
24 def get(self, name, extension=None): |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
25 """ Interface to get a template, possibly loading a compiled template. |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
26 |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
27 "name" and "extension" indicate the template we're after, which in |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
28 most cases will be "name.extension". If "extension" is None, then |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
29 we look for a template just called "name" with no extension. |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
30 |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
31 If the file "name.extension" doesn't exist, we look for |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
32 "_generic.extension" as a fallback. |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
33 """ |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
34 # default the name to "home" |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
35 if name is None: |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
36 name = 'home' |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
37 elif extension is None and '.' in name: |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
38 # split name |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
39 name, extension = name.split('.') |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
40 |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
41 # find the source |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
42 src, filename = find_template(self.dir, name, extension) |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
43 |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
44 # has it changed? |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
45 try: |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
46 stime = os.stat(src)[os.path.stat.ST_MTIME] |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
47 except os.error, error: |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
48 if error.errno != errno.ENOENT: |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
49 raise |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
50 |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
51 if self.templates.has_key(src) and \ |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
52 stime <= self.templates[src].mtime: |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
53 # compiled template is up to date |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
54 return self.templates[src] |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
55 |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
56 # compile the template |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
57 pt = RoundupPageTemplate() |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
58 # use pt_edit so we can pass the content_type guess too |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
59 content_type = mimetypes.guess_type(filename)[0] or 'text/html' |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
60 pt.pt_edit(open(src).read(), content_type) |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
61 pt.id = filename |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
62 pt.mtime = stime |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
63 # Add it to the cache. We cannot do this until the template |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
64 # is fully initialized, as we could otherwise have a race |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
65 # condition when running with multiple threads: |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
66 # |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
67 # 1. Thread A notices the template is not in the cache, |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
68 # adds it, but has not yet set "mtime". |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
69 # |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
70 # 2. Thread B notices the template is in the cache, checks |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
71 # "mtime" (above) and crashes. |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
72 # |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
73 # Since Python dictionary access is atomic, as long as we |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
74 # insert "pt" only after it is fully initialized, we avoid |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
75 # this race condition. It's possible that two separate |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
76 # threads will both do the work of initializing the template, |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
77 # but the risk of wasted work is offset by avoiding a lock. |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
78 self.templates[src] = pt |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
79 return pt |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
80 |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
81 class RoundupPageTemplate(PageTemplate.PageTemplate): |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
82 """A Roundup-specific PageTemplate. |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
83 |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
84 Interrogate the client to set up Roundup-specific template variables |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
85 to be available. See 'context' function for the list of variables. |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
86 |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
87 """ |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
88 |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
89 def render(self, client, classname, request, **options): |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
90 """Render this Page Template""" |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
91 |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
92 if not self._v_cooked: |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
93 self._cook() |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
94 |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
95 __traceback_supplement__ = (PageTemplate.PageTemplateTracebackSupplement, self) |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
96 |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
97 if self._v_errors: |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
98 raise PageTemplate.PTRuntimeError, \ |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
99 'Page Template %s has errors.'%self.id |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
100 |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
101 # figure the context |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
102 c = context(client, self, classname, request) |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
103 c.update({'options': options}) |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
104 |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
105 # and go |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
106 output = StringIO.StringIO() |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
107 TALInterpreter.TALInterpreter(self._v_program, self.macros, |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
108 getEngine().getContext(c), output, tal=1, strictinsert=0)() |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
109 return output.getvalue() |
|
a2eb4fb3e6d8
New Chameleon templating engine, engine is now configurable.
Ralf Schlatterbeck <rsc@runtux.com>
parents:
diff
changeset
|
110 |
