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 / pre.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-04-19  |  11.7 KB  |  402 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.2)
  3.  
  4. import sys
  5. from pcre import *
  6. __all__ = [
  7.     'match',
  8.     'search',
  9.     'sub',
  10.     'subn',
  11.     'split',
  12.     'findall',
  13.     'escape',
  14.     'compile',
  15.     'I',
  16.     'L',
  17.     'M',
  18.     'S',
  19.     'X',
  20.     'IGNORECASE',
  21.     'LOCALE',
  22.     'MULTILINE',
  23.     'DOTALL',
  24.     'VERBOSE',
  25.     'error']
  26. I = IGNORECASE
  27. L = LOCALE
  28. M = MULTILINE
  29. S = DOTALL
  30. X = VERBOSE
  31. _cache = { }
  32. _MAXCACHE = 20
  33.  
  34. def _cachecompile(pattern, flags = 0):
  35.     key = (pattern, flags)
  36.     
  37.     try:
  38.         return _cache[key]
  39.     except KeyError:
  40.         pass
  41.  
  42.     value = compile(pattern, flags)
  43.     if len(_cache) >= _MAXCACHE:
  44.         _cache.clear()
  45.     
  46.     _cache[key] = value
  47.     return value
  48.  
  49.  
  50. def match(pattern, string, flags = 0):
  51.     return _cachecompile(pattern, flags).match(string)
  52.  
  53.  
  54. def search(pattern, string, flags = 0):
  55.     return _cachecompile(pattern, flags).search(string)
  56.  
  57.  
  58. def sub(pattern, repl, string, count = 0):
  59.     if type(pattern) == type(''):
  60.         pattern = _cachecompile(pattern)
  61.     
  62.     return pattern.sub(repl, string, count)
  63.  
  64.  
  65. def subn(pattern, repl, string, count = 0):
  66.     if type(pattern) == type(''):
  67.         pattern = _cachecompile(pattern)
  68.     
  69.     return pattern.subn(repl, string, count)
  70.  
  71.  
  72. def split(pattern, string, maxsplit = 0):
  73.     if type(pattern) == type(''):
  74.         pattern = _cachecompile(pattern)
  75.     
  76.     return pattern.split(string, maxsplit)
  77.  
  78.  
  79. def findall(pattern, string):
  80.     if type(pattern) == type(''):
  81.         pattern = _cachecompile(pattern)
  82.     
  83.     return pattern.findall(string)
  84.  
  85.  
  86. def escape(pattern):
  87.     result = list(pattern)
  88.     for i in range(len(pattern)):
  89.         char = pattern[i]
  90.         if not char.isalnum():
  91.             if char == '\x00':
  92.                 result[i] = '\\000'
  93.             else:
  94.                 result[i] = '\\' + char
  95.         
  96.     
  97.     return ''.join(result)
  98.  
  99.  
  100. def compile(pattern, flags = 0):
  101.     groupindex = { }
  102.     code = pcre_compile(pattern, flags, groupindex)
  103.     return RegexObject(pattern, flags, code, groupindex)
  104.  
  105.  
  106. class RegexObject:
  107.     
  108.     def __init__(self, pattern, flags, code, groupindex):
  109.         self.code = code
  110.         self.flags = flags
  111.         self.pattern = pattern
  112.         self.groupindex = groupindex
  113.  
  114.     
  115.     def search(self, string, pos = 0, endpos = None):
  116.         if endpos is None or endpos > len(string):
  117.             endpos = len(string)
  118.         
  119.         if endpos < pos:
  120.             endpos = pos
  121.         
  122.         regs = self.code.match(string, pos, endpos, 0)
  123.         if regs is None:
  124.             return None
  125.         
  126.         self._num_regs = len(regs)
  127.         return MatchObject(self, string, pos, endpos, regs)
  128.  
  129.     
  130.     def match(self, string, pos = 0, endpos = None):
  131.         if endpos is None or endpos > len(string):
  132.             endpos = len(string)
  133.         
  134.         if endpos < pos:
  135.             endpos = pos
  136.         
  137.         regs = self.code.match(string, pos, endpos, ANCHORED)
  138.         if regs is None:
  139.             return None
  140.         
  141.         self._num_regs = len(regs)
  142.         return MatchObject(self, string, pos, endpos, regs)
  143.  
  144.     
  145.     def sub(self, repl, string, count = 0):
  146.         return self.subn(repl, string, count)[0]
  147.  
  148.     
  149.     def subn(self, repl, source, count = 0):
  150.         if count < 0:
  151.             raise error, 'negative substitution count'
  152.         
  153.         if count == 0:
  154.             count = sys.maxint
  155.         
  156.         n = 0
  157.         pos = 0
  158.         lastmatch = -1
  159.         results = []
  160.         end = len(source)
  161.         if type(repl) is type(''):
  162.             
  163.             try:
  164.                 repl = pcre_expand(_Dummy, repl)
  165.             except (error, TypeError):
  166.                 m = MatchObject(self, source, 0, end, [])
  167.                 
  168.                 repl = lambda m, repl = repl, expand = pcre_expand: expand(m, repl)
  169.  
  170.             m = None
  171.         else:
  172.             m = MatchObject(self, source, 0, end, [])
  173.         match = self.code.match
  174.         append = results.append
  175.         while n < count and pos <= end:
  176.             regs = match(source, pos, end, 0)
  177.             if not regs:
  178.                 break
  179.             
  180.             self._num_regs = len(regs)
  181.             (i, j) = regs[0]
  182.             if j == j:
  183.                 pass
  184.             elif j == lastmatch:
  185.                 pos = pos + 1
  186.                 append(source[lastmatch:pos])
  187.                 continue
  188.             
  189.             if pos < i:
  190.                 append(source[pos:i])
  191.             
  192.             if m:
  193.                 m.pos = pos
  194.                 m.regs = regs
  195.                 append(repl(m))
  196.             else:
  197.                 append(repl)
  198.             pos = lastmatch = j
  199.             if i == j:
  200.                 pos = pos + 1
  201.                 append(source[lastmatch:pos])
  202.             
  203.             n = n + 1
  204.         append(source[pos:])
  205.         return (''.join(results), n)
  206.  
  207.     
  208.     def split(self, source, maxsplit = 0):
  209.         if maxsplit < 0:
  210.             raise error, 'negative split count'
  211.         
  212.         if maxsplit == 0:
  213.             maxsplit = sys.maxint
  214.         
  215.         n = 0
  216.         pos = 0
  217.         lastmatch = 0
  218.         results = []
  219.         end = len(source)
  220.         match = self.code.match
  221.         append = results.append
  222.         while n < maxsplit:
  223.             regs = match(source, pos, end, 0)
  224.             if not regs:
  225.                 break
  226.             
  227.             (i, j) = regs[0]
  228.             if i == j:
  229.                 if pos >= end:
  230.                     break
  231.                 
  232.                 pos = pos + 1
  233.                 continue
  234.             
  235.             append(source[lastmatch:i])
  236.             rest = regs[1:]
  237.             if rest:
  238.                 for a, b in rest:
  239.                     if a == -1 or b == -1:
  240.                         group = None
  241.                     else:
  242.                         group = source[a:b]
  243.                     append(group)
  244.                 
  245.             
  246.             pos = lastmatch = j
  247.             n = n + 1
  248.         append(source[lastmatch:])
  249.         return results
  250.  
  251.     
  252.     def findall(self, source):
  253.         pos = 0
  254.         end = len(source)
  255.         results = []
  256.         match = self.code.match
  257.         append = results.append
  258.         while pos <= end:
  259.             regs = match(source, pos, end, 0)
  260.             if not regs:
  261.                 break
  262.             
  263.             (i, j) = regs[0]
  264.             rest = regs[1:]
  265.             if not rest:
  266.                 gr = source[i:j]
  267.             elif len(rest) == 1:
  268.                 (a, b) = rest[0]
  269.                 gr = source[a:b]
  270.             else:
  271.                 gr = []
  272.                 for a, b in rest:
  273.                     gr.append(source[a:b])
  274.                 
  275.                 gr = tuple(gr)
  276.             append(gr)
  277.             pos = max(j, pos + 1)
  278.         return results
  279.  
  280.     
  281.     def __getinitargs__(self):
  282.         return (None, None, None, None)
  283.  
  284.     
  285.     def __getstate__(self):
  286.         return (self.pattern, self.flags, self.groupindex)
  287.  
  288.     
  289.     def __setstate__(self, statetuple):
  290.         self.pattern = statetuple[0]
  291.         self.flags = statetuple[1]
  292.         self.groupindex = statetuple[2]
  293.         self.code = apply(pcre_compile, statetuple)
  294.  
  295.  
  296.  
  297. class _Dummy:
  298.     group = None
  299.  
  300.  
  301. class MatchObject:
  302.     
  303.     def __init__(self, re, string, pos, endpos, regs):
  304.         self.re = re
  305.         self.string = string
  306.         self.pos = pos
  307.         self.endpos = endpos
  308.         self.regs = regs
  309.  
  310.     
  311.     def start(self, g = 0):
  312.         if type(g) == type(''):
  313.             
  314.             try:
  315.                 g = self.re.groupindex[g]
  316.             except (KeyError, TypeError):
  317.                 raise IndexError, 'group %s is undefined' % `g`
  318.  
  319.         
  320.         return self.regs[g][0]
  321.  
  322.     
  323.     def end(self, g = 0):
  324.         if type(g) == type(''):
  325.             
  326.             try:
  327.                 g = self.re.groupindex[g]
  328.             except (KeyError, TypeError):
  329.                 raise IndexError, 'group %s is undefined' % `g`
  330.  
  331.         
  332.         return self.regs[g][1]
  333.  
  334.     
  335.     def span(self, g = 0):
  336.         if type(g) == type(''):
  337.             
  338.             try:
  339.                 g = self.re.groupindex[g]
  340.             except (KeyError, TypeError):
  341.                 raise IndexError, 'group %s is undefined' % `g`
  342.  
  343.         
  344.         return self.regs[g]
  345.  
  346.     
  347.     def groups(self, default = None):
  348.         result = []
  349.         for g in range(1, self.re._num_regs):
  350.             (a, b) = self.regs[g]
  351.             if a == -1 or b == -1:
  352.                 result.append(default)
  353.             else:
  354.                 result.append(self.string[a:b])
  355.         
  356.         return tuple(result)
  357.  
  358.     
  359.     def group(self, *groups):
  360.         if len(groups) == 0:
  361.             groups = (0,)
  362.         
  363.         result = []
  364.         for g in groups:
  365.             if type(g) == type(''):
  366.                 
  367.                 try:
  368.                     g = self.re.groupindex[g]
  369.                 except (KeyError, TypeError):
  370.                     raise IndexError, 'group %s is undefined' % `g`
  371.  
  372.             
  373.             if g >= len(self.regs):
  374.                 raise IndexError, 'group %s is undefined' % `g`
  375.             
  376.             (a, b) = self.regs[g]
  377.             if a == -1 or b == -1:
  378.                 result.append(None)
  379.             else:
  380.                 result.append(self.string[a:b])
  381.         
  382.         if len(result) > 1:
  383.             return tuple(result)
  384.         elif len(result) == 1:
  385.             return result[0]
  386.         else:
  387.             return ()
  388.  
  389.     
  390.     def groupdict(self, default = None):
  391.         dict = { }
  392.         for name, index in self.re.groupindex.items():
  393.             (a, b) = self.regs[index]
  394.             if a == -1 or b == -1:
  395.                 dict[name] = default
  396.             else:
  397.                 dict[name] = self.string[a:b]
  398.         
  399.         return dict
  400.  
  401.  
  402.