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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import re
  6. from types import StringType
  7. from types import UnicodeType
  8. STRING_TYPES = (StringType, UnicodeType)
  9. from _util import http2time
  10. import _rfc3986
  11.  
  12. def is_html(ct_headers, url, allow_xhtml = False):
  13.     if not ct_headers:
  14.         ext = os.path.splitext(_rfc3986.urlsplit(url)[2])[1]
  15.         html_exts = [
  16.             '.htm',
  17.             '.html']
  18.         if allow_xhtml:
  19.             html_exts += [
  20.                 '.xhtml']
  21.         
  22.         return ext in html_exts
  23.     ct = split_header_words(ct_headers)[0][0][0]
  24.     html_types = [
  25.         'text/html']
  26.     if allow_xhtml:
  27.         html_types += [
  28.             'text/xhtml',
  29.             'text/xml',
  30.             'application/xml',
  31.             'application/xhtml+xml']
  32.     
  33.     return ct in html_types
  34.  
  35.  
  36. def unmatched(match):
  37.     (start, end) = match.span(0)
  38.     return match.string[:start] + match.string[end:]
  39.  
  40. token_re = re.compile('^\\s*([^=\\s;,]+)')
  41. quoted_value_re = re.compile('^\\s*=\\s*\\"([^\\"\\\\]*(?:\\\\.[^\\"\\\\]*)*)\\"')
  42. value_re = re.compile('^\\s*=\\s*([^\\s;,]*)')
  43. escape_re = re.compile('\\\\(.)')
  44.  
  45. def split_header_words(header_values):
  46.     result = []
  47.     for text in header_values:
  48.         orig_text = text
  49.         pairs = []
  50.         while text:
  51.             m = token_re.search(text)
  52.             if m:
  53.                 text = unmatched(m)
  54.                 name = m.group(1)
  55.                 m = quoted_value_re.search(text)
  56.                 if m:
  57.                     text = unmatched(m)
  58.                     value = m.group(1)
  59.                     value = escape_re.sub('\\1', value)
  60.                 else:
  61.                     m = value_re.search(text)
  62.                     if m:
  63.                         text = unmatched(m)
  64.                         value = m.group(1)
  65.                         value = value.rstrip()
  66.                     else:
  67.                         value = None
  68.                 pairs.append((name, value))
  69.                 continue
  70.             if text.lstrip().startswith(','):
  71.                 text = text.lstrip()[1:]
  72.                 if pairs:
  73.                     result.append(pairs)
  74.                 
  75.                 pairs = []
  76.                 continue
  77.             (non_junk, nr_junk_chars) = re.subn('^[=\\s;]*', '', text)
  78.             text = non_junk
  79.         if pairs:
  80.             result.append(pairs)
  81.             continue
  82.     
  83.     return result
  84.  
  85. join_escape_re = re.compile('([\\"\\\\])')
  86.  
  87. def join_header_words(lists):
  88.     headers = []
  89.     for pairs in lists:
  90.         attr = []
  91.         for k, v in pairs:
  92.             if v is not None:
  93.                 if not re.search('^\\w+$', v):
  94.                     v = join_escape_re.sub('\\\\\\1', v)
  95.                     v = '"%s"' % v
  96.                 
  97.                 if k is None:
  98.                     k = v
  99.                 else:
  100.                     k = '%s=%s' % (k, v)
  101.             
  102.             attr.append(k)
  103.         
  104.         if attr:
  105.             headers.append('; '.join(attr))
  106.             continue
  107.     
  108.     return ', '.join(headers)
  109.  
  110.  
  111. def strip_quotes(text):
  112.     if text.startswith('"'):
  113.         text = text[1:]
  114.     
  115.     if text.endswith('"'):
  116.         text = text[:-1]
  117.     
  118.     return text
  119.  
  120.  
  121. def parse_ns_headers(ns_headers):
  122.     known_attrs = ('expires', 'domain', 'path', 'secure', 'version', 'port', 'max-age')
  123.     result = []
  124.     for ns_header in ns_headers:
  125.         pairs = []
  126.         version_set = False
  127.         params = re.split(';\\s*', ns_header)
  128.         for ii in range(len(params)):
  129.             param = params[ii]
  130.             param = param.rstrip()
  131.             if param == '':
  132.                 continue
  133.             
  134.             if '=' not in param:
  135.                 k = param
  136.                 v = None
  137.             else:
  138.                 (k, v) = re.split('\\s*=\\s*', param, 1)
  139.                 k = k.lstrip()
  140.             if ii != 0:
  141.                 lc = k.lower()
  142.                 if lc in known_attrs:
  143.                     k = lc
  144.                 
  145.                 if k == 'version':
  146.                     v = strip_quotes(v)
  147.                     version_set = True
  148.                 
  149.                 if k == 'expires':
  150.                     v = http2time(strip_quotes(v))
  151.                 
  152.             
  153.             pairs.append((k, v))
  154.         
  155.         if pairs:
  156.             if not version_set:
  157.                 pairs.append(('version', '0'))
  158.             
  159.             result.append(pairs)
  160.             continue
  161.     
  162.     return result
  163.  
  164.  
  165. def _test():
  166.     import doctest
  167.     import _headersutil
  168.     return doctest.testmod(_headersutil)
  169.  
  170. if __name__ == '__main__':
  171.     _test()
  172.  
  173.