[pitivi] Deserialize correctly the longinteger values. Fixes bug 634715.



commit 727ea84d630952490ec3d777a9e8c08371bbeffc
Author: Alex BÄ?luÈ? <alexandru balut gmail com>
Date:   Thu Apr 28 23:39:14 2011 +0200

    Deserialize correctly the longinteger values.
    Fixes bug 634715.

 pitivi/formatters/etree.py    |   54 +++++++++++++++++++++++-----------------
 tests/test_etree_formatter.py |   25 +++++++++++++++++++
 2 files changed, 56 insertions(+), 23 deletions(-)
---
diff --git a/pitivi/formatters/etree.py b/pitivi/formatters/etree.py
index 1e8bf71..3485a05 100644
--- a/pitivi/formatters/etree.py
+++ b/pitivi/formatters/etree.py
@@ -92,13 +92,18 @@ class ElementTreeFormatter(Formatter):
             yield name, value
 
     def _parsePropertyValue(self, value):
-        # nothing to read here, move along
-        # edward: argh, I went past there, what shall I do now ?
-
-        #FIXME
+        # We treat the GEnum values differently because they are actually ints.
         if "(GEnum)" in value:
             return int(value.split("(GEnum)")[1])
-        return gst.Caps("meh, name=%s" % value)[0]["name"]
+        # We treat the guint64 values differently because when we serialize
+        # a long integer, the serialization is, for example, "(guint64) 4L",
+        # and gst.Caps() fails to parse it, because it has "L" at the end.
+        if "(guint64)" in value:
+            value = value.rstrip('lL')
+        # TODO: Use what gst.Caps() uses to parse a property value.
+        caps = gst.Caps("structure1, property1=%s;" % value)
+        structure = caps[0]
+        return structure["property1"]
 
     def _saveStream(self, stream):
         element = Element("stream")
@@ -247,25 +252,28 @@ class ElementTreeFormatter(Formatter):
         self._sources = res
         return res
 
-    def _serializeDict(self, element, dict):
-        for a, b in dict.iteritems():
-            print a, b, type(b)
-            if isinstance(b, str):
-                element.attrib[a] = b
-            elif isinstance(b, bool):
-                element.attrib[a] = "(boolean) %r" % b
-            elif isinstance(b, float):
-                element.attrib[a] = "(float) %r" % b
+    def _serializeDict(self, element, values_dict):
+        """Serialize the specified dict into the specified Element instance."""
+        for key, value in values_dict.iteritems():
+            if isinstance(value, str):
+                # TODO: If this starts with "(<type>)", or if it contains ";",
+                # the deserialization might fail.
+                serialized_value = value
+            elif isinstance(value, bool):
+                serialized_value = "(boolean) %r" % value
+            elif isinstance(value, float):
+                serialized_value = "(float) %r" % value
             else:
-                element.attrib[a] = "(guint64) %r" % b
-
+                serialized_value = "(guint64) %r" % value
+            element.attrib[key] = serialized_value
 
     def _deserializeDict(self, element):
-        d = {}
-        for a, b in element.attrib.iteritems():
-            if len(b):
-                d[a] = self._parsePropertyValue(b)
-        return d
+        """Get the specified Element as a deserialized dict."""
+        values_dict = {}
+        for name, value_string in element.attrib.iteritems():
+            if value_string:
+                values_dict[name] = self._parsePropertyValue(value_string)
+        return values_dict
 
     def _saveProjectSettings(self, settings):
         element = Element('export-settings')
@@ -358,9 +366,9 @@ class ElementTreeFormatter(Formatter):
         effect = track_object.getElement()
         properties = gobject.list_properties(effect)
         for prop in properties:
-            type_name = str(gobject.type_name(prop.value_type.fundamental))
-            #FIXME we just take the int equivalent to the GEnum, how should it be handled?
             if prop.flags & gobject.PARAM_READABLE:
+                type_name = str(gobject.type_name(prop.value_type.fundamental))
+                #FIXME we just take the int equivalent to the GEnum, how should it be handled?
                 if type_name == "GEnum":
                     value = str(effect.get_property(prop.name).__int__())
                 else:
diff --git a/tests/test_etree_formatter.py b/tests/test_etree_formatter.py
index 31528fa..e33379c 100644
--- a/tests/test_etree_formatter.py
+++ b/tests/test_etree_formatter.py
@@ -44,6 +44,31 @@ class FakeElementTreeFormatter(ElementTreeFormatter):
 def ts(time):
     return "(gint64)%s" % time
 
+class TestSerialization(TestCase):
+    def setUp(self):
+        self.formatter = FakeElementTreeFormatter(EffectsHandler())
+
+    def testSerializeAndDeserialize(self):
+        element = Element('tag')
+        values_dict = {
+                'str_': 'four',
+                'boolean_': True,
+                'float_': 4.0,
+                'guint64_': 4,
+                'guint64_2': 4L}
+        self.formatter._serializeDict(element, values_dict)
+        # Make sure that all the keys end up in element.
+        for key, unused_value in values_dict.iteritems():
+            self.assertTrue(key in element.attrib)
+
+        deserialized_values_dict = self.formatter._deserializeDict(element)
+        # Make sure that all the keys in the original dict end up in the
+        # deserialized dict.
+        self.assertEqual(
+                set(values_dict.keys()), set(deserialized_values_dict.keys()))
+        for key, value in values_dict.iteritems():
+            self.assertEqual(value, deserialized_values_dict[key])
+
 class TestFormatterSave(TestCase):
     def setUp(self):
         self.formatter = FakeElementTreeFormatter(EffectsHandler())



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]