conduit r1236 - in trunk: . conduit test/python-tests



Author: jstowers
Date: Thu Jan 17 00:14:53 2008
New Revision: 1236
URL: http://svn.gnome.org/viewvc/conduit?rev=1236&view=rev

Log:
2008-01-17  John Stowers  <john stowers gmail com>

	* conduit/TypeConverter.py:
	* test/python-tests/TestCoreConvertSubtypesArgs.py: Conversions are now
	performed recursively. Fixes Tomboy <--> Folder sync bug



Modified:
   trunk/ChangeLog
   trunk/conduit/TypeConverter.py
   trunk/test/python-tests/TestCoreConvertSubtypesArgs.py

Modified: trunk/conduit/TypeConverter.py
==============================================================================
--- trunk/conduit/TypeConverter.py	(original)
+++ trunk/conduit/TypeConverter.py	Thu Jan 17 00:14:53 2008
@@ -150,7 +150,32 @@
             if self.convertables[from_type].has_key(to_type):
                 return True
         return False
+        
+    def _convert(self, conversions, data):
+        if len(conversions) > 0:
+            from_type, to_type, args = conversions[0]
+            message = "Converting"
+            if from_type == to_type:
+                message = "Transcoding"
+                #No conversion needed, or module does not supply transcode.
+                if args == {} or not self._conversion_exists(from_type, to_type):
+                    #recurse
+                    log.debug("Skipping %s -> %s" % (from_type, to_type))
+                    return self._convert(conversions[1:],data)
 
+            log.debug("%s %s -> %s (args: %s)" % (message, from_type, to_type, args))
+            try:
+                #recurse
+                return self._convert(
+                                conversions[1:],
+                                self.convertables[from_type][to_type](data, **args)
+                                )
+            except Exception:
+                log.debug(traceback.format_exc())
+                raise Exceptions.ConversionError(from_type, to_type)
+        else:
+            return data
+                        
     def conversion_exists(self, from_type, to_type):
         """
         Checks if all conversion(s) exists to convert from from_type 
@@ -178,38 +203,13 @@
         conversions = self._get_conversions(from_type, to_type)
         log.debug("Convert %s -> %s using %s" % (from_type, to_type, conversions))
 
-        newdata = data
-        for from_type, to_type, args in conversions:
-            conversionExists = self._conversion_exists(from_type, to_type)
-            #if fromtype and totype differ only through args, then check if that
-            #datatype has a transcode function (a convert function whose in and
-            #out types are the same)
-            if from_type == to_type:
-                if args == {}:
-                    return data
-                elif conversionExists == True:
-                    try:
-                        log.debug("Transcoding %s (args: %s)" % (from_type, args))
-                        to = self.convertables[from_type][to_type](data, **args)
-                        newdata = self._retain_info_in_conversion(fromdata=data, todata=to)
-                    except Exception:
-                        log.debug(traceback.format_exc())
-                        raise Exceptions.ConversionError(from_type, to_type)
-                else:
-                    return data
-            #perform the conversion
-            elif conversionExists == True:
-                try:
-                    log.debug("Converting %s -> %s (args: %s)" % (from_type, to_type, args))
-                    to = self.convertables[from_type][to_type](data, **args)
-                    newdata = self._retain_info_in_conversion(fromdata=data, todata=to)
-                except Exception:
-                    log.debug(traceback.format_exc())
-                    raise Exceptions.ConversionError(from_type, to_type)
-            else:
-                raise Exceptions.ConversionDoesntExistError(from_type, to_type)
-        
-        return newdata
+        if self.conversion_exists(from_type, to_type):
+            #recursively perform the needed conversions
+            newdata = self._convert(conversions, data)
+        else:
+            raise Exceptions.ConversionDoesntExistError(from_type, to_type)
+            
+        return self._retain_info_in_conversion(fromdata=data, todata=newdata)
 
     def get_convertables_list(self):
         """

Modified: trunk/test/python-tests/TestCoreConvertSubtypesArgs.py
==============================================================================
--- trunk/test/python-tests/TestCoreConvertSubtypesArgs.py	(original)
+++ trunk/test/python-tests/TestCoreConvertSubtypesArgs.py	Thu Jan 17 00:14:53 2008
@@ -9,18 +9,39 @@
     def __init__(self):
         DataType.DataType.__init__(self)
         
+class FooBarData(FooData):
+    _name_ = "foo/bar"
+    def __init__(self):
+        FooData.__init__(self)
+        
+class BazData(DataType.DataType):
+    _name_ = "baz"
+    def __init__(self):
+        DataType.DataType.__init__(self)
+        
+class BazBobData(BazData):
+    _name_ = "baz/bob"
+    def __init__(self):
+        BazData.__init__(self)
+        
 class FooConverter(object):
     def __init__(self):
         self.conversions =  {
-                "foo,foo"           : self.convert,     #transcode
-                "foo,foo/bar"       : self.convert,
-                "foo/bar,foo/bar"   : self.convert,     #transcode
-                "foo,baz"           : self.convert,
-                "baz,baz/bob"       : self.convert,
-                "conversion,error"  : self.dont_convert,
+                "foo,foo"           : self.transcode,
+                "foo,foo/bar"       : self.foo_to_foobar,
+                "foo/bar,foo/bar"   : self.transcode,
+                "foo,baz"           : self.foo_to_baz,
+                "baz,baz/bob"       : self.baz_to_bazbob,
+                "foo,error"         : self.dont_convert,
         }
-    def convert(self, data, **kwargs):
+    def transcode(self, data, **kwargs):
         return data
+    def foo_to_foobar(self, data, **kwargs):
+        return FooBarData()
+    def foo_to_baz(self, data, **kwargs):
+        return BazData()
+    def baz_to_bazbob(self, data, **kwargs):
+        return BazBobData()
     def dont_convert(self, data, **kwargs):
         raise Exception
 
@@ -39,19 +60,19 @@
 
 #Conversions to try
 TEST_CONVERSIONS = (
-    #from           #to             #exist  #expected conversion sequence
-    ("foo",         "foo",          True,   ("foo->foo",)),
-    ("foo",         "foo/bar",      True,   ("foo->foo/bar",)),
-    ("foo/bar",     "foo",          True,   ("foo->foo",)),
-    ("foo",         "baz",          True,   ("foo->baz",)),
-    ("foo",         "baz/bob",      True,   ("foo->baz","baz->baz/bob")),
-    ("foo/bar",     "baz/bob",      True,   ("foo->baz","baz->baz/bob")),
-    ("baz/bob",     "baz/bob",      True,   ("baz/bob->baz/bob",)),
-    ("no",          "conversion",   False,  False),
-    ("conversion",  "error",        True,   False)
+    #from           #from class     #to             #to class   #exist  #expected conversion sequence
+    ("foo",         FooData,        "foo",          FooData,    True,   ("foo->foo",)),
+    ("foo",         FooData,        "foo/bar",      FooBarData, True,   ("foo->foo/bar",)),
+    ("foo/bar",     FooBarData,     "foo",          FooBarData, True,   ("foo->foo",)),
+    ("foo",         FooData,        "baz",          BazData,    True,   ("foo->baz",)),
+    ("foo",         FooData,        "baz/bob",      BazBobData, True,   ("foo->baz","baz->baz/bob")),
+    ("foo/bar",     FooBarData,     "baz/bob",      BazBobData, True,   ("foo->baz","baz->baz/bob")),
+    ("baz/bob",     BazBobData,     "baz/bob",      BazBobData, True,   ("baz/bob->baz/bob",)),
+    ("foo",         FooData,        "error",        None,       True,   False),
+    ("no",          None,           "conversion",   None,       False,  False),
 )
 
-for f, t, exist, expected in TEST_CONVERSIONS:
+for f, fKlass, t, tKlass, exist, expected in TEST_CONVERSIONS:
     ok("Conv %s -> %s exists (%s)" % (f,t,exist), tc.conversion_exists(f,t) == exist)
     if expected == False:
         try:
@@ -73,16 +94,16 @@
             ok("Correct conversion: %s -> %s (v. %s -> %s)" % (cf,ct,ef,et), cf == ef and ct == et)
             i += 1
         
-        data = FooData()
+        data = fKlass()
         newdata = tc.convert(f,t,data)
-        ok("Data converted ok (no args)", data == newdata)
+        ok("Data converted ok (no args)", True)
 
         #check conversion args are handled
         args = {"arg1":Utils.random_string(),"arg2":Utils.random_string()}
         conversions = tc._get_conversions(f,"%s?%s" % (t,Utils.encode_conversion_args(args)))
         ok("Conversion args passed to last converter", conversions[-1][-1] == args)            
         newdata = tc.convert(f,"%s?%s"%(t,Utils.encode_conversion_args(args)),data)
-        ok("Data converted ok (with args)", data == newdata)
+        ok("Data converted ok (with args)", data._name_ == fKlass._name_ and newdata._name_ == tKlass._name_)
 
 finished()
 



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