/usr/lib/python3/dist-packages/pathspec/pattern.py is in python3-pathspec 0.5.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | # encoding: utf-8
"""
This module provides the base definition for patterns.
"""
import re
from .compat import unicode
class Pattern(object):
"""
The :class:`Pattern` class is the abstract definition of a pattern.
"""
# Make the class dict-less.
__slots__ = ('include',)
def __init__(self, include):
"""
Initializes the :class:`Pattern` instance.
*include* (:class:`bool` or :data:`None`) is whether the matched
files should be included (:data:`True`), excluded (:data:`False`),
or is a null-operation (:data:`None`).
"""
self.include = include
"""
*include* (:class:`bool` or :data:`None`) is whether the matched
files should be included (:data:`True`), excluded (:data:`False`),
or is a null-operation (:data:`None`).
"""
def match(self, files):
"""
Matches this pattern against the specified files.
*files* (:class:`~collections.Iterable` of :class:`str`) contains
each file relative to the root directory (e.g., ``"relative/path/to/file"``).
Returns an :class:`~collections.Iterable` yielding each matched file
path (:class:`str`).
"""
raise NotImplementedError("{0}.{1} must override match().".format(self.__class__.__module__, self.__class__.__name__))
class RegexPattern(Pattern):
"""
The :class:`RegexPattern` class is an implementation of a pattern
using regular expressions.
"""
# Make the class dict-less.
__slots__ = ('regex',)
def __init__(self, pattern, include=None):
"""
Initializes the :class:`RegexPattern` instance.
*pattern* (:class:`unicode`, :class:`bytes`, :class:`re.RegexObject`,
or :data:`None`) is the pattern to compile into a regular
expression.
*include* (:class:`bool` or :data:`None`) must be :data:`None`
unless *pattern* is a precompiled regular expression (:class:`re.RegexObject`)
in which case it is whether matched files should be included
(:data:`True`), excluded (:data:`False`), or is a null operation
(:data:`None`).
.. NOTE:: Subclasses do not need to support the *include*
parameter.
"""
self.regex = None
"""
*regex* (:class:`re.RegexObject`) is the regular expression for the
pattern.
"""
if isinstance(pattern, (unicode, bytes)):
assert include is None, "include:{0!r} must be null when pattern:{1!r} is a string.".format(include, pattern)
regex, include = self.pattern_to_regex(pattern)
# NOTE: Make sure to allow a null regular expression to be
# returned for a null-operation.
if include is not None:
regex = re.compile(regex)
elif pattern is not None and hasattr(pattern, 'match'):
# Assume pattern is a precompiled regular expression.
# - NOTE: Used specified *include*.
regex = pattern
elif pattern is None:
# NOTE: Make sure to allow a null pattern to be passed for a
# null-operation.
assert include is None, "include:{0!r} must be null when pattern:{1!r} is null.".format(include, pattern)
else:
raise TypeError("pattern:{0!r} is not a string, RegexObject, or None.".format(pattern))
super(RegexPattern, self).__init__(include)
self.regex = regex
def __eq__(self, other):
"""
Tests the equality of this regex pattern with *other* (:class:`RegexPattern`)
by comparing their :attr:`~Pattern.include` and :attr:`~RegexPattern.regex`
attributes.
"""
if isinstance(other, RegexPattern):
return self.include == other.include and self.regex == other.regex
else:
return NotImplemented
def match(self, files):
"""
Matches this pattern against the specified files.
*files* (:class:`~collections.Iterable` of :class:`str`) contains
each file relative to the root directory (e.g., "relative/path/to/file").
Returns an :class:`~collections.Iterable` yielding each matched file
path (:class:`str`).
"""
if self.include is not None:
for path in files:
if self.regex.match(path) is not None:
yield path
@classmethod
def pattern_to_regex(cls, pattern):
"""
Convert the pattern into an uncompiled regular expression.
*pattern* (:class:`str`) is the pattern to convert into a regular
expression.
Returns the uncompiled regular expression (:class:`str` or :data:`None`),
and whether matched files should be included (:data:`True`),
excluded (:data:`False`), or is a null-operation (:data:`None`).
.. NOTE:: The default implementation simply returns *pattern* and
:data:`True`.
"""
return pattern, True
|