Advertisement
alexs77

gio-to-path.py

Jul 29th, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import sys
  4.  
  5. def uri_to_path(uri):
  6.     '''Convert a file URI to a path'''
  7.     try:
  8.         # gio is easier to use
  9.         import gio
  10.         path = gio.File(uri).get_path()
  11.     except ImportError:
  12.         # if gio isn't available
  13.         import urllib
  14.         # get the path to file
  15.         path = ""
  16.         if uri.startswith('file:\\\\\\'): # windows
  17.         path = uri[8:] # 8 is len('file:///')
  18.         elif uri.startswith('file://'): # nautilus, rox
  19.             path = uri[7:] # 7 is len('file://')
  20.         elif uri.startswith('file:'): # xffm
  21.             path = uri[5:] # 5 is len('file:')
  22.  
  23.         path = urllib.url2pathname(path) # escape special chars
  24.         path = path.strip('\r\n\x00') # remove \r\n and NULL
  25.  
  26.     return path
  27.    
  28. def run():
  29.     if len(sys.argv) <= 1:
  30.         print 'no arguments passed'
  31.         sys.exit(1)
  32.     else:
  33. #        for uri in sys.argv[1:]:
  34.         for uri in sys.argv[1:]:
  35.             path = uri_to_path(uri)
  36.             #print uri + ": " + path
  37.             print path
  38.         sys.exit(0)
  39.    
  40. if __name__ == '__main__':
  41.     run()
  42.  
  43. # EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement