PageRenderTime 73ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib-python/2.7/fnmatch.py

https://bitbucket.org/kkris/pypy
Python | 116 lines | 79 code | 5 blank | 32 comment | 0 complexity | ff3c09863951a949a37b9dc950d21283 MD5 | raw file
  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. if not pat in _cache:
  38. res = translate(pat)
  39. if len(_cache) >= _MAXCACHE:
  40. _cache.clear()
  41. _cache[pat] = re.compile(res)
  42. match=_cache[pat].match
  43. if os.path is posixpath:
  44. # normcase on posix is NOP. Optimize it away from the loop.
  45. for name in names:
  46. if match(name):
  47. result.append(name)
  48. else:
  49. for name in names:
  50. if match(os.path.normcase(name)):
  51. result.append(name)
  52. return result
  53. def fnmatchcase(name, pat):
  54. """Test whether FILENAME matches PATTERN, including case.
  55. This is a version of fnmatch() which doesn't case-normalize
  56. its arguments.
  57. """
  58. if not pat in _cache:
  59. res = translate(pat)
  60. if len(_cache) >= _MAXCACHE:
  61. _cache.clear()
  62. _cache[pat] = re.compile(res)
  63. return _cache[pat].match(name) is not None
  64. def translate(pat):
  65. """Translate a shell PATTERN to a regular expression.
  66. There is no way to quote meta-characters.
  67. """
  68. i, n = 0, len(pat)
  69. res = ''
  70. while i < n:
  71. c = pat[i]
  72. i = i+1
  73. if c == '*':
  74. res = res + '.*'
  75. elif c == '?':
  76. res = res + '.'
  77. elif c == '[':
  78. j = i
  79. if j < n and pat[j] == '!':
  80. j = j+1
  81. if j < n and pat[j] == ']':
  82. j = j+1
  83. while j < n and pat[j] != ']':
  84. j = j+1
  85. if j >= n:
  86. res = res + '\\['
  87. else:
  88. stuff = pat[i:j].replace('\\','\\\\')
  89. i = j+1
  90. if stuff[0] == '!':
  91. stuff = '^' + stuff[1:]
  92. elif stuff[0] == '^':
  93. stuff = '\\' + stuff
  94. res = '%s[%s]' % (res, stuff)
  95. else:
  96. res = res + re.escape(c)
  97. return res + '\Z(?ms)'