home *** CD-ROM | disk | FTP | other *** search
/ PC Extra 07 & 08 / pca1507.iso / Software / psp8 / Data1.cab / fnmatch.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-04-22  |  3.7 KB  |  121 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.2)
  3.  
  4. '''Filename matching with shell patterns.
  5.  
  6. fnmatch(FILENAME, PATTERN) matches according to the local convention.
  7. fnmatchcase(FILENAME, PATTERN) always takes case in account.
  8.  
  9. The functions operate by translating the pattern into a regular
  10. expression.  They cache the compiled regular expressions for speed.
  11.  
  12. The function translate(PATTERN) returns a regular expression
  13. corresponding to PATTERN.  (It does not compile it.)
  14. '''
  15. import re
  16. __all__ = [
  17.     'fnmatch',
  18.     'fnmatchcase',
  19.     'translate']
  20. _cache = { }
  21.  
  22. def fnmatch(name, pat):
  23.     """Test whether FILENAME matches PATTERN.
  24.  
  25.     Patterns are Unix shell style:
  26.  
  27.     *       matches everything
  28.     ?       matches any single character
  29.     [seq]   matches any character in seq
  30.     [!seq]  matches any char not in seq
  31.  
  32.     An initial period in FILENAME is not special.
  33.     Both FILENAME and PATTERN are first case-normalized
  34.     if the operating system requires it.
  35.     If you don't want this, use fnmatchcase(FILENAME, PATTERN).
  36.     """
  37.     import os
  38.     name = os.path.normcase(name)
  39.     pat = os.path.normcase(pat)
  40.     return fnmatchcase(name, pat)
  41.  
  42.  
  43. def filter(names, pat):
  44.     '''Return the subset of the list NAMES that match PAT'''
  45.     import os
  46.     import posixpath
  47.     result = []
  48.     pat = os.path.normcase(pat)
  49.     if not _cache.has_key(pat):
  50.         res = translate(pat)
  51.         _cache[pat] = re.compile(res)
  52.     
  53.     match = _cache[pat].match
  54.     if os.path is posixpath:
  55.         for name in names:
  56.             if match(name):
  57.                 result.append(name)
  58.             
  59.         
  60.     else:
  61.         for name in names:
  62.             if match(os.path.normcase(name)):
  63.                 result.append(name)
  64.             
  65.         
  66.     return result
  67.  
  68.  
  69. def fnmatchcase(name, pat):
  70.     """Test whether FILENAME matches PATTERN, including case.
  71.  
  72.     This is a version of fnmatch() which doesn't case-normalize
  73.     its arguments.
  74.     """
  75.     if not _cache.has_key(pat):
  76.         res = translate(pat)
  77.         _cache[pat] = re.compile(res)
  78.     
  79.     return _cache[pat].match(name) is not None
  80.  
  81.  
  82. def translate(pat):
  83.     '''Translate a shell PATTERN to a regular expression.
  84.  
  85.     There is no way to quote meta-characters.
  86.     '''
  87.     (i, n) = (0, len(pat))
  88.     res = ''
  89.     while i < n:
  90.         c = pat[i]
  91.         i = i + 1
  92.         if c == '*':
  93.             res = res + '.*'
  94.         elif c == '?':
  95.             res = res + '.'
  96.         elif c == '[':
  97.             j = i
  98.             if j < n and pat[j] == '!':
  99.                 j = j + 1
  100.             
  101.             if j < n and pat[j] == ']':
  102.                 j = j + 1
  103.             
  104.             while j < n and pat[j] != ']':
  105.                 j = j + 1
  106.             if j >= n:
  107.                 res = res + '\\['
  108.             else:
  109.                 stuff = pat[i:j].replace('\\', '\\\\')
  110.                 i = j + 1
  111.                 if stuff[0] == '!':
  112.                     stuff = '^' + stuff[1:]
  113.                 elif stuff[0] == '^':
  114.                     stuff = '\\' + stuff
  115.                 
  116.                 res = '%s[%s]' % (res, stuff)
  117.         else:
  118.             res = res + re.escape(c)
  119.     return res + '$'
  120.  
  121.