home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_2406 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  6.0 KB  |  164 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import re
  5. import sys
  6. import codecs
  7. split_path_re = re.compile('[/\\\\ ]')
  8. doctype_lookup_re = re.compile('(?smx)\n    (<\\?.*?\\?>)?\\s*\n    <!DOCTYPE\\s+(\n     [a-zA-Z_][a-zA-Z0-9]*\\s+\n     [a-zA-Z_][a-zA-Z0-9]*\\s+\n     "[^"]*")\n     [^>]*>\n')
  9. tag_re = re.compile('<(.+?)(\\s.*?)?>.*?</.+?>(?uism)')
  10.  
  11. class ClassNotFound(ValueError):
  12.     pass
  13.  
  14.  
  15. class OptionError(Exception):
  16.     pass
  17.  
  18.  
  19. def get_choice_opt(options, optname, allowed, default = None, normcase = False):
  20.     string = options.get(optname, default)
  21.     if normcase:
  22.         string = string.lower()
  23.     
  24.     if string not in allowed:
  25.         raise OptionError('Value for option %s must be one of %s' % (optname, ', '.join(map(str, allowed))))
  26.     string not in allowed
  27.     return string
  28.  
  29.  
  30. def get_bool_opt(options, optname, default = None):
  31.     string = options.get(optname, default)
  32.     if isinstance(string, bool):
  33.         return string
  34.     if isinstance(string, int):
  35.         return bool(string)
  36.     if not isinstance(string, basestring):
  37.         raise OptionError('Invalid type %r for option %s; use 1/0, yes/no, true/false, on/off' % (string, optname))
  38.     isinstance(string, basestring)
  39.     if string.lower() in ('1', 'yes', 'true', 'on'):
  40.         return True
  41.     if string.lower() in ('0', 'no', 'false', 'off'):
  42.         return False
  43.     raise OptionError('Invalid value %r for option %s; use 1/0, yes/no, true/false, on/off' % (string, optname))
  44.  
  45.  
  46. def get_int_opt(options, optname, default = None):
  47.     string = options.get(optname, default)
  48.     
  49.     try:
  50.         return int(string)
  51.     except TypeError:
  52.         raise OptionError('Invalid type %r for option %s; you must give an integer value' % (string, optname))
  53.     except ValueError:
  54.         raise OptionError('Invalid value %r for option %s; you must give an integer value' % (string, optname))
  55.  
  56.  
  57.  
  58. def get_list_opt(options, optname, default = None):
  59.     val = options.get(optname, default)
  60.     if isinstance(val, basestring):
  61.         return val.split()
  62.     if isinstance(val, (list, tuple)):
  63.         return list(val)
  64.     raise OptionError('Invalid type %r for option %s; you must give a list value' % (val, optname))
  65.  
  66.  
  67. def docstring_headline(obj):
  68.     if not obj.__doc__:
  69.         return ''
  70.     res = []
  71.     for line in obj.__doc__.strip().splitlines():
  72.         if line.strip():
  73.             res.append(' ' + line.strip())
  74.             continue
  75.         obj.__doc__
  76.     
  77.     return ''.join(res).lstrip()
  78.  
  79.  
  80. def make_analysator(f):
  81.     
  82.     def text_analyse(text):
  83.         rv = f(text)
  84.         if not rv:
  85.             return 0
  86.         return min(1, max(0, float(rv)))
  87.  
  88.     text_analyse.__doc__ = f.__doc__
  89.     return staticmethod(text_analyse)
  90.  
  91.  
  92. def shebang_matches(text, regex):
  93.     index = text.find('\n')
  94.     if index >= 0:
  95.         first_line = text[:index].lower()
  96.     else:
  97.         first_line = text.lower()
  98.     if first_line.startswith('#!'):
  99.         
  100.         try:
  101.             found = _[1][-1]
  102.         except IndexError:
  103.             return False
  104.  
  105.         regex = re.compile('^%s(\\.(exe|cmd|bat|bin))?$' % regex, re.IGNORECASE)
  106.         if regex.search(found) is not None:
  107.             return True
  108.     
  109.     return False
  110.  
  111.  
  112. def doctype_matches(text, regex):
  113.     m = doctype_lookup_re.match(text)
  114.     if m is None:
  115.         return False
  116.     doctype = m.group(2)
  117.     return re.compile(regex).match(doctype.strip()) is not None
  118.  
  119.  
  120. def html_doctype_matches(text):
  121.     return doctype_matches(text, 'html\\s+PUBLIC\\s+"-//W3C//DTD X?HTML.*')
  122.  
  123. _looks_like_xml_cache = { }
  124.  
  125. def looks_like_xml(text):
  126.     key = hash(text)
  127.     
  128.     try:
  129.         return _looks_like_xml_cache[key]
  130.     except KeyError:
  131.         m = doctype_lookup_re.match(text)
  132.         if m is not None:
  133.             return True
  134.         rv = tag_re.search(text[:1000]) is not None
  135.         _looks_like_xml_cache[key] = rv
  136.         return rv
  137.         m is not None
  138.  
  139.  
  140. if sys.version_info < (3, 0):
  141.     b = bytes = str
  142.     u_prefix = 'u'
  143.     import StringIO
  144.     import cStringIO
  145.     BytesIO = cStringIO.StringIO
  146.     StringIO = StringIO.StringIO
  147.     uni_open = codecs.open
  148. else:
  149.     import builtins
  150.     bytes = builtins.bytes
  151.     u_prefix = ''
  152.     
  153.     def b(s):
  154.         if isinstance(s, str):
  155.             return bytes(map(ord, s))
  156.         if isinstance(s, bytes):
  157.             return s
  158.         raise TypeError('Invalid argument %r for b()' % (s,))
  159.  
  160.     import io
  161.     BytesIO = io.BytesIO
  162.     StringIO = io.StringIO
  163.     uni_open = builtins.open
  164.