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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. import sre_compile
  6. import sre_parse
  7. __all__ = [
  8.     'match',
  9.     'search',
  10.     'sub',
  11.     'subn',
  12.     'split',
  13.     'findall',
  14.     'compile',
  15.     'purge',
  16.     'template',
  17.     'escape',
  18.     'I',
  19.     'L',
  20.     'M',
  21.     'S',
  22.     'X',
  23.     'U',
  24.     'IGNORECASE',
  25.     'LOCALE',
  26.     'MULTILINE',
  27.     'DOTALL',
  28.     'VERBOSE',
  29.     'UNICODE',
  30.     'error']
  31. __version__ = '2.2.1'
  32. I = IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE
  33. L = LOCALE = sre_compile.SRE_FLAG_LOCALE
  34. U = UNICODE = sre_compile.SRE_FLAG_UNICODE
  35. M = MULTILINE = sre_compile.SRE_FLAG_MULTILINE
  36. S = DOTALL = sre_compile.SRE_FLAG_DOTALL
  37. X = VERBOSE = sre_compile.SRE_FLAG_VERBOSE
  38. T = TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE
  39. DEBUG = sre_compile.SRE_FLAG_DEBUG
  40. error = sre_compile.error
  41.  
  42. def match(pattern, string, flags = 0):
  43.     return _compile(pattern, flags).match(string)
  44.  
  45.  
  46. def search(pattern, string, flags = 0):
  47.     return _compile(pattern, flags).search(string)
  48.  
  49.  
  50. def sub(pattern, repl, string, count = 0):
  51.     return _compile(pattern, 0).sub(repl, string, count)
  52.  
  53.  
  54. def subn(pattern, repl, string, count = 0):
  55.     return _compile(pattern, 0).subn(repl, string, count)
  56.  
  57.  
  58. def split(pattern, string, maxsplit = 0):
  59.     return _compile(pattern, 0).split(string, maxsplit)
  60.  
  61.  
  62. def findall(pattern, string, flags = 0):
  63.     return _compile(pattern, flags).findall(string)
  64.  
  65. if sys.hexversion >= 33685504:
  66.     __all__.append('finditer')
  67.     
  68.     def finditer(pattern, string, flags = 0):
  69.         return _compile(pattern, flags).finditer(string)
  70.  
  71.  
  72.  
  73. def compile(pattern, flags = 0):
  74.     return _compile(pattern, flags)
  75.  
  76.  
  77. def purge():
  78.     _cache.clear()
  79.     _cache_repl.clear()
  80.  
  81.  
  82. def template(pattern, flags = 0):
  83.     return _compile(pattern, flags | T)
  84.  
  85. _alphanum = { }
  86. for c in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890':
  87.     _alphanum[c] = 1
  88.  
  89. del c
  90.  
  91. def escape(pattern):
  92.     s = list(pattern)
  93.     alphanum = _alphanum
  94.     for i in range(len(pattern)):
  95.         c = pattern[i]
  96.         if c not in alphanum:
  97.             if c == '\x00':
  98.                 s[i] = '\\000'
  99.             else:
  100.                 s[i] = '\\' + c
  101.         c == '\x00'
  102.     
  103.     return pattern[:0].join(s)
  104.  
  105. _cache = { }
  106. _cache_repl = { }
  107. _pattern_type = type(sre_compile.compile('', 0))
  108. _MAXCACHE = 100
  109.  
  110. def _compile(*key):
  111.     cachekey = (type(key[0]),) + key
  112.     p = _cache.get(cachekey)
  113.     if p is not None:
  114.         return p
  115.     (pattern, flags) = key
  116.     if isinstance(pattern, _pattern_type):
  117.         if flags:
  118.             raise ValueError('Cannot process flags argument with a compiled pattern')
  119.         flags
  120.         return pattern
  121.     if not sre_compile.isstring(pattern):
  122.         raise TypeError, 'first argument must be string or compiled pattern'
  123.     sre_compile.isstring(pattern)
  124.     
  125.     try:
  126.         p = sre_compile.compile(pattern, flags)
  127.     except error:
  128.         isinstance(pattern, _pattern_type)
  129.         v = isinstance(pattern, _pattern_type)
  130.         p is not None
  131.         raise error, v
  132.     except:
  133.         isinstance(pattern, _pattern_type)
  134.  
  135.     _cache[cachekey] = p
  136.     return p
  137.  
  138.  
  139. def _compile_repl(*key):
  140.     p = _cache_repl.get(key)
  141.     if p is not None:
  142.         return p
  143.     (repl, pattern) = key
  144.     
  145.     try:
  146.         p = sre_parse.parse_template(repl, pattern)
  147.     except error:
  148.         p is not None
  149.         v = p is not None
  150.         raise error, v
  151.     except:
  152.         p is not None
  153.  
  154.     if len(_cache_repl) >= _MAXCACHE:
  155.         _cache_repl.clear()
  156.     
  157.     _cache_repl[key] = p
  158.     return p
  159.  
  160.  
  161. def _expand(pattern, match, template):
  162.     template = sre_parse.parse_template(template, pattern)
  163.     return sre_parse.expand_template(template, match)
  164.  
  165.  
  166. def _subx(pattern, template):
  167.     template = _compile_repl(template, pattern)
  168.     if not template[0] and len(template[1]) == 1:
  169.         return template[1][0]
  170.     
  171.     def filter(match, template = template):
  172.         return sre_parse.expand_template(template, match)
  173.  
  174.     return filter
  175.  
  176. import copy_reg
  177.  
  178. def _pickle(p):
  179.     return (_compile, (p.pattern, p.flags))
  180.  
  181. copy_reg.pickle(_pattern_type, _pickle, _compile)
  182.  
  183. class Scanner:
  184.     
  185.     def __init__(self, lexicon, flags = 0):
  186.         BRANCH = BRANCH
  187.         SUBPATTERN = SUBPATTERN
  188.         import sre_constants
  189.         self.lexicon = lexicon
  190.         p = []
  191.         s = sre_parse.Pattern()
  192.         s.flags = flags
  193.         for phrase, action in lexicon:
  194.             p.append(sre_parse.SubPattern(s, [
  195.                 (SUBPATTERN, (len(p) + 1, sre_parse.parse(phrase, flags)))]))
  196.         
  197.         s.groups = len(p) + 1
  198.         p = sre_parse.SubPattern(s, [
  199.             (BRANCH, (None, p))])
  200.         self.scanner = sre_compile.compile(p)
  201.  
  202.     
  203.     def scan(self, string):
  204.         result = []
  205.         append = result.append
  206.         match = self.scanner.scanner(string).match
  207.         i = 0
  208.         while None:
  209.             m = match()
  210.             if not m:
  211.                 break
  212.             
  213.             j = m.end()
  214.             if i == j:
  215.                 break
  216.             
  217.             action = self.lexicon[m.lastindex - 1][1]
  218.             if hasattr(action, '__call__'):
  219.                 self.match = m
  220.                 action = action(self, m.group())
  221.             
  222.             if action is not None:
  223.                 append(action)
  224.             
  225.             i = j
  226.             continue
  227.             return (result, string[i:])
  228.  
  229.  
  230.