conduit r1240 - in trunk: . conduit conduit/modules test/python-tests



Author: jstowers
Date: Thu Jan 17 10:31:01 2008
New Revision: 1240
URL: http://svn.gnome.org/viewvc/conduit?rev=1240&view=rev

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

	* conduit/TypeConverter.py: Always use the most specific converter if
	available
	
	* conduit/modules/TomboyModule.py:
	* test/python-tests/TestDataProviderTomboy.py:
	* test/python-tests/TestSyncTomboyFolder.py: Use a TomboyNote datatype
	and implement converters. Update the tests to use this API



Modified:
   trunk/ChangeLog
   trunk/conduit/TypeConverter.py
   trunk/conduit/modules/TomboyModule.py
   trunk/test/python-tests/TestDataProviderTomboy.py
   trunk/test/python-tests/TestSyncTomboyFolder.py

Modified: trunk/conduit/TypeConverter.py
==============================================================================
--- trunk/conduit/TypeConverter.py	(original)
+++ trunk/conduit/TypeConverter.py	Thu Jan 17 10:31:01 2008
@@ -121,24 +121,28 @@
         except ValueError: pass            
 
         if fromType != toType:
-            froms = fromType.split("/")
-            tos = toType.split("/")
-            if froms[0] == tos[0]:
-                #same base type, so only convert parent -> child e.g.
-                #file/audio -> file = file -> file
-                #file -> file/audio = file -> file/audio
-                conversions.append( (froms[0],"/".join(tos),args) )
+            #check first for and explicit conversion
+            if self._conversion_exists(fromType, toType):
+                conversions.append( (fromType, toType, args) )
             else:
-                #different base type, e.g.
-                #foo/bar -> bar/baz
-                if len(tos) > 1:
-                    #Two conversions are needed, a main type, and a subtype
-                    #conversion. remains is any necessary subtype conversion
-                    conversions.append( (froms[0], tos[0], {}) )
-                    conversions.append( (tos[0],"/".join(tos),args) )
+                froms = fromType.split("/")
+                tos = toType.split("/")
+                if froms[0] == tos[0]:
+                    #same base type, so only convert parent -> child e.g.
+                    #file/audio -> file = file -> file
+                    #file -> file/audio = file -> file/audio
+                    conversions.append( (froms[0],"/".join(tos),args) )
                 else:
-                    #Just a main type conversion is needed (remember the args)
-                    conversions.append( (froms[0], tos[0], args) )
+                    #different base type, e.g.
+                    #foo/bar -> bar/baz
+                    if len(tos) > 1:
+                        #Two conversions are needed, a main type, and a subtype
+                        #conversion. remains is any necessary subtype conversion
+                        conversions.append( (froms[0], tos[0], {}) )
+                        conversions.append( (tos[0],"/".join(tos),args) )
+                    else:
+                        #Just a main type conversion is needed (remember the args)
+                        conversions.append( (froms[0], tos[0], args) )
         else:
             conversions.append( (fromType, toType, args) )
 

Modified: trunk/conduit/modules/TomboyModule.py
==============================================================================
--- trunk/conduit/modules/TomboyModule.py	(original)
+++ trunk/conduit/modules/TomboyModule.py	Thu Jan 17 10:31:01 2008
@@ -8,6 +8,7 @@
 import conduit.dataproviders.AutoSync as AutoSync
 import conduit.Exceptions as Exceptions
 import conduit.datatypes.Note as Note
+import conduit.datatypes.File as File
 import conduit.Utils as Utils
 
 TOMBOY_DBUS_PATH = "/org/gnome/Tomboy/RemoteControl"
@@ -29,7 +30,7 @@
             return (txt[:txt.find(sep)], sep, txt[txt.find(sep)+len(sep):])
 
 class TomboyNote(Note.Note):
-    def __init__(self, title, xmlContent):
+    def __init__(self, xmlContent):
         self.xmlContent = xmlContent
         #strip the xml
         text = xmlContent.replace('<note-content version="0.1">','').replace('</note-content>','')
@@ -49,13 +50,33 @@
         Note.Note.__setstate__(self, data)
 
 class TomboyNoteConverter(object):
+    NOTE_EXTENSION = ".xml"
     def __init__(self):
         self.conversions =  {
-                "note,note/tomboy"  : self.convert_to_tomboy_note
+                "note,note/tomboy"  : self.note_to_tomboy_note,
+                "note/tomboy,file"  : self.tomboy_note_to_file,
+                "file,note/tomboy"  : self.file_to_tomboy_note,
         }
                             
-    def convert_to_tomboy_note(self, note, **kwargs):
-        xmlContent = '<note-content version="0.1">%s\n%s</note-content>' % (note.get_title(), note.get_contents())
+    def note_to_tomboy_note(self, note, **kwargs):
+        n = TomboyNote(
+                '<note-content version="0.1">%s\n%s</note-content>' % (note.get_title(), note.get_contents())
+                )
+        return n
+        
+    def tomboy_note_to_file(self, note, **kwargs):
+        f = File.TempFile(note.get_xml())
+        f.force_new_filename(note.get_title())
+        f.force_new_file_extension(TomboyNoteConverter.NOTE_EXTENSION)
+        return f
+        
+    def file_to_tomboy_note(self, f, **kwargs):        
+        note = None
+        title,ext = f.get_filename_and_extension()
+        if ext == TomboyNoteConverter.NOTE_EXTENSION:
+            note = TomboyNote(
+                        f.get_contents_as_text()
+                        )
         return note
 
 class TomboyNoteTwoWay(DataProvider.TwoWay, AutoSync.AutoSync):
@@ -96,8 +117,10 @@
 
     def _update_note(self, uid, note):
         log.debug("Updating note uid: %s" % uid)
-        xmlContent = '<note-content version="0.1">%s\n%s</note-content>' % (note.get_title(), note.get_contents())
-        ok = self.remoteTomboy.SetNoteContentsXml(uid, xmlContent)
+        ok = self.remoteTomboy.SetNoteContentsXml(
+                                    uid,
+                                    note.get_xml()
+                                    )
         if not ok:
             raise Exceptions.SyncronizeError("Error setting Tomboy note content (uri: %s)" % uid)
 
@@ -113,12 +136,8 @@
     def _get_note(self, uid):
         #Get the whole xml and strip out the tags
         log.debug("Getting note: %s" % uid)
-        xmlContent=str(self.remoteTomboy.GetNoteContentsXml(uid))
-        xmlContent=xmlContent.replace('<note-content version="0.1">','').replace('</note-content>','')
-        title, sep, contents = partition(xmlContent, "\n")
-        n = Note.Note(
-                title=title,
-                contents=contents
+        n = TomboyNote(
+                xmlContent=str(self.remoteTomboy.GetNoteContentsXml(uid))
                 )
         n.set_UID(str(uid))
         n.set_mtime(self._get_note_mtime(uid))

Modified: trunk/test/python-tests/TestDataProviderTomboy.py
==============================================================================
--- trunk/test/python-tests/TestDataProviderTomboy.py	(original)
+++ trunk/test/python-tests/TestDataProviderTomboy.py	Thu Jan 17 10:31:01 2008
@@ -31,54 +31,21 @@
 ok("Got note #%s" % idx, note != None)
 ok("Got note title (%s)" % note.title, len(note.title) > 0)
 ok("Got note contents", len(note.contents) > 0)
+ok("Got note xml", len(note.get_xml()) > 0)
 
 #make a new note
-newnote = Note.Note(
-                    title="Conduit-"+Utils.random_string(),
-                    mtime=datetime.datetime.today(),
-                    contents="Conduit Test Note"
-                    )
-try:
-    rid = tomboy.put(newnote,False)
-    uid = rid.get_UID()
-    ok("Put new note (%s)" % uid, uid != None)
-except Exception, err:
-    traceback.print_exc()
-    ok("Put new note: %s" % err, False)
-
-#modify the note and replace the old one
-teststr = Utils.random_string()
-newnote.contents += teststr
-try:
-    i = tomboy.put(newnote, True, uid)
-    ok("Overwrite the note", i.get_UID() == uid)
-except:
-    ok("Overwrite the note", False)
-
-#check the content was replace correctly
-try:
-    tomboy._get_note(uid).contents.index(teststr)
-    ok("Note was overwritten correctly", True)
-except:
-    traceback.print_exc()
-    ok("Note was overwritten correctly", False)
-
-#Try and overwrite the note with an older version. Check it conflicts
-olddate = datetime.datetime.fromtimestamp(0)
-newnote = tomboy._get_note(uid)
-newnote.set_mtime(olddate)
-try:
-    tomboy.put(newnote, False, uid)
-    ok("Oldnote conflicts with newnote", False)
-except Exceptions.SynchronizeConflictError, err:
-    comp = err.comparison
-    ok("Oldnote conflicts with newnote. Comparison: %s" % comp, comp == conduit.datatypes.COMPARISON_OLDER)
-
-#remove the note
-try:
-    tomboy.delete(uid)
-    ok("Deleted note (%s)" % uid, tomboy.remoteTomboy.NoteExists(uid) == False)
-except:
-    ok("Deleted note (%s)" % uid, False)
+note = Note.Note(
+                title="Conduit-"+Utils.random_string(),
+                contents="Conduit Test Note"
+                )
+tnote = test.type_converter.convert("note","note/tomboy",note)
+
+test.do_dataprovider_tests(
+            supportsGet=True,
+            supportsDelete=True,
+            safeLUID=None,
+            data=tnote,
+            name="tomboy note"
+            )
 
 finished()

Modified: trunk/test/python-tests/TestSyncTomboyFolder.py
==============================================================================
--- trunk/test/python-tests/TestSyncTomboyFolder.py	(original)
+++ trunk/test/python-tests/TestSyncTomboyFolder.py	Thu Jan 17 10:31:01 2008
@@ -2,54 +2,32 @@
 from common import *
 
 import conduit.datatypes.File as File
-
-import time
+import conduit.Utils as Utils
 
 test = SimpleSyncTest()
-test.set_two_way_policy({"conflict":"skip","deleted":"skip"})
+test.set_two_way_policy({"conflict":"ask","deleted":"ask"})
 
 #setup the conduit
 sourceW = test.get_dataprovider("TomboyNoteTwoWay")
 sinkW = test.get_dataprovider("FolderTwoWay")
 test.prepare(sourceW, sinkW)
 
-#prepare the test data
-folderDir = os.path.join(os.environ['TEST_DIRECTORY'],"folder")
-if not os.path.exists(folderDir):
-    os.mkdir(folderDir)
-
-tomboyFiles = get_files_from_data_dir("*.tomboy")
-for i in tomboyFiles:
-    f = File.File(i)
-    f.transfer(folderDir+"/"+f.get_filename(), True)
-
-time.sleep(1)
-
 #configure the source and sink
 config = {}
-config["folder"] = "file://"+folderDir
+config["folder"] = "file://"+Utils.new_tempdir()
 config["folderGroupName"] = "Tomboy"
 test.configure(sink=config)
 
 #check they refresh ok
 test.refresh()
-
 a = test.get_source_count()
-b = test.get_sink_count()
-ok("Got items to sync (%s,%s)" % (a,b), a > 0 and b == len(tomboyFiles))
+ok("Got notes to sync (%s)" % a, a > 0)
 
 #sync
 test.set_two_way_sync(True)
-test.sync()
-aborted = test.sync_aborted()
-ok("Sync completed", aborted == False)
-
-#Check the notes were all transferred
-test.refresh()
-a = test.get_source_count()
-b = test.get_sink_count()
+a,b = test.sync()
+abort,error,conflict = test.get_sync_result()
+ok("Sync completed", abort == False)
 ok("All notes transferred (%s,%s)" % (a,b), a == b)
 
-#test.print_mapping_db()
-
 finished()



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