1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2025-03-15 00:21:56 +00:00

Rename pattern classes for consistency

The class names “IncludePattern” and “ExcludePattern” may have been
appropriate when they were the only styles. With the recent addition of
regular expression support and with at least one more style being added
in forthcoming changes these classes should be renamed to be more
descriptive. “ExcludeRegex” is also renamed to match the new names.
This commit is contained in:
Michael Hanselmann 2016-01-15 15:41:02 +01:00
parent 2c7ab8595d
commit 3a39ddbd83
3 changed files with 31 additions and 31 deletions

View file

@ -17,7 +17,7 @@ import traceback
from . import __version__ from . import __version__
from .helpers import Error, location_validator, format_time, format_file_size, \ from .helpers import Error, location_validator, format_time, format_file_size, \
format_file_mode, parse_pattern, IncludePattern, exclude_path, adjust_patterns, to_localtime, timestamp, \ format_file_mode, parse_pattern, PathPrefixPattern, exclude_path, adjust_patterns, to_localtime, timestamp, \
get_cache_dir, get_keys_dir, prune_within, prune_split, unhexlify, \ get_cache_dir, get_keys_dir, prune_within, prune_split, unhexlify, \
Manifest, remove_surrogates, update_excludes, format_archive, check_extension_modules, Statistics, \ Manifest, remove_surrogates, update_excludes, format_archive, check_extension_modules, Statistics, \
dir_is_tagged, bigint_to_int, ChunkerParams, CompressionSpec, is_slow_msgpack, yes, sysinfo, \ dir_is_tagged, bigint_to_int, ChunkerParams, CompressionSpec, is_slow_msgpack, yes, sysinfo, \
@ -314,7 +314,7 @@ class Archiver:
while dirs: while dirs:
archive.extract_item(dirs.pop(-1)) archive.extract_item(dirs.pop(-1))
for pattern in (patterns or []): for pattern in (patterns or []):
if isinstance(pattern, IncludePattern) and pattern.match_count == 0: if isinstance(pattern, PathPrefixPattern) and pattern.match_count == 0:
self.print_warning("Include pattern '%s' never matched.", pattern) self.print_warning("Include pattern '%s' never matched.", pattern)
return self.exit_code return self.exit_code

View file

@ -259,7 +259,7 @@ def update_excludes(args):
def adjust_patterns(paths, excludes): def adjust_patterns(paths, excludes):
if paths: if paths:
return (excludes or []) + [IncludePattern(path) for path in paths] + [ExcludePattern('*')] return (excludes or []) + [PathPrefixPattern(path) for path in paths] + [FnmatchPattern('*')]
else: else:
return excludes return excludes
@ -270,7 +270,7 @@ def exclude_path(path, patterns):
""" """
for pattern in (patterns or []): for pattern in (patterns or []):
if pattern.match(path): if pattern.match(path):
return isinstance(pattern, (ExcludePattern, ExcludeRegex)) return isinstance(pattern, (FnmatchPattern, RegexPattern))
return False return False
@ -326,14 +326,14 @@ class PatternBase:
raise NotImplementedError raise NotImplementedError
# For both IncludePattern and ExcludePattern, we require that # For both PathPrefixPattern and FnmatchPattern, we require that
# the pattern either match the whole path or an initial segment # the pattern either match the whole path or an initial segment
# of the path up to but not including a path separator. To # of the path up to but not including a path separator. To
# unify the two cases, we add a path separator to the end of # unify the two cases, we add a path separator to the end of
# the path before matching. # the path before matching.
class IncludePattern(PatternBase): class PathPrefixPattern(PatternBase):
"""Literal files or directories listed on the command line """Literal files or directories listed on the command line
for some operations (e.g. extract, but not create). for some operations (e.g. extract, but not create).
If a directory is specified, all paths that start with that If a directory is specified, all paths that start with that
@ -346,7 +346,7 @@ class IncludePattern(PatternBase):
return (path + os.path.sep).startswith(self.pattern) return (path + os.path.sep).startswith(self.pattern)
class ExcludePattern(PatternBase): class FnmatchPattern(PatternBase):
"""Shell glob patterns to exclude. A trailing slash means to """Shell glob patterns to exclude. A trailing slash means to
exclude the contents of a directory, but not the directory itself. exclude the contents of a directory, but not the directory itself.
""" """
@ -366,7 +366,7 @@ class ExcludePattern(PatternBase):
return (self.regex.match(path + os.path.sep) is not None) return (self.regex.match(path + os.path.sep) is not None)
class ExcludeRegex(PatternBase): class RegexPattern(PatternBase):
"""Regular expression to exclude. """Regular expression to exclude.
""" """
def _prepare(self, pattern): def _prepare(self, pattern):
@ -383,8 +383,8 @@ class ExcludeRegex(PatternBase):
_DEFAULT_PATTERN_STYLE = "fm" _DEFAULT_PATTERN_STYLE = "fm"
_PATTERN_STYLES = { _PATTERN_STYLES = {
"fm": ExcludePattern, "fm": FnmatchPattern,
"re": ExcludeRegex, "re": RegexPattern,
} }

View file

@ -9,8 +9,8 @@ import sys
import msgpack import msgpack
import msgpack.fallback import msgpack.fallback
from ..helpers import adjust_patterns, exclude_path, Location, format_file_size, format_timedelta, IncludePattern, ExcludePattern, make_path_safe, \ from ..helpers import adjust_patterns, exclude_path, Location, format_file_size, format_timedelta, PathPrefixPattern, FnmatchPattern, make_path_safe, \
prune_within, prune_split, get_cache_dir, Statistics, is_slow_msgpack, yes, ExcludeRegex, \ prune_within, prune_split, get_cache_dir, Statistics, is_slow_msgpack, yes, RegexPattern, \
StableDict, int_to_bigint, bigint_to_int, parse_timestamp, CompressionSpec, ChunkerParams, \ StableDict, int_to_bigint, bigint_to_int, parse_timestamp, CompressionSpec, ChunkerParams, \
ProgressIndicatorPercent, ProgressIndicatorEndless, load_excludes, parse_pattern ProgressIndicatorPercent, ProgressIndicatorEndless, load_excludes, parse_pattern
from . import BaseTestCase, environment_variable, FakeInputs from . import BaseTestCase, environment_variable, FakeInputs
@ -193,7 +193,7 @@ def test_patterns(paths, excludes, expected):
'/var/log/messages', '/var/log/dmesg', '/var/log/messages', '/var/log/dmesg',
] ]
check_patterns(files, paths, [ExcludePattern(p) for p in excludes], expected) check_patterns(files, paths, [FnmatchPattern(p) for p in excludes], expected)
@pytest.mark.parametrize("paths, excludes, expected", [ @pytest.mark.parametrize("paths, excludes, expected", [
@ -218,7 +218,7 @@ def test_patterns_regex(paths, excludes, expected):
patterns = [] patterns = []
for i in excludes: for i in excludes:
pat = ExcludeRegex(i) pat = RegexPattern(i)
assert str(pat) == i assert str(pat) == i
assert pat.pattern == i assert pat.pattern == i
patterns.append(pat) patterns.append(pat)
@ -228,9 +228,9 @@ def test_patterns_regex(paths, excludes, expected):
def test_regex_pattern(): def test_regex_pattern():
# The forward slash must match the platform-specific path separator # The forward slash must match the platform-specific path separator
assert ExcludeRegex("^/$").match("/") assert RegexPattern("^/$").match("/")
assert ExcludeRegex("^/$").match(os.path.sep) assert RegexPattern("^/$").match(os.path.sep)
assert not ExcludeRegex(r"^\\$").match("/") assert not RegexPattern(r"^\\$").match("/")
def use_normalized_unicode(): def use_normalized_unicode():
@ -238,9 +238,9 @@ def use_normalized_unicode():
def _make_test_patterns(pattern): def _make_test_patterns(pattern):
return [IncludePattern(pattern), return [PathPrefixPattern(pattern),
ExcludePattern(pattern), FnmatchPattern(pattern),
ExcludeRegex("^{}/foo$".format(pattern)), RegexPattern("^{}/foo$".format(pattern)),
] ]
@ -311,23 +311,23 @@ def test_patterns_from_file(tmpdir, lines, expected):
@pytest.mark.parametrize("pattern, cls", [ @pytest.mark.parametrize("pattern, cls", [
("", ExcludePattern), ("", FnmatchPattern),
# Default style # Default style
("*", ExcludePattern), ("*", FnmatchPattern),
("/data/*", ExcludePattern), ("/data/*", FnmatchPattern),
# fnmatch style # fnmatch style
("fm:", ExcludePattern), ("fm:", FnmatchPattern),
("fm:*", ExcludePattern), ("fm:*", FnmatchPattern),
("fm:/data/*", ExcludePattern), ("fm:/data/*", FnmatchPattern),
("fm:fm:/data/*", ExcludePattern), ("fm:fm:/data/*", FnmatchPattern),
# Regular expression # Regular expression
("re:", ExcludeRegex), ("re:", RegexPattern),
("re:.*", ExcludeRegex), ("re:.*", RegexPattern),
("re:^/something/", ExcludeRegex), ("re:^/something/", RegexPattern),
("re:re:^/something/", ExcludeRegex), ("re:re:^/something/", RegexPattern),
]) ])
def test_parse_pattern(pattern, cls): def test_parse_pattern(pattern, cls):
assert isinstance(parse_pattern(pattern), cls) assert isinstance(parse_pattern(pattern), cls)