home *** CD-ROM | disk | FTP | other *** search
/ com!online 2005 April / com_0405_1.iso / opensource / BTpp-0.5.4-bin.exe / $INSTDIR / BT++.exe / sre.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-04-19  |  7.7 KB  |  231 lines

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