glob.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import os
  2. import re
  3. _default_seps = os.sep + str(os.altsep) * bool(os.altsep)
  4. class Translator:
  5. """
  6. >>> Translator('xyz')
  7. Traceback (most recent call last):
  8. ...
  9. AssertionError: Invalid separators
  10. >>> Translator('')
  11. Traceback (most recent call last):
  12. ...
  13. AssertionError: Invalid separators
  14. """
  15. seps: str
  16. def __init__(self, seps: str = _default_seps):
  17. assert seps and set(seps) <= set(_default_seps), "Invalid separators"
  18. self.seps = seps
  19. def translate(self, pattern):
  20. """
  21. Given a glob pattern, produce a regex that matches it.
  22. """
  23. return self.extend(self.translate_core(pattern))
  24. def extend(self, pattern):
  25. r"""
  26. Extend regex for pattern-wide concerns.
  27. Apply '(?s:)' to create a non-matching group that
  28. matches newlines (valid on Unix).
  29. Append '\Z' to imply fullmatch even when match is used.
  30. """
  31. return rf'(?s:{pattern})\Z'
  32. def translate_core(self, pattern):
  33. r"""
  34. Given a glob pattern, produce a regex that matches it.
  35. >>> t = Translator()
  36. >>> t.translate_core('*.txt').replace('\\\\', '')
  37. '[^/]*\\.txt'
  38. >>> t.translate_core('a?txt')
  39. 'a[^/]txt'
  40. >>> t.translate_core('**/*').replace('\\\\', '')
  41. '.*/[^/][^/]*'
  42. """
  43. self.restrict_rglob(pattern)
  44. return ''.join(map(self.replace, separate(self.star_not_empty(pattern))))
  45. def replace(self, match):
  46. """
  47. Perform the replacements for a match from :func:`separate`.
  48. """
  49. return match.group('set') or (
  50. re.escape(match.group(0))
  51. .replace('\\*\\*', r'.*')
  52. .replace('\\*', rf'[^{re.escape(self.seps)}]*')
  53. .replace('\\?', r'[^/]')
  54. )
  55. def restrict_rglob(self, pattern):
  56. """
  57. Raise ValueError if ** appears in anything but a full path segment.
  58. >>> Translator().translate('**foo')
  59. Traceback (most recent call last):
  60. ...
  61. ValueError: ** must appear alone in a path segment
  62. """
  63. seps_pattern = rf'[{re.escape(self.seps)}]+'
  64. segments = re.split(seps_pattern, pattern)
  65. if any('**' in segment and segment != '**' for segment in segments):
  66. raise ValueError("** must appear alone in a path segment")
  67. def star_not_empty(self, pattern):
  68. """
  69. Ensure that * will not match an empty segment.
  70. """
  71. def handle_segment(match):
  72. segment = match.group(0)
  73. return '?*' if segment == '*' else segment
  74. not_seps_pattern = rf'[^{re.escape(self.seps)}]+'
  75. return re.sub(not_seps_pattern, handle_segment, pattern)
  76. def separate(pattern):
  77. """
  78. Separate out character sets to avoid translating their contents.
  79. >>> [m.group(0) for m in separate('*.txt')]
  80. ['*.txt']
  81. >>> [m.group(0) for m in separate('a[?]txt')]
  82. ['a', '[?]', 'txt']
  83. """
  84. return re.finditer(r'([^\[]+)|(?P<set>[\[].*?[\]])|([\[][^\]]*$)', pattern)