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.py < prev    next >
Encoding:
Python Source  |  2006-08-23  |  3.8 KB  |  116 lines

  1. # LGPL License
  2. #
  3. # Copyright(C) 2005 Tiago Cogumbreiro <cogumbreiro@users.sf.net>
  4. #
  5. # This library is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Library General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 2 of the License, or(at your option) any later version.
  9. #
  10. # This library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. # Library General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Library General Public
  16. # License along with this library; if not, write to the
  17. # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18. # Boston, MA 02111-1307, USA.
  19. #
  20. # Authors: Tiago Cogumbreiro <cogumbreiro@users.sf.net>
  21.  
  22. import urllib
  23.  
  24. from os import path
  25. from urlparse import urlparse, urlunparse
  26.  
  27. class _PropertyGen(object):
  28.     """This is a simple descriptor that uses 'data' array to retrieve its data"""
  29.     def __init__(self, index):
  30.         self.index = index
  31.     
  32.     def __get__(self, obj, type = None):
  33.         return obj.data[self.index]
  34.     
  35.     def __set__(self, obj, value):
  36.         obj.data[self.index] = value
  37.     
  38.  
  39. class UrlParse(object):
  40.     """UrlParse objects represent a wrapper above urlparse.urlparse with some
  41.     improvements:
  42.        - access the parsed fields as the object field, instead of indexes of
  43.           a tuple
  44.           
  45.        - filename path are converted to the appropriate URL path, with "file"
  46.           scheme. A filename path is one with no associated "scheme" and it is
  47.           unquoted.
  48.        - allows field alterations(after calling the `make_writable` method)
  49.        
  50.        - 'path' field is unquoted, whereas the 'quoted_path' is the real one.
  51.     """
  52.     
  53.     data = None
  54.     
  55.     def __init__(self, data, basepath=None):
  56.         if data is not None:
  57.             self.parse(data, basepath)
  58.     
  59.     def parse(self, data, basepath=None):
  60.         data = data.encode("utf-8")
  61.         self.data = urlparse(data)
  62.         # if the scheme is empty then we're locating a local file
  63.         if self.scheme == "":
  64.             # quoted path is actually not quoted
  65.             self.make_writable()
  66.             if basepath is None:
  67.                 unqpath = path.abspath(self.quoted_path)
  68.             else:
  69.                 unqpath = path.join(basepath, self.quoted_path)
  70.                 
  71.             self.quoted_path = urllib.quote(unqpath)
  72.             self.scheme = "file"
  73.     
  74.     scheme      = _PropertyGen(0)
  75.     netloc      = _PropertyGen(1)
  76.     quoted_path = _PropertyGen(2)
  77.     params      = _PropertyGen(3)
  78.     query       = _PropertyGen(4)
  79.     fragment    = _PropertyGen(5)
  80.     
  81.     is_local = property(lambda self: self.scheme == "file")
  82.     is_writable = property(lambda self: isinstance(self.data, list))
  83.     
  84.     def get_path(self):
  85.         return urllib.unquote(self.quoted_path)
  86.     
  87.     def set_path(self, value):
  88.         self.quoted_path = urllib.quote(value)
  89.  
  90.     path = property(get_path, set_path)
  91.     
  92.     # Methods
  93.     
  94.     unparse = lambda self: urlunparse(self.data)
  95.     
  96.     def make_writable(self):
  97.         self.data = list(self.data)
  98.     
  99.     
  100. def get_path(uri_or_path, basepath=None):
  101.     """Returns a path from a path or from a URI"""
  102.     return UrlParse(uri_or_path, basepath).path
  103.  
  104. def normalize(uri_or_path, basepath=None):
  105.     """Converts a path or a URI to a URI"""
  106.     return UrlParse(uri_or_path, basepath).unparse()
  107.  
  108. def is_local(uri_or_path, basepath=None):
  109.     """Checks if a path(paths are always local) or a URI is local(when it
  110.     contains the file scheme)"""
  111.     return UrlParse(uri_or_path, basepath).is_local
  112.  
  113. def basename(uri_or_path, basepath=None):
  114.     return path.basename(UrlParse(uri_or_path, basepath).path)
  115.     
  116.