home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2073 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  3.9 KB  |  155 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import re
  5. import urllib
  6. BAD_URI_CHARS_RE = re.compile("[^A-Za-z0-9\\-_.~!*'();:@&=+$,/?%#[\\]]")
  7.  
  8. def clean_url(url, encoding):
  9.     if type(url) == type(''):
  10.         url = url.decode(encoding, 'replace')
  11.     
  12.     url = url.strip()
  13.     return urllib.quote(url.encode(encoding), "!*'();:@&=+$,/?%#[]~")
  14.  
  15.  
  16. def is_clean_uri(uri):
  17.     return not bool(BAD_URI_CHARS_RE.search(uri))
  18.  
  19. SPLIT_MATCH = re.compile('^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?').match
  20.  
  21. def urlsplit(absolute_uri):
  22.     match = SPLIT_MATCH(absolute_uri)
  23.     if match:
  24.         g = match.groups()
  25.         return (g[1], g[3], g[4], g[6], g[8])
  26.  
  27.  
  28. def urlunsplit(parts):
  29.     (scheme, authority, path, query, fragment) = parts
  30.     r = []
  31.     append = r.append
  32.     if scheme is not None:
  33.         append(scheme)
  34.         append(':')
  35.     
  36.     if authority is not None:
  37.         append('//')
  38.         append(authority)
  39.     
  40.     append(path)
  41.     if query is not None:
  42.         append('?')
  43.         append(query)
  44.     
  45.     if fragment is not None:
  46.         append('#')
  47.         append(fragment)
  48.     
  49.     return ''.join(r)
  50.  
  51.  
  52. def urljoin(base_uri, uri_reference):
  53.     return urlunsplit(urljoin_parts(urlsplit(base_uri), urlsplit(uri_reference)))
  54.  
  55.  
  56. def urljoin_parts(base_parts, reference_parts):
  57.     (scheme, authority, path, query, fragment) = base_parts
  58.     (rscheme, rauthority, rpath, rquery, rfragment) = reference_parts
  59.     if rscheme == scheme:
  60.         rscheme = None
  61.     
  62.     if rscheme is not None:
  63.         (tscheme, tauthority, tpath, tquery) = (rscheme, rauthority, remove_dot_segments(rpath), rquery)
  64.     elif rauthority is not None:
  65.         tauthority = rauthority
  66.         tpath = remove_dot_segments(rpath)
  67.         tquery = rquery
  68.     elif rpath == '':
  69.         tpath = path
  70.         if rquery is not None:
  71.             tquery = rquery
  72.         else:
  73.             tquery = query
  74.     elif rpath.startswith('/'):
  75.         tpath = remove_dot_segments(rpath)
  76.     else:
  77.         tpath = merge(authority, path, rpath)
  78.         tpath = remove_dot_segments(tpath)
  79.     tquery = rquery
  80.     tauthority = authority
  81.     tscheme = scheme
  82.     tfragment = rfragment
  83.     return (tscheme, tauthority, tpath, tquery, tfragment)
  84.  
  85.  
  86. def remove_dot_segments(path):
  87.     r = []
  88.     while path:
  89.         if path.startswith('../'):
  90.             path = path[3:]
  91.             continue
  92.         
  93.         if path.startswith('./'):
  94.             path = path[2:]
  95.             continue
  96.         
  97.         if path.startswith('/./'):
  98.             path = path[2:]
  99.             continue
  100.         
  101.         if path == '/.':
  102.             path = '/'
  103.             continue
  104.         
  105.         if path.startswith('/../'):
  106.             path = path[3:]
  107.             if r:
  108.                 r.pop()
  109.                 continue
  110.             continue
  111.         
  112.         if path == '/..':
  113.             path = '/'
  114.             if r:
  115.                 r.pop()
  116.                 continue
  117.             continue
  118.         
  119.         if path == '.':
  120.             path = path[1:]
  121.             continue
  122.         
  123.         if path == '..':
  124.             path = path[2:]
  125.             continue
  126.         
  127.         start = 0
  128.         if path.startswith('/'):
  129.             start = 1
  130.         
  131.         ii = path.find('/', start)
  132.         if ii < 0:
  133.             ii = None
  134.         
  135.         r.append(path[:ii])
  136.         if ii is None:
  137.             break
  138.         
  139.         path = path[ii:]
  140.     return ''.join(r)
  141.  
  142.  
  143. def merge(base_authority, base_path, ref_path):
  144.     if base_path == '':
  145.         return '/' + ref_path
  146.     ii = base_path.rfind('/')
  147.     if ii >= 0:
  148.         return base_path[:ii + 1] + ref_path
  149.     return ref_path
  150.  
  151. if __name__ == '__main__':
  152.     import doctest
  153.     doctest.testmod()
  154.  
  155.