[Checkins] SVN: z3c.pt/trunk/ Dynamic attributes are now ordered as they appear in the template.

Malthe Borch mborch at gmail.com
Mon Sep 1 06:37:49 EDT 2008


Log message for revision 90639:
  Dynamic attributes are now ordered as they appear in the template.

Changed:
  U   z3c.pt/trunk/CHANGES.txt
  U   z3c.pt/trunk/src/z3c/pt/i18n.txt
  U   z3c.pt/trunk/src/z3c/pt/translation.py
  U   z3c.pt/trunk/src/z3c/pt/utils.py
  U   z3c.pt/trunk/src/z3c/pt/zpt.txt

-=-
Modified: z3c.pt/trunk/CHANGES.txt
===================================================================
--- z3c.pt/trunk/CHANGES.txt	2008-09-01 09:25:09 UTC (rev 90638)
+++ z3c.pt/trunk/CHANGES.txt	2008-09-01 10:37:49 UTC (rev 90639)
@@ -13,6 +13,9 @@
 
   Features
 
+- Dynamic attributes are now ordered as they appear in the
+  template. [malthe]
+  
 - Added ``symbol_mapping`` attribute to code streams such that
   function dependencies can be registered at compile-time. [malthe]
 

Modified: z3c.pt/trunk/src/z3c/pt/i18n.txt
===================================================================
--- z3c.pt/trunk/src/z3c/pt/i18n.txt	2008-09-01 09:25:09 UTC (rev 90638)
+++ z3c.pt/trunk/src/z3c/pt/i18n.txt	2008-09-01 10:37:49 UTC (rev 90639)
@@ -221,7 +221,7 @@
   >>> template = PageTemplate(body)
   >>> print template.render()
   <div>
-    <span longdesc="A not so short description." title="Simple Title">
+    <span title="Simple Title" longdesc="A not so short description.">
       Default
     </span>
   </div>
@@ -230,8 +230,8 @@
 
   >>> print template.render(target_language='de')
   <div>
-    <span longdesc="Mock translation of 'desc_short'."
-          title="Mock translation of 'title_simple'.">
+    <span title="Mock translation of 'title_simple'."
+          longdesc="Mock translation of 'desc_short'.">
       Default
     </span>
   </div>

Modified: z3c.pt/trunk/src/z3c/pt/translation.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/translation.py	2008-09-01 09:25:09 UTC (rev 90638)
+++ z3c.pt/trunk/src/z3c/pt/translation.py	2008-09-01 10:37:49 UTC (rev 90639)
@@ -120,25 +120,27 @@
         dynamic = content or self.translate is not None
 
         # static attributes are at the bottom of the food chain
-        attributes = self.static_attributes
+        attributes = utils.odict()
+        attributes.update(self.static_attributes)
 
         # dynamic attributes
-        attrs = self.dynamic_attributes or ()
-        dynamic_attributes = tuple(attrs)
-
-        for variables, expression in attrs:
+        dynamic_attrs = self.dynamic_attributes or ()
+        dynamic_attr_names = []
+        
+        for variables, expression in dynamic_attrs:
             if len(variables) != 1:
                 raise ValueError("Tuple definitions in assignment clause "
                                      "is not supported.")
 
             variable = variables[0]
             attributes[variable] = expression
+            dynamic_attr_names.append(variable)
 
         # translated attributes
         translated_attributes = self.translated_attributes or ()
         for variable, msgid in translated_attributes:
             if msgid:
-                if variable in dynamic_attributes:
+                if variable in dynamic_attr_names:
                     raise ValueError(
                         "Message id not allowed in conjunction with "
                         "a dynamic attribute.")
@@ -151,7 +153,7 @@
                 else:
                     expression = self.translate_expression(value)
             else:
-                if variable in dynamic_attributes or variable in attributes:
+                if variable in dynamic_attr_names or variable in attributes:
                     text = '"%s"' % attributes[variable]
                     expression = self.translate_expression(text)
                 else:

Modified: z3c.pt/trunk/src/z3c/pt/utils.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/utils.py	2008-09-01 09:25:09 UTC (rev 90638)
+++ z3c.pt/trunk/src/z3c/pt/utils.py	2008-09-01 10:37:49 UTC (rev 90639)
@@ -2,6 +2,8 @@
 import logging
 import config
 
+from UserDict import UserDict
+
 # check if we're able to coerce unicode to str
 try:
     str(u'La Pe\xf1a')
@@ -118,6 +120,59 @@
             
         return value
 
+class odict(UserDict):
+    def __init__(self, dict = None):
+        self._keys = []
+        UserDict.__init__(self, dict)
+
+    def __delitem__(self, key):
+        UserDict.__delitem__(self, key)
+        self._keys.remove(key)
+
+    def __setitem__(self, key, item):
+        UserDict.__setitem__(self, key, item)
+        if key in self._keys:
+            self._keys.remove(key)
+        self._keys.append(key)
+
+    def clear(self):
+        UserDict.clear(self)
+        self._keys = []
+
+    def copy(self):
+        dict = UserDict.copy(self)
+        dict._keys = self._keys[:]
+        return dict
+
+    def items(self):
+        return zip(self._keys, self.values())
+
+    def keys(self):
+        return self._keys
+
+    def popitem(self):
+        try:
+            key = self._keys[-1]
+        except IndexError:
+            raise KeyError('dictionary is empty')
+
+        val = self[key]
+        del self[key]
+
+        return (key, val)
+
+    def setdefault(self, key, failobj = None):
+        UserDict.setdefault(self, key, failobj)
+        if key not in self._keys: self._keys.append(key)
+
+    def update(self, dict):
+        UserDict.update(self, dict)
+        for key in dict.keys():
+            if key not in self._keys: self._keys.append(key)
+
+    def values(self):
+        return map(self.get, self._keys)
+    
 def get_attributes_from_namespace(element, namespace):
     if element.nsmap.get(element.prefix) == namespace:
         return dict([

Modified: z3c.pt/trunk/src/z3c/pt/zpt.txt
===================================================================
--- z3c.pt/trunk/src/z3c/pt/zpt.txt	2008-09-01 09:25:09 UTC (rev 90638)
+++ z3c.pt/trunk/src/z3c/pt/zpt.txt	2008-09-01 10:37:49 UTC (rev 90639)
@@ -31,7 +31,7 @@
   ...   <span tal:content="None" />
   ... </div>""")
     <div>
-      <span id="test" style="hij" class="defabc">abcghi</span>
+      <span id="test" class="defabc" style="hij">abcghi</span>
       Hello World!
       Hello World!
       <span></span>



More information about the Checkins mailing list