[Zope-Checkins] SVN: Zope/branches/ajung-final-zpt-integration/lib/python/Products/PageTemplates/ - refactored Engine and moved it out from Five

Andreas Jung andreas at andreas-jung.com
Wed Mar 1 18:07:11 EST 2006


Log message for revision 65686:
  - refactored Engine and moved it out from Five
  - translation service needs to be rehooked
  

Changed:
  A   Zope/branches/ajung-final-zpt-integration/lib/python/Products/PageTemplates/Engine.py
  U   Zope/branches/ajung-final-zpt-integration/lib/python/Products/PageTemplates/PageTemplateFile.py
  U   Zope/branches/ajung-final-zpt-integration/lib/python/Products/PageTemplates/ZopePageTemplate.py

-=-
Added: Zope/branches/ajung-final-zpt-integration/lib/python/Products/PageTemplates/Engine.py
===================================================================
--- Zope/branches/ajung-final-zpt-integration/lib/python/Products/PageTemplates/Engine.py	2006-03-01 22:47:54 UTC (rev 65685)
+++ Zope/branches/ajung-final-zpt-integration/lib/python/Products/PageTemplates/Engine.py	2006-03-01 23:07:10 UTC (rev 65686)
@@ -0,0 +1,95 @@
+
+# Common Engine for Zope3-ZPT-in-Zope-2
+
+
+from zope.tales.tales import ExpressionEngine
+from zope.tales.expressions import PathExpr, StringExpr, NotExpr, DeferExpr, SubPathExpr
+from zope.tales.expressions import SimpleModuleImporter, _marker
+from zope.tales.pythonexpr import PythonExpr
+from zope.tales.tales import _valid_name, _parse_expr, NAME_RE, Undefined 
+
+
+def BoboTraverseAwareSimpleTraverse(object, path_items, econtext):
+    """ a slightly modified version of zope.tales.expressions.simpleTraverse()
+        that interacts correctly with objects implementing bobo_traverse().
+    """
+
+    for name in path_items:
+        next = getattr(object, name, _marker)
+        if next is not _marker:
+            object = next
+        else:
+            try:
+                object = object.restrictedTraverse(name)
+            except (KeyError, AttributeError):
+                try:
+                    object = object[name]
+                except:
+                    object = getattr(object, name)
+
+    return object
+
+
+class PathExpr(PathExpr):
+    """We need to subclass PathExpr at this point since there is no other
+       away to pass our own traverser because we do not instantiate 
+       PathExpr on our own...this sucks!
+    """
+
+    def __init__(self, name, expr, engine, traverser=BoboTraverseAwareSimpleTraverse):
+        self._s = expr
+        self._name = name
+        paths = expr.split('|')
+        self._subexprs = []
+        add = self._subexprs.append
+        for i in range(len(paths)):
+            path = paths[i].lstrip()
+            if _parse_expr(path):
+                # This part is the start of another expression type,
+                # so glue it back together and compile it.
+                add(engine.compile('|'.join(paths[i:]).lstrip()))
+                break
+            add(SubPathExpr(path, traverser, engine)._eval)
+
+
+from zope.tales.tales import Context
+
+
+from zope.i18n import translate
+
+class Context(Context):
+
+
+    def translate(self, msgid, domain=None, mapping=None, default=None):
+        # fix that
+        return msgid
+#        return translate(msgid, domain, mapping,
+#                         context=context, default=default)
+
+
+
+class ExpressionEngine(ExpressionEngine):
+    
+    def getContext(self, contexts=None, **kwcontexts):
+        if contexts is not None:
+            if kwcontexts:
+                kwcontexts.update(contexts)
+            else:
+                kwcontexts = contexts
+        return Context(self, kwcontexts)
+
+
+def Engine():
+    e = ExpressionEngine()
+    reg = e.registerType
+    for pt in PathExpr._default_type_names:
+        reg(pt, PathExpr)
+    reg('string', StringExpr)
+    reg('python', PythonExpr)
+    reg('not', NotExpr)
+    reg('defer', DeferExpr)
+    e.registerBaseName('modules', SimpleModuleImporter())
+    return e
+
+Engine = Engine()
+

Modified: Zope/branches/ajung-final-zpt-integration/lib/python/Products/PageTemplates/PageTemplateFile.py
===================================================================
--- Zope/branches/ajung-final-zpt-integration/lib/python/Products/PageTemplates/PageTemplateFile.py	2006-03-01 22:47:54 UTC (rev 65685)
+++ Zope/branches/ajung-final-zpt-integration/lib/python/Products/PageTemplates/PageTemplateFile.py	2006-03-01 23:07:10 UTC (rev 65686)
@@ -32,7 +32,9 @@
 from OFS.SimpleItem import Item_w__name__
 from Shared.DC.Scripts.Signature import FuncCode
 
+from Engine import Engine
 
+
 class PageTemplateFile(SimpleItem, Script, PT, Traversable):
     """ A Zope 2-aware wrapper class around the Zope 3 ZPT
         PageTemplateFile implementation.
@@ -86,6 +88,9 @@
         self.pt_edit( content, guess_type(filename, content))
 
 
+    def pt_getEngine(self):
+        return Engine
+
     def pt_getContext(self):
         root = self.getPhysicalRoot()
         context = self._getContext()

Modified: Zope/branches/ajung-final-zpt-integration/lib/python/Products/PageTemplates/ZopePageTemplate.py
===================================================================
--- Zope/branches/ajung-final-zpt-integration/lib/python/Products/PageTemplates/ZopePageTemplate.py	2006-03-01 22:47:54 UTC (rev 65685)
+++ Zope/branches/ajung-final-zpt-integration/lib/python/Products/PageTemplates/ZopePageTemplate.py	2006-03-01 23:07:10 UTC (rev 65686)
@@ -38,6 +38,8 @@
 from zope.pagetemplate.pagetemplate import PageTemplate 
 from zope.pagetemplate.pagetemplatefile import sniff_type
 
+from Engine import Engine
+
 # regular expression to extract the encoding from the XML preamble
 encoding_reg= re.compile('<\?xml.*?encoding="(.*?)".*?\?>', re.M)
 
@@ -175,6 +177,10 @@
         self.ZCacheable_invalidate()
 
 
+    def pt_getEngine(self):
+        return Engine
+
+
     security.declareProtected(change_page_templates, 'pt_upload')
     def pt_upload(self, REQUEST, file='', encoding='utf-8'):
         """Replace the document with the text in file."""



More information about the Zope-Checkins mailing list