Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fnmatch.py 3.2KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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__ = ["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. name = name.lower()
  29. pat = pat.lower()
  30. return fnmatchcase(name, pat)
  31. def fnmatchcase(name, pat):
  32. """Test whether FILENAME matches PATTERN, including case.
  33. This is a version of fnmatch() which doesn't case-normalize
  34. its arguments.
  35. """
  36. try:
  37. re_pat = _cache[pat]
  38. except KeyError:
  39. res = translate(pat)
  40. if len(_cache) >= _MAXCACHE:
  41. _cache.clear()
  42. _cache[pat] = re_pat = re.compile(res)
  43. return re_pat.match(name) is not None
  44. def translate(pat):
  45. """Translate a shell PATTERN to a regular expression.
  46. There is no way to quote meta-characters.
  47. """
  48. i, n = 0, len(pat)
  49. res = '^'
  50. while i < n:
  51. c = pat[i]
  52. i = i + 1
  53. if c == '*':
  54. if i < n and pat[i] == '*':
  55. # is some flavor of "**"
  56. i = i + 1
  57. # Treat **/ as ** so eat the "/"
  58. if i < n and pat[i] == '/':
  59. i = i + 1
  60. if i >= n:
  61. # is "**EOF" - to align with .gitignore just accept all
  62. res = res + '.*'
  63. else:
  64. # is "**"
  65. # Note that this allows for any # of /'s (even 0) because
  66. # the .* will eat everything, even /'s
  67. res = res + '(.*/)?'
  68. else:
  69. # is "*" so map it to anything but "/"
  70. res = res + '[^/]*'
  71. elif c == '?':
  72. # "?" is any char except "/"
  73. res = res + '[^/]'
  74. elif c == '[':
  75. j = i
  76. if j < n and pat[j] == '!':
  77. j = j + 1
  78. if j < n and pat[j] == ']':
  79. j = j + 1
  80. while j < n and pat[j] != ']':
  81. j = j + 1
  82. if j >= n:
  83. res = res + '\\['
  84. else:
  85. stuff = pat[i:j].replace('\\', '\\\\')
  86. i = j + 1
  87. if stuff[0] == '!':
  88. stuff = '^' + stuff[1:]
  89. elif stuff[0] == '^':
  90. stuff = '\\' + stuff
  91. res = f'{res}[{stuff}]'
  92. else:
  93. res = res + re.escape(c)
  94. return res + '$'