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:
parent
2c7ab8595d
commit
3a39ddbd83
3 changed files with 31 additions and 31 deletions
|
@ -17,7 +17,7 @@ import traceback
|
|||
|
||||
from . import __version__
|
||||
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, \
|
||||
Manifest, remove_surrogates, update_excludes, format_archive, check_extension_modules, Statistics, \
|
||||
dir_is_tagged, bigint_to_int, ChunkerParams, CompressionSpec, is_slow_msgpack, yes, sysinfo, \
|
||||
|
@ -314,7 +314,7 @@ class Archiver:
|
|||
while dirs:
|
||||
archive.extract_item(dirs.pop(-1))
|
||||
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)
|
||||
return self.exit_code
|
||||
|
||||
|
|
|
@ -259,7 +259,7 @@ def update_excludes(args):
|
|||
|
||||
def adjust_patterns(paths, excludes):
|
||||
if paths:
|
||||
return (excludes or []) + [IncludePattern(path) for path in paths] + [ExcludePattern('*')]
|
||||
return (excludes or []) + [PathPrefixPattern(path) for path in paths] + [FnmatchPattern('*')]
|
||||
else:
|
||||
return excludes
|
||||
|
||||
|
@ -270,7 +270,7 @@ def exclude_path(path, patterns):
|
|||
"""
|
||||
for pattern in (patterns or []):
|
||||
if pattern.match(path):
|
||||
return isinstance(pattern, (ExcludePattern, ExcludeRegex))
|
||||
return isinstance(pattern, (FnmatchPattern, RegexPattern))
|
||||
return False
|
||||
|
||||
|
||||
|
@ -326,14 +326,14 @@ class PatternBase:
|
|||
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
|
||||
# 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
|
||||
# the path before matching.
|
||||
|
||||
|
||||
class IncludePattern(PatternBase):
|
||||
class PathPrefixPattern(PatternBase):
|
||||
"""Literal files or directories listed on the command line
|
||||
for some operations (e.g. extract, but not create).
|
||||
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)
|
||||
|
||||
|
||||
class ExcludePattern(PatternBase):
|
||||
class FnmatchPattern(PatternBase):
|
||||
"""Shell glob patterns to exclude. A trailing slash means to
|
||||
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)
|
||||
|
||||
|
||||
class ExcludeRegex(PatternBase):
|
||||
class RegexPattern(PatternBase):
|
||||
"""Regular expression to exclude.
|
||||
"""
|
||||
def _prepare(self, pattern):
|
||||
|
@ -383,8 +383,8 @@ class ExcludeRegex(PatternBase):
|
|||
|
||||
_DEFAULT_PATTERN_STYLE = "fm"
|
||||
_PATTERN_STYLES = {
|
||||
"fm": ExcludePattern,
|
||||
"re": ExcludeRegex,
|
||||
"fm": FnmatchPattern,
|
||||
"re": RegexPattern,
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -9,8 +9,8 @@ import sys
|
|||
import msgpack
|
||||
import msgpack.fallback
|
||||
|
||||
from ..helpers import adjust_patterns, exclude_path, Location, format_file_size, format_timedelta, IncludePattern, ExcludePattern, make_path_safe, \
|
||||
prune_within, prune_split, get_cache_dir, Statistics, is_slow_msgpack, yes, ExcludeRegex, \
|
||||
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, RegexPattern, \
|
||||
StableDict, int_to_bigint, bigint_to_int, parse_timestamp, CompressionSpec, ChunkerParams, \
|
||||
ProgressIndicatorPercent, ProgressIndicatorEndless, load_excludes, parse_pattern
|
||||
from . import BaseTestCase, environment_variable, FakeInputs
|
||||
|
@ -193,7 +193,7 @@ def test_patterns(paths, excludes, expected):
|
|||
'/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", [
|
||||
|
@ -218,7 +218,7 @@ def test_patterns_regex(paths, excludes, expected):
|
|||
patterns = []
|
||||
|
||||
for i in excludes:
|
||||
pat = ExcludeRegex(i)
|
||||
pat = RegexPattern(i)
|
||||
assert str(pat) == i
|
||||
assert pat.pattern == i
|
||||
patterns.append(pat)
|
||||
|
@ -228,9 +228,9 @@ def test_patterns_regex(paths, excludes, expected):
|
|||
|
||||
def test_regex_pattern():
|
||||
# The forward slash must match the platform-specific path separator
|
||||
assert ExcludeRegex("^/$").match("/")
|
||||
assert ExcludeRegex("^/$").match(os.path.sep)
|
||||
assert not ExcludeRegex(r"^\\$").match("/")
|
||||
assert RegexPattern("^/$").match("/")
|
||||
assert RegexPattern("^/$").match(os.path.sep)
|
||||
assert not RegexPattern(r"^\\$").match("/")
|
||||
|
||||
|
||||
def use_normalized_unicode():
|
||||
|
@ -238,9 +238,9 @@ def use_normalized_unicode():
|
|||
|
||||
|
||||
def _make_test_patterns(pattern):
|
||||
return [IncludePattern(pattern),
|
||||
ExcludePattern(pattern),
|
||||
ExcludeRegex("^{}/foo$".format(pattern)),
|
||||
return [PathPrefixPattern(pattern),
|
||||
FnmatchPattern(pattern),
|
||||
RegexPattern("^{}/foo$".format(pattern)),
|
||||
]
|
||||
|
||||
|
||||
|
@ -311,23 +311,23 @@ def test_patterns_from_file(tmpdir, lines, expected):
|
|||
|
||||
|
||||
@pytest.mark.parametrize("pattern, cls", [
|
||||
("", ExcludePattern),
|
||||
("", FnmatchPattern),
|
||||
|
||||
# Default style
|
||||
("*", ExcludePattern),
|
||||
("/data/*", ExcludePattern),
|
||||
("*", FnmatchPattern),
|
||||
("/data/*", FnmatchPattern),
|
||||
|
||||
# fnmatch style
|
||||
("fm:", ExcludePattern),
|
||||
("fm:*", ExcludePattern),
|
||||
("fm:/data/*", ExcludePattern),
|
||||
("fm:fm:/data/*", ExcludePattern),
|
||||
("fm:", FnmatchPattern),
|
||||
("fm:*", FnmatchPattern),
|
||||
("fm:/data/*", FnmatchPattern),
|
||||
("fm:fm:/data/*", FnmatchPattern),
|
||||
|
||||
# Regular expression
|
||||
("re:", ExcludeRegex),
|
||||
("re:.*", ExcludeRegex),
|
||||
("re:^/something/", ExcludeRegex),
|
||||
("re:re:^/something/", ExcludeRegex),
|
||||
("re:", RegexPattern),
|
||||
("re:.*", RegexPattern),
|
||||
("re:^/something/", RegexPattern),
|
||||
("re:re:^/something/", RegexPattern),
|
||||
])
|
||||
def test_parse_pattern(pattern, cls):
|
||||
assert isinstance(parse_pattern(pattern), cls)
|
||||
|
|
Loading…
Add table
Reference in a new issue