home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 January / Chip_2003-01_cd2.bin / convert / eJayMp3Pro / mp3pro_demo.exe / FNMATCH.PYC (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-10-30  |  2.8 KB  |  94 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  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. _cache = { }
  17.  
  18. def fnmatch(name, pat):
  19.     """Test whether FILENAME matches PATTERN.
  20. \t
  21. \tPatterns are Unix shell style:
  22. \t
  23. \t*\tmatches everything
  24. \t?\tmatches any single character
  25. \t[seq]\tmatches any character in seq
  26. \t[!seq]\tmatches any char not in seq
  27. \t
  28. \tAn initial period in FILENAME is not special.
  29. \tBoth FILENAME and PATTERN are first case-normalized
  30. \tif the operating system requires it.
  31. \tIf you don't want this, use fnmatchcase(FILENAME, PATTERN).
  32. \t"""
  33.     import os
  34.     name = os.path.normcase(name)
  35.     pat = os.path.normcase(pat)
  36.     return fnmatchcase(name, pat)
  37.  
  38.  
  39. def fnmatchcase(name, pat):
  40.     """Test wheter FILENAME matches PATTERN, including case.
  41. \t
  42. \tThis is a version of fnmatch() which doesn't case-normalize
  43. \tits arguments.
  44. \t"""
  45.     if not _cache.has_key(pat):
  46.         res = translate(pat)
  47.         _cache[pat] = re.compile(res)
  48.     
  49.     return _cache[pat].match(name) is not None
  50.  
  51.  
  52. def translate(pat):
  53.     '''Translate a shell PATTERN to a regular expression.
  54. \t
  55. \tThere is no way to quote meta-characters.
  56. \t'''
  57.     (i, n) = (0, len(pat))
  58.     res = ''
  59.     while i < n:
  60.         c = pat[i]
  61.         i = i + 1
  62.         if c == '*':
  63.             res = res + '.*'
  64.         elif c == '?':
  65.             res = res + '.'
  66.         elif c == '[':
  67.             j = i
  68.             if j < n and pat[j] == '!':
  69.                 j = j + 1
  70.             
  71.             if j < n and pat[j] == ']':
  72.                 j = j + 1
  73.             
  74.             while j < n and pat[j] != ']':
  75.                 j = j + 1
  76.             if j >= n:
  77.                 res = res + '\\['
  78.             else:
  79.                 stuff = pat[i:j]
  80.                 i = j + 1
  81.                 if stuff[0] == '!':
  82.                     stuff = '[^' + stuff[1:] + ']'
  83.                 elif stuff == '^' * len(stuff):
  84.                     stuff = '\\^'
  85.                 else:
  86.                     while stuff[0] == '^':
  87.                         stuff = stuff[1:] + stuff[0]
  88.                     stuff = '[' + stuff + ']'
  89.                 res = res + stuff
  90.         else:
  91.             res = res + re.escape(c)
  92.     return res + '$'
  93.  
  94.