pattern.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. """
  2. This module provides the base definition for patterns.
  3. """
  4. import dataclasses
  5. import re
  6. import warnings
  7. from typing import (
  8. Any,
  9. AnyStr,
  10. Iterable, # Replaced by `collections.abc.Iterable` in 3.9.
  11. Iterator, # Replaced by `collections.abc.Iterator` in 3.9.
  12. Match as MatchHint, # Replaced by `re.Match` in 3.9.
  13. Optional, # Replaced by `X | None` in 3.10.
  14. Pattern as PatternHint, # Replaced by `re.Pattern` in 3.9.
  15. Tuple, # Replaced by `tuple` in 3.9.
  16. Union) # Replaced by `X | Y` in 3.10.
  17. class Pattern(object):
  18. """
  19. The :class:`Pattern` class is the abstract definition of a pattern.
  20. """
  21. # Make the class dict-less.
  22. __slots__ = (
  23. 'include',
  24. )
  25. def __init__(self, include: Optional[bool]) -> None:
  26. """
  27. Initializes the :class:`Pattern` instance.
  28. *include* (:class:`bool` or :data:`None`) is whether the matched files
  29. should be included (:data:`True`), excluded (:data:`False`), or is a
  30. null-operation (:data:`None`).
  31. """
  32. self.include = include
  33. """
  34. *include* (:class:`bool` or :data:`None`) is whether the matched files
  35. should be included (:data:`True`), excluded (:data:`False`), or is a
  36. null-operation (:data:`None`).
  37. """
  38. def match(self, files: Iterable[str]) -> Iterator[str]:
  39. """
  40. DEPRECATED: This method is no longer used and has been replaced by
  41. :meth:`.match_file`. Use the :meth:`.match_file` method with a loop for
  42. similar results.
  43. Matches this pattern against the specified files.
  44. *files* (:class:`~collections.abc.Iterable` of :class:`str`) contains each
  45. file relative to the root directory (e.g., ``"relative/path/to/file"``).
  46. Returns an :class:`~collections.abc.Iterable` yielding each matched file
  47. path (:class:`str`).
  48. """
  49. warnings.warn((
  50. "{cls.__module__}.{cls.__qualname__}.match() is deprecated. Use "
  51. "{cls.__module__}.{cls.__qualname__}.match_file() with a loop for "
  52. "similar results."
  53. ).format(cls=self.__class__), DeprecationWarning, stacklevel=2)
  54. for file in files:
  55. if self.match_file(file) is not None:
  56. yield file
  57. def match_file(self, file: str) -> Optional[Any]:
  58. """
  59. Matches this pattern against the specified file.
  60. *file* (:class:`str`) is the normalized file path to match against.
  61. Returns the match result if *file* matched; otherwise, :data:`None`.
  62. """
  63. raise NotImplementedError((
  64. "{cls.__module__}.{cls.__qualname__} must override match_file()."
  65. ).format(cls=self.__class__))
  66. class RegexPattern(Pattern):
  67. """
  68. The :class:`RegexPattern` class is an implementation of a pattern using
  69. regular expressions.
  70. """
  71. # Keep the class dict-less.
  72. __slots__ = (
  73. 'pattern',
  74. 'regex',
  75. )
  76. def __init__(
  77. self,
  78. pattern: Union[AnyStr, PatternHint, None],
  79. include: Optional[bool] = None,
  80. ) -> None:
  81. """
  82. Initializes the :class:`RegexPattern` instance.
  83. *pattern* (:class:`str`, :class:`bytes`, :class:`re.Pattern`, or
  84. :data:`None`) is the pattern to compile into a regular expression.
  85. *include* (:class:`bool` or :data:`None`) must be :data:`None` unless
  86. *pattern* is a precompiled regular expression (:class:`re.Pattern`) in which
  87. case it is whether matched files should be included (:data:`True`), excluded
  88. (:data:`False`), or is a null operation (:data:`None`).
  89. .. NOTE:: Subclasses do not need to support the *include* parameter.
  90. """
  91. if isinstance(pattern, (str, bytes)):
  92. assert include is None, (
  93. f"include:{include!r} must be null when pattern:{pattern!r} is a string."
  94. )
  95. regex, include = self.pattern_to_regex(pattern)
  96. # NOTE: Make sure to allow a null regular expression to be
  97. # returned for a null-operation.
  98. if include is not None:
  99. regex = re.compile(regex)
  100. elif pattern is not None and hasattr(pattern, 'match'):
  101. # Assume pattern is a precompiled regular expression.
  102. # - NOTE: Used specified *include*.
  103. regex = pattern
  104. elif pattern is None:
  105. # NOTE: Make sure to allow a null pattern to be passed for a
  106. # null-operation.
  107. assert include is None, (
  108. f"include:{include!r} must be null when pattern:{pattern!r} is null."
  109. )
  110. else:
  111. raise TypeError(f"pattern:{pattern!r} is not a string, re.Pattern, or None.")
  112. super(RegexPattern, self).__init__(include)
  113. self.pattern: Union[AnyStr, PatternHint, None] = pattern
  114. """
  115. *pattern* (:class:`str`, :class:`bytes`, :class:`re.Pattern`, or
  116. :data:`None`) is the uncompiled, input pattern. This is for reference.
  117. """
  118. self.regex: PatternHint = regex
  119. """
  120. *regex* (:class:`re.Pattern`) is the regular expression for the pattern.
  121. """
  122. def __eq__(self, other: 'RegexPattern') -> bool:
  123. """
  124. Tests the equality of this regex pattern with *other* (:class:`RegexPattern`)
  125. by comparing their :attr:`~Pattern.include` and :attr:`~RegexPattern.regex`
  126. attributes.
  127. """
  128. if isinstance(other, RegexPattern):
  129. return self.include == other.include and self.regex == other.regex
  130. else:
  131. return NotImplemented
  132. def match_file(self, file: str) -> Optional['RegexMatchResult']:
  133. """
  134. Matches this pattern against the specified file.
  135. *file* (:class:`str`) contains each file relative to the root directory
  136. (e.g., "relative/path/to/file").
  137. Returns the match result (:class:`.RegexMatchResult`) if *file* matched;
  138. otherwise, :data:`None`.
  139. """
  140. if self.include is not None:
  141. match = self.regex.match(file)
  142. if match is not None:
  143. return RegexMatchResult(match)
  144. return None
  145. @classmethod
  146. def pattern_to_regex(cls, pattern: str) -> Tuple[str, bool]:
  147. """
  148. Convert the pattern into an uncompiled regular expression.
  149. *pattern* (:class:`str`) is the pattern to convert into a regular
  150. expression.
  151. Returns the uncompiled regular expression (:class:`str` or :data:`None`),
  152. and whether matched files should be included (:data:`True`), excluded
  153. (:data:`False`), or is a null-operation (:data:`None`).
  154. .. NOTE:: The default implementation simply returns *pattern* and
  155. :data:`True`.
  156. """
  157. return pattern, True
  158. @dataclasses.dataclass()
  159. class RegexMatchResult(object):
  160. """
  161. The :class:`RegexMatchResult` data class is used to return information about
  162. the matched regular expression.
  163. """
  164. # Keep the class dict-less.
  165. __slots__ = (
  166. 'match',
  167. )
  168. match: MatchHint
  169. """
  170. *match* (:class:`re.Match`) is the regex match result.
  171. """