home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / serpentine / urlutil.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  4.1 KB  |  102 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. import urllib
  5. from os import path
  6. from urlparse import urlparse, urlunparse
  7.  
  8. class _PropertyGen(object):
  9.     """This is a simple descriptor that uses 'data' array to retrieve its data"""
  10.     
  11.     def __init__(self, index):
  12.         self.index = index
  13.  
  14.     
  15.     def __get__(self, obj, type = None):
  16.         return obj.data[self.index]
  17.  
  18.     
  19.     def __set__(self, obj, value):
  20.         obj.data[self.index] = value
  21.  
  22.  
  23.  
  24. class UrlParse(object):
  25.     '''UrlParse objects represent a wrapper above urlparse.urlparse with some
  26.     improvements:
  27.        - access the parsed fields as the object field, instead of indexes of
  28.           a tuple
  29.           
  30.        - filename path are converted to the appropriate URL path, with "file"
  31.           scheme. A filename path is one with no associated "scheme" and it is
  32.           unquoted.
  33.        - allows field alterations(after calling the `make_writable` method)
  34.        
  35.        - \'path\' field is unquoted, whereas the \'quoted_path\' is the real one.
  36.     '''
  37.     data = None
  38.     
  39.     def __init__(self, data, basepath = None):
  40.         if data is not None:
  41.             self.parse(data, basepath)
  42.         
  43.  
  44.     
  45.     def parse(self, data, basepath = None):
  46.         data = data.encode('utf-8')
  47.         self.data = urlparse(data)
  48.         if self.scheme == '':
  49.             self.make_writable()
  50.             if basepath is None:
  51.                 unqpath = path.abspath(self.quoted_path)
  52.             else:
  53.                 unqpath = path.join(basepath, self.quoted_path)
  54.             self.quoted_path = urllib.quote(unqpath)
  55.             self.scheme = 'file'
  56.         
  57.  
  58.     scheme = _PropertyGen(0)
  59.     netloc = _PropertyGen(1)
  60.     quoted_path = _PropertyGen(2)
  61.     params = _PropertyGen(3)
  62.     query = _PropertyGen(4)
  63.     fragment = _PropertyGen(5)
  64.     is_local = property((lambda self: self.scheme == 'file'))
  65.     is_writable = property((lambda self: isinstance(self.data, list)))
  66.     
  67.     def get_path(self):
  68.         return urllib.unquote(self.quoted_path)
  69.  
  70.     
  71.     def set_path(self, value):
  72.         self.quoted_path = urllib.quote(value)
  73.  
  74.     path = property(get_path, set_path)
  75.     
  76.     unparse = lambda self: urlunparse(self.data)
  77.     
  78.     def make_writable(self):
  79.         self.data = list(self.data)
  80.  
  81.  
  82.  
  83. def get_path(uri_or_path, basepath = None):
  84.     '''Returns a path from a path or from a URI'''
  85.     return UrlParse(uri_or_path, basepath).path
  86.  
  87.  
  88. def normalize(uri_or_path, basepath = None):
  89.     '''Converts a path or a URI to a URI'''
  90.     return UrlParse(uri_or_path, basepath).unparse()
  91.  
  92.  
  93. def is_local(uri_or_path, basepath = None):
  94.     '''Checks if a path(paths are always local) or a URI is local(when it
  95.     contains the file scheme)'''
  96.     return UrlParse(uri_or_path, basepath).is_local
  97.  
  98.  
  99. def basename(uri_or_path, basepath = None):
  100.     return path.basename(UrlParse(uri_or_path, basepath).path)
  101.  
  102.