[Checkins] SVN: Sandbox/malthe/chameleon.core/ Use custom serialization method to serialize static default translation blocks.

Malthe Borch mborch at gmail.com
Mon Nov 17 08:40:15 EST 2008


Log message for revision 93047:
  Use custom serialization method to serialize static default translation blocks.

Changed:
  U   Sandbox/malthe/chameleon.core/CHANGES.txt
  U   Sandbox/malthe/chameleon.core/src/chameleon/core/translation.py
  U   Sandbox/malthe/chameleon.core/src/chameleon/core/utils.py

-=-
Modified: Sandbox/malthe/chameleon.core/CHANGES.txt
===================================================================
--- Sandbox/malthe/chameleon.core/CHANGES.txt	2008-11-17 13:09:15 UTC (rev 93046)
+++ Sandbox/malthe/chameleon.core/CHANGES.txt	2008-11-17 13:40:15 UTC (rev 93047)
@@ -4,6 +4,10 @@
 HEAD
 ~~~~
 
+- Use custom serialization method to serialize static default
+  translation blocks; we can't rely on `lxml` for sane
+  output. [malthe]
+
 - Make `default` symbol available to dynamic attribute
   evaluations. [malthe]
 

Modified: Sandbox/malthe/chameleon.core/src/chameleon/core/translation.py
===================================================================
--- Sandbox/malthe/chameleon.core/src/chameleon/core/translation.py	2008-11-17 13:09:15 UTC (rev 93046)
+++ Sandbox/malthe/chameleon.core/src/chameleon/core/translation.py	2008-11-17 13:40:15 UTC (rev 93047)
@@ -417,7 +417,7 @@
                     value = types.value("%s['%s']" % (mapping, name))
                     subclauses.append(clauses.Write(value))
                 else:
-                    subclauses.append(clauses.Out(element.tostring()))
+                    subclauses.append(clauses.Out(utils.serialize(element)))
                     
                 for part in reversed(element.node.tail):
                     if isinstance(part, types.expression):
@@ -459,7 +459,7 @@
 
         msgid = out.getvalue().strip()
         msgid = msgid.replace('  ', ' ').replace('\n', '')
-        
+
         return msgid
 
     def translate_expression(self, value, mapping=None, default=None):

Modified: Sandbox/malthe/chameleon.core/src/chameleon/core/utils.py
===================================================================
--- Sandbox/malthe/chameleon.core/src/chameleon/core/utils.py	2008-11-17 13:09:15 UTC (rev 93046)
+++ Sandbox/malthe/chameleon.core/src/chameleon/core/utils.py	2008-11-17 13:40:15 UTC (rev 93047)
@@ -87,6 +87,37 @@
         
     return string
 
+def serialize(element, encoding=None):
+    return "".join(serialize_element(element, encoding))
+
+def serialize_element(element, encoding):
+    try:
+        name = element.tag.split('}')[-1]
+    except AttributeError:
+        yield element.text; return
+    
+    # tag opening
+    yield "<%s" % name
+
+    # attributes
+    for key, value in element.attrib.items():
+        yield ' %s="%s"' % (key, escape('"', encoding=encoding))
+
+    # elements with no text which have no children are self-closing.
+    if element.text is None and len(element) == 0:
+        yield ' />'; return
+
+    yield '>'
+            
+    if element.text is not None:
+        yield element.text
+
+    for child in element:
+        for string in serialize_element(child, encoding):
+            yield string
+
+    yield '</%s>' % name
+
 class scope(list):
     def __init__(self, *args):
         global s_counter



More information about the Checkins mailing list