conduit r1174 - in trunk: . conduit



Author: jstowers
Date: Wed Jan  9 23:07:18 2008
New Revision: 1174
URL: http://svn.gnome.org/viewvc/conduit?rev=1174&view=rev

Log:
Add functions to strip illegal filesystem characters. Borrowed from soundjuicer #321436

Modified:
   trunk/ChangeLog
   trunk/conduit/Utils.py
   trunk/conduit/Vfs.py

Modified: trunk/conduit/Utils.py
==============================================================================
--- trunk/conduit/Utils.py	(original)
+++ trunk/conduit/Utils.py	Wed Jan  9 23:07:18 2008
@@ -97,13 +97,27 @@
     protocol = uri[:uri.index("://")+3]
     return protocol.lower()
 
-def uri_get_filename(path):
+def uri_get_filename(uri):
     """
     Method to return the filename of a file. Could use GnomeVFS for this
     is it wasnt so slow
     """
-    return path.split(os.sep)[-1]
-
+    return uri.split(os.sep)[-1]
+    
+def uri_sanitize_for_filesystem(uri, filesystem=None):
+    """
+    Removes illegal characters in uri that cannot be stored on the 
+    given filesystem - particuarly fat and ntfs types
+    """
+    import string
+    if filesystem in ("vfat","ntfs"):
+        ILLEGAL_CHARS = "\\:*?\"<>|"
+        #replace illegal chars with a space
+        return uri.translate(string.maketrans(
+                                ILLEGAL_CHARS,
+                                " "*len(ILLEGAL_CHARS)))
+    return uri
+    
 #
 # Temporary file functions
 #

Modified: trunk/conduit/Vfs.py
==============================================================================
--- trunk/conduit/Vfs.py	(original)
+++ trunk/conduit/Vfs.py	Wed Jan  9 23:07:18 2008
@@ -1,14 +1,22 @@
+import logging
+log = logging.getLogger("Vfs")
+
 try:
     import gnomevfs
 except ImportError:
     from gnome import gnomevfs
 
-# to add: STARTEXECUTING, STOPEXECUTING, METADATA_CHANGED  
-MONITOR_EVENT_CREATED = gnomevfs.MONITOR_EVENT_CREATED
-MONITOR_EVENT_CHANGED = gnomevfs.MONITOR_EVENT_CHANGED
-MONITOR_EVENT_DELETED = gnomevfs.MONITOR_EVENT_DELETED
+MONITOR_EVENT_CREATED =             gnomevfs.MONITOR_EVENT_CREATED
+MONITOR_EVENT_CHANGED =             gnomevfs.MONITOR_EVENT_CHANGED
+MONITOR_EVENT_DELETED =             gnomevfs.MONITOR_EVENT_DELETED
+MONITOR_EVENT_METADATA_CHANGED =    gnomevfs.MONITOR_EVENT_METADATA_CHANGED
+MONITOR_EVENT_STARTEXECUTING =      gnomevfs.MONITOR_EVENT_STARTEXECUTING
+MONITOR_EVENT_STOPEXECUTING =       gnomevfs.MONITOR_EVENT_STOPEXECUTING
+MONITOR_FILE =                      gnomevfs.MONITOR_FILE
+MONITOR_DIRECTORY =                 gnomevfs.MONITOR_DIRECTORY
 
-MONITOR_DIRECTORY = gnomevfs.MONITOR_DIRECTORY
+class VolumeMonitor(gnomevfs.VolumeMonitor):
+    pass
 
 def monitor_add(folder, type, monitor_cb):
     try:
@@ -20,8 +28,26 @@
 def monitor_cancel(monitor_id):
     gnomevfs.monitor_cancel(monitor_id)
 
-class VolumeMonitor(gnomevfs.VolumeMonitor):
-    pass
+def get_filesystem_type(uri):
+    """
+    @returns: The filesystem that uri is stored on or None if it cannot
+    be determined
+    """
+    scheme = gnomevfs.get_uri_scheme(uri)
+    if scheme == "file":
+        try:
+            path = gnomevfs.get_local_path_from_uri(uri)
+            volume = VolumeMonitor().get_volume_for_path(path)
+            return  volume.get_filesystem_type()
+        except RuntimeError:
+            log.warn("Could not get local path from URI")
+            return None
+        except AttributeError:
+            log.warn("Could not determine volume for path")
+            return None
+    return None
+
+
 
 
 



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