diff roundup/cgi/templating.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 760483ce731e
children 11b6601629d7
line wrap: on
line diff
--- a/roundup/cgi/templating.py	Thu Feb 23 14:55:35 2012 +0100
+++ b/roundup/cgi/templating.py	Thu Feb 23 18:10:03 2012 +0100
@@ -20,7 +20,7 @@
 __docformat__ = 'restructuredtext'
 
 
-import sys, cgi, urllib, os, re, os.path, time, errno, mimetypes, csv
+import cgi, urllib, re, os.path, mimetypes, csv
 import calendar, textwrap
 
 from roundup import hyperdb, date, support
@@ -50,9 +50,6 @@
     ReStructuredText = None
 
 # bring in the templating support
-from roundup.cgi.PageTemplates import PageTemplate, GlobalTranslationService
-from roundup.cgi.PageTemplates.Expressions import getEngine
-from roundup.cgi.TAL import TALInterpreter
 from roundup.cgi import TranslationService, ZTUtils
 
 ### i18n services
@@ -60,7 +57,6 @@
 # it is left here for backward compatibility
 # until all Web UI translations are done via client.translator object
 translationService = TranslationService.get_translation()
-GlobalTranslationService.setGlobalTranslationService(translationService)
 
 ### templating
 
@@ -121,12 +117,8 @@
         'with template "%s" (neither "%s" nor "%s")'%(name, view,
         filename, generic))
 
-class Templates:
-    templates = {}
-
-    def __init__(self, dir):
-        self.dir = dir
-
+class TemplatesBase:
+    """Base for engine-specific Templates class."""
     def precompileTemplates(self):
         """ Go through a directory and precompile all the templates therein
         """
@@ -152,63 +144,6 @@
             else:
                 self.get(filename, None)
 
-    def get(self, name, extension=None):
-        """ Interface to get a template, possibly loading a compiled template.
-
-            "name" and "extension" indicate the template we're after, which in
-            most cases will be "name.extension". If "extension" is None, then
-            we look for a template just called "name" with no extension.
-
-            If the file "name.extension" doesn't exist, we look for
-            "_generic.extension" as a fallback.
-        """
-        # default the name to "home"
-        if name is None:
-            name = 'home'
-        elif extension is None and '.' in name:
-            # split name
-            name, extension = name.split('.')
-
-        # find the source
-        src, filename = find_template(self.dir, name, extension)
-
-        # has it changed?
-        try:
-            stime = os.stat(src)[os.path.stat.ST_MTIME]
-        except os.error, error:
-            if error.errno != errno.ENOENT:
-                raise
-
-        if self.templates.has_key(src) and \
-                stime <= self.templates[src].mtime:
-            # compiled template is up to date
-            return self.templates[src]
-
-        # compile the template
-        pt = RoundupPageTemplate()
-        # use pt_edit so we can pass the content_type guess too
-        content_type = mimetypes.guess_type(filename)[0] or 'text/html'
-        pt.pt_edit(open(src).read(), content_type)
-        pt.id = filename
-        pt.mtime = stime
-        # Add it to the cache.  We cannot do this until the template
-        # is fully initialized, as we could otherwise have a race
-        # condition when running with multiple threads:
-        #
-        # 1. Thread A notices the template is not in the cache,
-        #    adds it, but has not yet set "mtime".
-        #
-        # 2. Thread B notices the template is in the cache, checks
-        #    "mtime" (above) and crashes.
-        #
-        # Since Python dictionary access is atomic, as long as we
-        # insert "pt" only after it is fully initialized, we avoid
-        # this race condition.  It's possible that two separate
-        # threads will both do the work of initializing the template,
-        # but the risk of wasted work is offset by avoiding a lock.
-        self.templates[src] = pt
-        return pt
-
     def __getitem__(self, name):
         name, extension = os.path.splitext(name)
         if extension:
@@ -218,6 +153,13 @@
         except NoTemplate, message:
             raise KeyError, message
 
+def get_templates(dir, engine_name):
+    if engine_name == 'chameleon':
+        import engine_chameleon as engine
+    else:
+        import engine_zopetal as engine
+    return engine.Templates(dir)
+
 def context(client, template=None, classname=None, request=None):
     """Return the rendering context dictionary
 
@@ -304,43 +246,6 @@
         c['context'] = HTMLClass(client, classname, anonymous=1)
     return c
 
-class RoundupPageTemplate(PageTemplate.PageTemplate):
-    """A Roundup-specific PageTemplate.
-
-    Interrogate the client to set up Roundup-specific template variables
-    to be available.  See 'context' function for the list of variables.
-
-    """
-
-    # 06-jun-2004 [als] i am not sure if this method is used yet
-    def getContext(self, client, classname, request):
-        return context(client, self, classname, request)
-
-    def render(self, client, classname, request, **options):
-        """Render this Page Template"""
-
-        if not self._v_cooked:
-            self._cook()
-
-        __traceback_supplement__ = (PageTemplate.PageTemplateTracebackSupplement, self)
-
-        if self._v_errors:
-            raise PageTemplate.PTRuntimeError, \
-                'Page Template %s has errors.'%self.id
-
-        # figure the context
-        c = context(client, self, classname, request)
-        c.update({'options': options})
-
-        # and go
-        output = StringIO.StringIO()
-        TALInterpreter.TALInterpreter(self._v_program, self.macros,
-            getEngine().getContext(c), output, tal=1, strictinsert=0)()
-        return output.getvalue()
-
-    def __repr__(self):
-        return '<Roundup PageTemplate %r>'%self.id
-
 class HTMLDatabase:
     """ Return HTMLClasses for valid class fetches
     """

Roundup Issue Tracker: http://roundup-tracker.org/