home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / iria107a.lzh / script / sre.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-11-17  |  7.7 KB  |  220 lines

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