conduit r1420 - in trunk: . conduit conduit/dataproviders conduit/datatypes conduit/gtkui test/python-tests



Author: jstowers
Date: Sun Apr 13 01:23:53 2008
New Revision: 1420
URL: http://svn.gnome.org/viewvc/conduit?rev=1420&view=rev

Log:
2008-04-13  John Stowers  <john stowers gmail com>

	* conduit/gtkui/Canvas.py: Remove workarounds for goocanvas versions
	< 0.9.0.

	* conduit/Vfs.py:
	* conduit/datatypes/File.py: Move get_relative_uri into Vfs class.
	
	* conduit/dataproviders/File.py: Be more careful about joining uri components
	when put()ing files that originated from another group. Fixes #527438
	
	* test/python-tests/TestCoreVfs.py: Test new Vfs functions
	
	* test/python-tests/TestDataProviderFolder.py:
	* test/python-tests/TestSyncFolderFolder.py: Test all put() scenarios of the
	folder data provider.



Modified:
   trunk/ChangeLog
   trunk/conduit/Vfs.py
   trunk/conduit/dataproviders/File.py
   trunk/conduit/datatypes/File.py
   trunk/conduit/gtkui/Canvas.py
   trunk/test/python-tests/TestCoreVfs.py
   trunk/test/python-tests/TestDataProviderFolder.py
   trunk/test/python-tests/TestSyncFolderFolder.py

Modified: trunk/conduit/Vfs.py
==============================================================================
--- trunk/conduit/Vfs.py	(original)
+++ trunk/conduit/Vfs.py	Sun Apr 13 01:23:53 2008
@@ -10,12 +10,36 @@
 #
 # URI Functions
 #
-def uri_join(*args):
+def uri_is_valid(uri):
     """
-    Joins multiple uri components
+    (weakly) checks if a uri is valid by looking for a scheme seperator
     """
-    return os.path.join(*args)
-
+    return uri[0] != "/" and uri.find("://") != -1
+    
+def uri_join(first, *rest):
+    """
+    Joins multiple uri components. Performs safely if the first
+    argument contains a uri scheme
+    """
+    return os.path.join(first,*rest)
+    #idx = first.rfind("://")
+    #if idx == -1:
+    #    start = 0
+    #else:
+    #   start = idx + 3
+    #return first[0:start]+os.path.join(first[start:],*rest)
+    
+def uri_get_relative(fromURI, toURI):
+    """
+    Returns the relative path fromURI --> toURI
+    """
+    rel = toURI.replace(fromURI,"")
+    #strip leading /
+    if rel[0] == os.sep:
+        return rel[1:]
+    else:
+        return rel
+    
 def uri_open(uri):
     """
     Opens a gnomevfs or xdg compatible uri.
@@ -23,8 +47,7 @@
     if uri == None:
         log.warn("Cannot open non-existant URI")
 
-    #FIXME: Use xdg-open?
-    APP = "gnome-open"
+    APP = "xdg-open"
     os.spawnlp(os.P_NOWAIT, APP, APP, uri)
     
 def uri_to_local_path(uri):
@@ -143,14 +166,14 @@
     illegal = ILLEGAL_CHARS.get(filesystem,None)
     if illegal:
         #dont escape the scheme part
-        scheme = uri_get_protocol(uri)
-        if scheme:
-            start = uri.index("://")+3
+        idx = uri.rfind("://")
+        if idx == -1:
+            start = 0
         else:
-            start = 0    
-        
-        #replace illegal chars with a space
-        return scheme+uri[start:].translate(string.maketrans(
+            start = idx + 3        
+
+        #replace illegal chars with a space, ignoring the scheme
+        return uri[0:start]+uri[start:].translate(string.maketrans(
                                                 illegal,
                                                 " "*len(illegal)
                                                 )

Modified: trunk/conduit/dataproviders/File.py
==============================================================================
--- trunk/conduit/dataproviders/File.py	(original)
+++ trunk/conduit/dataproviders/File.py	Sun Apr 13 01:23:53 2008
@@ -255,18 +255,19 @@
 
     def put(self, vfsFile, overwrite, LUID=None):
         """
-        Puts vfsFile at the correct location. There are two scenarios
+        Puts vfsFile at the correct location. There are three scenarios
         1) File came from a foreign DP like tomboy
         2) File came from another file dp
 
         Behaviour:
         1) The foreign DP should have encoded enough information (such as
         the filename) so that we can go ahead and put the file in the dir
-        2) First we see if the file has a group attribute. If so, and the
-        group matches the groupName here then we put the files into the 
-        directory. If not we put the file in the orphan dir. We try and 
-        retain the relative path for the files in the specifed group 
-        and recreate that in the group dir
+        2) First we see if the file has a group attribute.
+            a) If so, and the group matches the groupName here then we 
+            put the files into the directory.
+            b) If not we put the file in a subdir by the name of the group 
+            
+        We always retain the relative path for the files
         """
         DataProvider.TwoWay.put(self, vfsFile, overwrite, LUID)
         newURI = ""
@@ -276,19 +277,20 @@
             #came from another type of dataprovider such as tomboy
             #where relative path makes no sense. Could also come from
             #the FileSource dp when the user has selected a single file
-            log.debug("FolderTwoWay: No basepath. Going to empty dir")
-            newURI = self.folder+os.sep+vfsFile.get_filename()
+            log.debug("No basepath. Going to empty dir")
+            newURI = Vfs.uri_join(self.folder,vfsFile.get_filename())
         else:
             #Look for corresponding groups
             relpath = vfsFile.get_relative_uri()
+            log.debug("Relative path: %s" % relpath)
             if self.folderGroupName == vfsFile.group:
-                log.debug("FolderTwoWay: Found corresponding group")
+                log.debug("Found corresponding group")
                 #put in the folder
-                newURI = self.folder+relpath
+                newURI = Vfs.uri_join(self.folder,relpath)
             else:
-                log.debug("FolderTwoWay: Recreating group %s --- %s --- %s" % (vfsFile._get_text_uri(),vfsFile.basePath,vfsFile.group))
+                log.debug("Recreating group: %s" % vfsFile.group)
                 #unknown. Store in the dir but recreate the group
-                newURI = self.folder+os.sep+os.path.join(vfsFile.group+relpath)
+                newURI = Vfs.uri_join(self.folder,vfsFile.group,relpath)
 
         #escape illegal filesystem characters
         if self.fstype:

Modified: trunk/conduit/datatypes/File.py
==============================================================================
--- trunk/conduit/datatypes/File.py	(original)
+++ trunk/conduit/datatypes/File.py	Sun Apr 13 01:23:53 2008
@@ -423,7 +423,10 @@
         """
         @returns: The files URI relative to its basepath
         """
-        return self._get_text_uri().replace(self.basePath,"")
+        if self.basePath:
+            return Vfs.uri_get_relative(self.basePath,self._get_text_uri())
+        else:
+            return self._get_text_uri()
 
     def compare(self, B, sizeOnly=False):
         """

Modified: trunk/conduit/gtkui/Canvas.py
==============================================================================
--- trunk/conduit/gtkui/Canvas.py	(original)
+++ trunk/conduit/gtkui/Canvas.py	Sun Apr 13 01:23:53 2008
@@ -54,9 +54,6 @@
 LINE_WIDTH = 3.0
 RECTANGLE_RADIUS = 5.0
 
-#GRR support api break in pygoocanvas 0.6/8.0 -> 0.9.0
-NEW_GOOCANVAS_API = goocanvas.pygoocanvas_version >= (0,9,0)
-
 class Canvas(goocanvas.Canvas):
     """
     This class manages many objects
@@ -152,13 +149,9 @@
         return items
 
     def _canvas_resized(self, widget, allocation):
-        if NEW_GOOCANVAS_API:
-            self.set_bounds(0,0,allocation.width,allocation.height)
-            for i in self._get_child_conduit_canvas_items():
-                i.set_width(allocation.width)
-        else:
-            for i in self._get_child_conduit_canvas_items():
-                i.set_width(allocation.width)
+        self.set_bounds(0,0,allocation.width,allocation.height)
+        for i in self._get_child_conduit_canvas_items():
+            i.set_width(allocation.width)
 
 
     def _on_conduit_button_press(self, view, target, event):
@@ -508,51 +501,27 @@
         self.model = model
 
     def get_height(self):
-        if NEW_GOOCANVAS_API:
-            b = self.get_bounds()
-        else:
-            b = goocanvas.Bounds()
-            self.get_bounds(b)
+        b = self.get_bounds()
         return b.y2-b.y1
 
     def get_width(self):
-        if NEW_GOOCANVAS_API:
-            b = self.get_bounds()
-        else:
-            b = goocanvas.Bounds()
-            self.get_bounds(b)
+        b = self.get_bounds()
         return b.x2-b.x1
 
     def get_top(self):
-        if NEW_GOOCANVAS_API:
-            b = self.get_bounds()
-        else:
-            b = goocanvas.Bounds()
-            self.get_bounds(b)
+        b = self.get_bounds()
         return b.y1
 
     def get_bottom(self):
-        if NEW_GOOCANVAS_API:
-            b = self.get_bounds()
-        else:
-            b = goocanvas.Bounds()
-            self.get_bounds(b)
+        b = self.get_bounds()
         return b.y2
 
     def get_left(self):
-        if NEW_GOOCANVAS_API:
-            b = self.get_bounds()
-        else:
-            b = goocanvas.Bounds()
-            self.get_bounds(b)
+        b = self.get_bounds()
         return b.x1
 
     def get_right(self):
-        if NEW_GOOCANVAS_API:
-            b = self.get_bounds()
-        else:
-            b = goocanvas.Bounds()
-            self.get_bounds(b)
+        b = self.get_bounds()
         return b.x2
 
 class DataProviderCanvasItem(_CanvasItem):

Modified: trunk/test/python-tests/TestCoreVfs.py
==============================================================================
--- trunk/test/python-tests/TestCoreVfs.py	(original)
+++ trunk/test/python-tests/TestCoreVfs.py	Sun Apr 13 01:23:53 2008
@@ -54,4 +54,38 @@
 ok("Removable volume detected removable", Vfs.uri_is_on_removable_volume(removableUri))
 ok("Removable volume calculate root path", Vfs.uri_get_volume_root_uri(removableUri) == "file:///media/media")
 
+URIS_TO_JOIN = (
+    (   ("file:///foo/bar","gax","ssss"),   
+        "file:///foo/bar/gax/ssss"),
+    (   ("smb://192.168.1.1","Disk-2","Travel%20Videos/","Switzerland"),
+        "smb://192.168.1.1/Disk-2/Travel%20Videos/Switzerland"),
+    (   ("ssh://john open grcnz com/home","john","phd"),
+        "ssh://john open grcnz com/home/john/phd"),
+    (   ("foo","bar","baz"),
+        "foo/bar/baz")
+)
+
+for parts, result in URIS_TO_JOIN:
+    ok("Join uri: %s" % result, Vfs.uri_join(*parts) == result)
+    
+RELATIVE_URIS = (
+    #from                   #to                         #relativ    
+(   "file:///foo/bar",      "file:///baz/bob",          "file:///baz/bob"   ),
+(   "file:///foo/bar",      "file:///foo/bar/baz/bob",  "baz/bob"           ),
+(   "file:///foo/bar",      "file:///foo/bar/baz",      "baz"               ))
+for f,t,result in RELATIVE_URIS:
+    ok("Get relative uri: %s" % result, Vfs.uri_get_relative(f,t) == result)
+    
+VALID_URIS = (
+    #uri                                #valid
+(   "smb://192.168.1.1/foo/bar",        True                        ),
+(   "ftp://192.168.1.1/foo/bar";,        True                        ),
+(   "file:///foo/bar",                  True                        ),
+(   "file:/foo/bar",                    False                       ),
+(   "ftp:192.168.1.1",                  False                       ),
+(   "/foo/bar",                         False                       ))
+for uri,result in VALID_URIS:
+    desc = ("Invalid","Valid")[int(result)]
+    ok("%s uri: %s" % (desc,uri),Vfs.uri_is_valid(uri) == result)
+
 finished()

Modified: trunk/test/python-tests/TestDataProviderFolder.py
==============================================================================
--- trunk/test/python-tests/TestDataProviderFolder.py	(original)
+++ trunk/test/python-tests/TestDataProviderFolder.py	Sun Apr 13 01:23:53 2008
@@ -2,16 +2,102 @@
 from common import *
 
 import conduit.dataproviders.File as FileDataProvider
+import conduit.datatypes.File as File
+import conduit.utils as Utils
 import conduit.Vfs as Vfs
 
+GROUP_NAME = "Cheese"
+NESTED_DIR_NAME = "A Directory"
+DIFFERENT_GROUP_NAME = "Steak"
+
+def create_file(inDirURI):
+    name = Utils.random_string()+".txt"
+    uri = Vfs.uri_join(inDirURI.replace("file://",""),name)
+    f = open(uri,'w')
+    f.write(Utils.random_string())
+    f.close()
+    return name,uri
+
 #Test removable volume support
 removableUri = get_external_resources('folder')['removable-volume']
 ok("Is on a removable volume", FileDataProvider.is_on_removable_volume(removableUri))
 
 #save and restore a group
-groupName = "GroupName Is Lame"
-groupInfo = (removableUri, groupName)
+groupInfo = (removableUri, GROUP_NAME)
 ok("Save group info", FileDataProvider.save_removable_volume_group_file(*groupInfo))
 readInfo = FileDataProvider.read_removable_volume_group_file(removableUri)
-ok("Read group info (%s)" % str(readInfo), len(readInfo) > 0 and readInfo[0][1] == groupName)
+ok("Read group info (%s)" % str(readInfo), len(readInfo) > 0 and readInfo[0][1] == GROUP_NAME)
+
+#create some test directories
+dpdir = "file://"+Utils.new_tempdir()
+tempdir = "file://"+Utils.new_tempdir()
+tempdir2 = Vfs.uri_join(tempdir, NESTED_DIR_NAME)
+Vfs.uri_make_directory(tempdir2)
+#create some test files
+f1Name, f1URI = create_file(tempdir)
+f2Name, f2URI = create_file(tempdir2)
+#create a test dataprovider
+dp = FileDataProvider.FolderTwoWay(
+            folder=dpdir,
+            folderGroupName=GROUP_NAME,
+            includeHidden=False,
+            compareIgnoreMtime=False)
+
+# Scenario 1)
+#   File came from a foreign DP like tomboy. No concept of relative path
+#   or group. Goes into the folder and keeps its name
+plainFile = Utils.new_tempfile("TomboyNote")
+plainFileName = plainFile.get_filename()
+rid = dp.put(
+        vfsFile=plainFile,
+        overwrite=False,
+        LUID=None)
+ok("Put plain file", rid.get_UID() == Vfs.uri_join(dpdir,plainFileName))
+
+# Scehario 2a)
+#   File came from another folder dp with the same group
+#   Goes into the folder, keeps its relative path
+f1 = File.File(
+        URI=f1URI,
+        basepath=tempdir,
+        group=GROUP_NAME)
+rid = dp.put(
+        vfsFile=f1,
+        overwrite=False,
+        LUID=None)
+ok("Put same group file", rid.get_UID() == Vfs.uri_join(dpdir,f1Name))
+
+f2 = File.File(
+        URI=f2URI,
+        basepath=tempdir,
+        group=GROUP_NAME)
+rid = dp.put(
+        vfsFile=f2,
+        overwrite=False,
+        LUID=None)
+ok("Put same group file in nested dir", rid.get_UID() == Vfs.uri_join(dpdir,NESTED_DIR_NAME,f2Name))
+
+# Scehario 2b)
+#   File came from another folder dp with a different group
+#   Goes into a new folder, by the name of the group, keeps its relative path
+f1 = File.File(
+        URI=f1URI,
+        basepath=tempdir,
+        group=DIFFERENT_GROUP_NAME)
+rid = dp.put(
+        vfsFile=f1,
+        overwrite=False,
+        LUID=None)
+ok("Put different group file", rid.get_UID() == Vfs.uri_join(dpdir,DIFFERENT_GROUP_NAME,f1Name))
+
+f2 = File.File(
+        URI=f2URI,
+        basepath=tempdir,
+        group=DIFFERENT_GROUP_NAME)
+rid = dp.put(
+        vfsFile=f2,
+        overwrite=False,
+        LUID=None)
+ok("Put different group file in nested dir", rid.get_UID() == Vfs.uri_join(dpdir,DIFFERENT_GROUP_NAME,NESTED_DIR_NAME,f2Name))
+
 finished()

Modified: trunk/test/python-tests/TestSyncFolderFolder.py
==============================================================================
--- trunk/test/python-tests/TestSyncFolderFolder.py	(original)
+++ trunk/test/python-tests/TestSyncFolderFolder.py	Sun Apr 13 01:23:53 2008
@@ -16,7 +16,7 @@
 #Sleep time for file I/O
 SLEEP_TIME = 1
 #Print the mapping DB on the last sync?
-PRINT_MAPPING_DB = True
+PRINT_MAPPING_DB = False
 
 #setup test
 test = SimpleSyncTest()
@@ -78,7 +78,6 @@
 
     mapSource2Sink = conduit.GLOBALS.mappingDB.get_mappings_for_dataproviders(sourceW.get_UID(), sinkW.get_UID())
     mapSink2Source = conduit.GLOBALS.mappingDB.get_mappings_for_dataproviders(sinkW.get_UID(), sourceW.get_UID())
-    print mapSource2Sink, mapSink2Source
     ok("Oneway Sync: 5 Mappings source -> sink", len(mapSource2Sink) == 5 and len(mapSink2Source) == 0)
 
 #two way sync



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