[Erp5-report] r37204 nicolas - /erp5/trunk/utils/xml_marshaller/xml_marshaller/

nobody at svn.erp5.org nobody at svn.erp5.org
Tue Jul 20 18:52:46 CEST 2010


Author: nicolas
Date: Tue Jul 20 18:52:45 2010
New Revision: 37204

URL: http://svn.erp5.org?rev=37204&view=rev
Log:
Add support of set type

Modified:
    erp5/trunk/utils/xml_marshaller/xml_marshaller/xml_marshaller.py

Modified: erp5/trunk/utils/xml_marshaller/xml_marshaller/xml_marshaller.py
URL: http://svn.erp5.org/erp5/trunk/utils/xml_marshaller/xml_marshaller/xml_marshaller.py?rev=37204&r1=37203&r2=37204&view=diff
==============================================================================
--- erp5/trunk/utils/xml_marshaller/xml_marshaller/xml_marshaller.py [utf8] (original)
+++ erp5/trunk/utils/xml_marshaller/xml_marshaller/xml_marshaller.py [utf8] Tue Jul 20 18:52:45 2010
@@ -57,6 +57,7 @@ class Marshaller(object):
     self.tag_code = E.code
     self.tag_none = E.none
     self.tag_instance = E.object
+    self.tag_set = E.set
 
   # The four basic functions that form the caller's interface
   def dump(self, value, file):
@@ -189,6 +190,16 @@ class Marshaller(object):
       xml_tree.append(self._marshal(v, kw))
     return xml_tree
 
+  def m_set(self, value, kw):
+    kw['id'] += 1
+    i = str(kw['id'])
+    kw[str(id(value))] = i
+    kw[i] = value
+    xml_tree = self.tag_set(id='i%s' % i)
+    for elem in value:
+      xml_tree.append(self._marshal(elem, kw))
+    return xml_tree
+
   # Python 2.2 renames dictionary to dict.
   def m_dict(self, value, kw):
     return self.m_dictionary(value, kw)
@@ -250,6 +261,7 @@ class Marshaller(object):
 TUPLE = {}
 LIST = {}
 DICT = {}
+SET = {}
 
 class Unmarshaller(ElementTreeContentHandler):
   # This dictionary maps element names to the names of starting and ending
@@ -270,7 +282,8 @@ class Unmarshaller(ElementTreeContentHan
       'reference': ('um_start_reference', None),
       'code': ('um_start_code', 'um_end_code'),
       'none': ('um_start_none', 'um_end_none'),
-      'object': ('um_start_instance', 'um_end_instance')
+      'object': ('um_start_instance', 'um_end_instance'),
+      'set': ('um_start_set', 'um_end_set'),
       }
 
   def __init__(self):
@@ -476,6 +489,14 @@ class Unmarshaller(ElementTreeContentHan
       self.kw[id] = L
     self.data_stack.append(L)
 
+  def um_start_set(self, name, attrs):
+    self.data_stack.append(SET)
+    S = set()
+    if 'id' in attrs:
+      id = attrs['id']
+      self.kw[id] = S
+    self.data_stack.append(S)
+
   def um_end_list(self, name):
     ds = self.data_stack
     for index in range(len(ds)-1, -1, -1):
@@ -486,6 +507,16 @@ class Unmarshaller(ElementTreeContentHan
     L[:] = ds[index + 2:len(ds)]
     ds[index:] = [L]
 
+  def um_end_set(self, name):
+    ds = self.data_stack
+    for index in range(len(ds)-1, -1, -1):
+      if ds[index] is SET:
+        break
+    assert index != -1
+    S = ds[index + 1]
+    [S.add(item) for item in ds[index + 2:len(ds)]]
+    ds[index:] = [S]
+
   def um_start_tuple(self, name, attrs):
     self.data_stack.append(TUPLE)
 
@@ -611,6 +642,7 @@ def runtests(namespace_uri=None):
        ['alpha', 'beta', 'gamma'],
        {'key': 'value', 1: 2},
        'éàù^ç'.decode('utf-8'),
+       set(('a', 1,)),
        ]
   if namespace_uri:
     test(load, loads, dump_ns, dumps_ns, L)




More information about the Erp5-report mailing list