fnmatch.py 3.4 KB

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