PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Python/system/fnmatch.py

https://bitbucket.org/cwalther/moulscript-dlanor
Python | 107 lines | 72 code | 4 blank | 31 comment | 0 complexity | 16984fd2319d814a98fc6a5e9a6b370e MD5 | raw file
Possible License(s): AGPL-1.0, GPL-3.0
  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. def fnmatch(name, pat):
  13. """Test whether FILENAME matches PATTERN.
  14. Patterns are Unix shell style:
  15. * matches everything
  16. ? matches any single character
  17. [seq] matches any character in seq
  18. [!seq] matches any char not in seq
  19. An initial period in FILENAME is not special.
  20. Both FILENAME and PATTERN are first case-normalized
  21. if the operating system requires it.
  22. If you don't want this, use fnmatchcase(FILENAME, PATTERN).
  23. """
  24. import os
  25. name = os.path.normcase(name)
  26. pat = os.path.normcase(pat)
  27. return fnmatchcase(name, pat)
  28. def filter(names, pat):
  29. """Return the subset of the list NAMES that match PAT"""
  30. import os,posixpath
  31. result=[]
  32. pat=os.path.normcase(pat)
  33. if not pat in _cache:
  34. res = translate(pat)
  35. _cache[pat] = re.compile(res)
  36. match=_cache[pat].match
  37. if os.path is posixpath:
  38. # normcase on posix is NOP. Optimize it away from the loop.
  39. for name in names:
  40. if match(name):
  41. result.append(name)
  42. else:
  43. for name in names:
  44. if match(os.path.normcase(name)):
  45. result.append(name)
  46. return result
  47. def fnmatchcase(name, pat):
  48. """Test whether FILENAME matches PATTERN, including case.
  49. This is a version of fnmatch() which doesn't case-normalize
  50. its arguments.
  51. """
  52. if not pat in _cache:
  53. res = translate(pat)
  54. _cache[pat] = re.compile(res)
  55. return _cache[pat].match(name) is not None
  56. def translate(pat):
  57. """Translate a shell PATTERN to a regular expression.
  58. There is no way to quote meta-characters.
  59. """
  60. i, n = 0, len(pat)
  61. res = ''
  62. while i < n:
  63. c = pat[i]
  64. i = i+1
  65. if c == '*':
  66. res = res + '.*'
  67. elif c == '?':
  68. res = res + '.'
  69. elif c == '[':
  70. j = i
  71. if j < n and pat[j] == '!':
  72. j = j+1
  73. if j < n and pat[j] == ']':
  74. j = j+1
  75. while j < n and pat[j] != ']':
  76. j = j+1
  77. if j >= n:
  78. res = res + '\\['
  79. else:
  80. stuff = pat[i:j].replace('\\','\\\\')
  81. i = j+1
  82. if stuff[0] == '!':
  83. stuff = '^' + stuff[1:]
  84. elif stuff[0] == '^':
  85. stuff = '\\' + stuff
  86. res = '%s[%s]' % (res, stuff)
  87. else:
  88. res = res + re.escape(c)
  89. return res + "$"