Exemple #1
0
 def _add_patterns(self, patterns, translator, prefix=''):
     while patterns:
         grouped_rules = ['(%s)' % translator(pat) for pat in patterns[:99]]
         joined_rule = '%s(?:%s)$' % (prefix, '|'.join(grouped_rules))
         # Explicitly use lazy_compile here, because we count on its
         # nicer error reporting.
         self._regex_patterns.append(
             (lazy_regex.lazy_compile(joined_rule,
                                      re.UNICODE), patterns[:99]))
         patterns = patterns[99:]
Exemple #2
0
class Replacer(object):
    """Do a multiple-pattern substitution.

    The patterns and substitutions are combined into one, so the result of
    one replacement is never substituted again. Add the patterns and
    replacements via the add method and then call the object. The patterns
    must not contain capturing groups.
    """

    _expand = lazy_regex.lazy_compile(r'\\&')

    def __init__(self, source=None):
        self._pat = None
        if source:
            self._pats = list(source._pats)
            self._funs = list(source._funs)
        else:
            self._pats = []
            self._funs = []

    def add(self, pat, fun):
        r"""Add a pattern and replacement.

        The pattern must not contain capturing groups.
        The replacement might be either a string template in which \& will be
        replaced with the match, or a function that will get the matching text
        as argument. It does not get match object, because capturing is
        forbidden anyway.
        """
        self._pat = None
        self._pats.append(pat)
        self._funs.append(fun)

    def add_replacer(self, replacer):
        r"""Add all patterns from another replacer.

        All patterns and replacements from replacer are appended to the ones
        already defined.
        """
        self._pat = None
        self._pats.extend(replacer._pats)
        self._funs.extend(replacer._funs)

    def __call__(self, text):
        if not self._pat:
            self._pat = lazy_regex.lazy_compile(
                '|'.join(['(%s)' % p for p in self._pats]), re.UNICODE)
        return self._pat.sub(self._do_sub, text)

    def _do_sub(self, m):
        fun = self._funs[m.lastindex - 1]
        if hasattr(fun, '__call__'):
            return fun(m.group(0))
        else:
            return self._expand.sub(m.group(0), fun)
Exemple #3
0
 def _add_patterns(self, patterns, translator, prefix=''):
     while patterns:
         grouped_rules = [
             '(%s)' % translator(pat) for pat in patterns[:99]]
         joined_rule = '%s(?:%s)$' % (prefix, '|'.join(grouped_rules))
         # Explicitly use lazy_compile here, because we count on its
         # nicer error reporting.
         self._regex_patterns.append((
             lazy_regex.lazy_compile(joined_rule, re.UNICODE),
             patterns[:99]))
         patterns = patterns[99:]
Exemple #4
0
    def is_pattern_valid(pattern):
        """Returns True if pattern is valid.

        :param pattern: Normalized pattern.
        is_pattern_valid() assumes pattern to be normalized.
        see: globbing.normalize_pattern
        """
        result = True
        translator = Globster.pattern_info[Globster.identify(pattern)]["translator"]
        tpattern = '(%s)' % translator(pattern)
        try:
            re_obj = lazy_regex.lazy_compile(tpattern, re.UNICODE)
            re_obj.search("") # force compile
        except Exception as e:
            result = False
        return result
Exemple #5
0
    def is_pattern_valid(pattern):
        """Returns True if pattern is valid.

        :param pattern: Normalized pattern.
        is_pattern_valid() assumes pattern to be normalized.
        see: globbing.normalize_pattern
        """
        result = True
        translator = Globster.pattern_info[Globster.identify(
            pattern)]["translator"]
        tpattern = '(%s)' % translator(pattern)
        try:
            re_obj = lazy_regex.lazy_compile(tpattern, re.UNICODE)
            re_obj.search("")  # force compile
        except Exception as e:
            result = False
        return result
Exemple #6
0
 def __call__(self, text):
     if not self._pat:
         self._pat = lazy_regex.lazy_compile(
             '|'.join(['(%s)' % p for p in self._pats]), re.UNICODE)
     return self._pat.sub(self._do_sub, text)
Exemple #7
0
class _OrderedGlobster(Globster):
    """A Globster that keeps pattern order."""
    def __init__(self, patterns):
        """Constructor.

        :param patterns: sequence of glob patterns
        """
        # Note: This could be smarter by running like sequences together
        self._regex_patterns = []
        for pat in patterns:
            pat = normalize_pattern(pat)
            t = Globster.identify(pat)
            self._add_patterns([pat], Globster.pattern_info[t]["translator"],
                               Globster.pattern_info[t]["prefix"])


_slashes = lazy_regex.lazy_compile(r'[\\/]+')


def normalize_pattern(pattern):
    """Converts backslashes in path patterns to forward slashes.

    Doesn't normalize regular expressions - they may contain escapes.
    """
    if not (pattern.startswith('RE:') or pattern.startswith('!RE:')):
        pattern = _slashes.sub('/', pattern)
    if len(pattern) > 1:
        pattern = pattern.rstrip('/')
    return pattern
Exemple #8
0
 def __call__(self, text):
     if not self._pat:
         self._pat = lazy_regex.lazy_compile(
                 '|'.join(['(%s)' % p for p in self._pats]),
                 re.UNICODE)
     return self._pat.sub(self._do_sub, text)
Exemple #9
0
            return self._ignores[0].match(filename)

class _OrderedGlobster(Globster):
    """A Globster that keeps pattern order."""

    def __init__(self, patterns):
        """Constructor.

        :param patterns: sequence of glob patterns
        """
        # Note: This could be smarter by running like sequences together
        self._regex_patterns = []
        for pat in patterns:
            pat = normalize_pattern(pat)
            t = Globster.identify(pat)
            self._add_patterns([pat], Globster.pattern_info[t]["translator"],
                Globster.pattern_info[t]["prefix"])


_slashes = lazy_regex.lazy_compile(r'[\\/]+')
def normalize_pattern(pattern):
    """Converts backslashes in path patterns to forward slashes.

    Doesn't normalize regular expressions - they may contain escapes.
    """
    if not (pattern.startswith('RE:') or pattern.startswith('!RE:')):
        pattern = _slashes.sub('/', pattern)
    if len(pattern) > 1:
        pattern = pattern.rstrip('/')
    return pattern