[Checkins] SVN: z3c.pt/trunk/src/z3c/pt/ Refactor.

Malthe Borch mborch at gmail.com
Wed Aug 6 07:34:34 EDT 2008


Log message for revision 89436:
  Refactor.

Changed:
  U   z3c.pt/trunk/src/z3c/pt/translation.py
  U   z3c.pt/trunk/src/z3c/pt/utils.py

-=-
Modified: z3c.pt/trunk/src/z3c/pt/translation.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/translation.py	2008-08-06 11:34:19 UTC (rev 89435)
+++ z3c.pt/trunk/src/z3c/pt/translation.py	2008-08-06 11:34:33 UTC (rev 89436)
@@ -7,27 +7,9 @@
 import clauses
 import interfaces
 import types
+import utils
 
-def attribute(ns, factory=None, default=None):
-    def get(self):
-        value = self.attrib.get(ns)
-        if value is not None:
-            if factory is None:
-                return value
-
-            f = factory(self._translator())
-            return f(value)
-        elif default is not None:
-            return default
-        
-    def set(self, value):
-        self.attrib[ns] = value
-
-    return property(get, set)
-
-
 class Element(lxml.etree.ElementBase):
-
     def begin(self, stream):
         stream.scope.append(set())
         stream.begin(self._clauses())
@@ -61,7 +43,8 @@
         """The current interpolation strategy is to translate the
         interpolation statements into TAL."""
         
-        translator = self._translator()
+        translator = self.translator
+        
         if self.text is not None:
             while self.text:
                 m = translator.interpolate(self.text)
@@ -108,6 +91,16 @@
                     self.attrib[attributes] += '; %s' % expr
                 else:
                     self.attrib[attributes] = expr
+                    
+    @property
+    def translator(self):
+        while self.default_expression is None:
+            self = self.getparent()
+            if self is None:
+                raise ValueError("Default expression not set.")
+            
+        return component.getUtility(
+            interfaces.IExpressionTranslation, name=self.default_expression)
                 
     def _clauses(self):
         _ = []
@@ -330,51 +323,42 @@
 
         return attributes
 
-    def _translator(self):
-        while self.default_expression is None:
-            self = self.getparent()
-            if self is None:
-                raise ValueError("Default expression not set.")
-            
-        return component.getUtility(
-            interfaces.IExpressionTranslation, name=self.default_expression)
-
-    define = attribute(
+    define = utils.attribute(
         "{http://xml.zope.org/namespaces/tal}define", lambda p: p.definitions)
-    condition = attribute(
+    condition = utils.attribute(
         "{http://xml.zope.org/namespaces/tal}condition",
         lambda p: p.expression)
-    repeat = attribute(
+    repeat = utils.attribute(
         "{http://xml.zope.org/namespaces/tal}repeat", lambda p: p.definition)
-    attributes = attribute(
+    attributes = utils.attribute(
         "{http://xml.zope.org/namespaces/tal}attributes",
         lambda p: p.definitions)
-    content = attribute(
+    content = utils.attribute(
         "{http://xml.zope.org/namespaces/tal}content", lambda p: p.output)
-    replace = attribute(
+    replace = utils.attribute(
         "{http://xml.zope.org/namespaces/tal}replace", lambda p: p.output)
-    omit = attribute(
+    omit = utils.attribute(
         "{http://xml.zope.org/namespaces/tal}omit-tag", lambda p: p.expression)
-    i18n_translate = attribute(
+    i18n_translate = utils.attribute(
         "{http://xml.zope.org/namespaces/i18n}translate")
-    i18n_attributes = attribute(
+    i18n_attributes = utils.attribute(
         "{http://xml.zope.org/namespaces/i18n}attributes", lambda p: p.mapping)
-    i18n_domain = attribute(
+    i18n_domain = utils.attribute(
         "{http://xml.zope.org/namespaces/i18n}domain")
-    i18n_name = attribute(
+    i18n_name = utils.attribute(
         "{http://xml.zope.org/namespaces/i18n}name")
-    default_expression = attribute(
+    default_expression = utils.attribute(
         "{http://xml.zope.org/namespaces/tal}default-expression")
     
 class TALElement(Element):
-    define = attribute("define", lambda p: p.definitions)
-    condition = attribute("condition", lambda p: p.expression)
-    replace = attribute("replace", lambda p: p.output)
-    repeat = attribute("repeat", lambda p: p.definition)
-    attributes = attribute("attributes", lambda p: p.expression)
-    content = attribute("content", lambda p: p.output)
-    omit = attribute("omit-tag", lambda p: p.expression, u"")
-    default_expression = attribute("default-expression", lambda p: p.name)
+    define = utils.attribute("define", lambda p: p.definitions)
+    condition = utils.attribute("condition", lambda p: p.expression)
+    replace = utils.attribute("replace", lambda p: p.output)
+    repeat = utils.attribute("repeat", lambda p: p.definition)
+    attributes = utils.attribute("attributes", lambda p: p.expression)
+    content = utils.attribute("content", lambda p: p.output)
+    omit = utils.attribute("omit-tag", lambda p: p.expression, u"")
+    default_expression = utils.attribute("default-expression", lambda p: p.name)
     
     def _static_attributes(self):
         attributes = {}

Modified: z3c.pt/trunk/src/z3c/pt/utils.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/utils.py	2008-08-06 11:34:19 UTC (rev 89435)
+++ z3c.pt/trunk/src/z3c/pt/utils.py	2008-08-06 11:34:33 UTC (rev 89436)
@@ -13,6 +13,8 @@
              "an encoding that coerces gracefully to "
              "unicode is used ('utf-8' recommended)." % sys.getdefaultencoding())
 
+s_counter = 0
+
 def handler(key=None):
     def decorate(f):
         def g(node):
@@ -23,8 +25,23 @@
         return g
     return decorate
 
-s_counter = 0
+def attribute(ns, factory=None, default=None):
+    def get(self):
+        value = self.attrib.get(ns)
+        if value is not None:
+            if factory is None:
+                return value
 
+            f = factory(self.translator)
+            return f(value)
+        elif default is not None:
+            return default
+        
+    def set(self, value):
+        self.attrib[ns] = value
+
+    return property(get, set)
+
 class scope(list):
     def __init__(self, *args):
         global s_counter



More information about the Checkins mailing list