mirror of
https://github.com/borgbackup/borg.git
synced 2024-12-27 02:08:54 +00:00
Merge pull request #2338 from ThomasWaldmann/normalize-once-only
optimize / simplify path normalization
This commit is contained in:
commit
f0a04a9db2
1 changed files with 16 additions and 26 deletions
|
@ -467,29 +467,18 @@ def add_inclexcl(self, patterns):
|
||||||
self._items.extend(patterns)
|
self._items.extend(patterns)
|
||||||
|
|
||||||
def match(self, path):
|
def match(self, path):
|
||||||
|
path = normalize_path(path)
|
||||||
for (pattern, value) in self._items:
|
for (pattern, value) in self._items:
|
||||||
if pattern.match(path):
|
if pattern.match(path, normalize=False):
|
||||||
return value
|
return value
|
||||||
|
|
||||||
return self.fallback
|
return self.fallback
|
||||||
|
|
||||||
|
|
||||||
def normalized(func):
|
def normalize_path(path):
|
||||||
""" Decorator for the Pattern match methods, returning a wrapper that
|
"""normalize paths for MacOS (but do nothing on other platforms)"""
|
||||||
normalizes OSX paths to match the normalized pattern on OSX, and
|
# HFS+ converts paths to a canonical form, so users shouldn't be required to enter an exact match.
|
||||||
returning the original method on other platforms"""
|
# Windows and Unix filesystems allow different forms, so users always have to enter an exact match.
|
||||||
@wraps(func)
|
return unicodedata.normalize('NFD', path) if sys.platform == 'darwin' else path
|
||||||
def normalize_wrapper(self, path):
|
|
||||||
return func(self, unicodedata.normalize("NFD", path))
|
|
||||||
|
|
||||||
if sys.platform in ('darwin',):
|
|
||||||
# HFS+ converts paths to a canonical form, so users shouldn't be
|
|
||||||
# required to enter an exact match
|
|
||||||
return normalize_wrapper
|
|
||||||
else:
|
|
||||||
# Windows and Unix filesystems allow different forms, so users
|
|
||||||
# always have to enter an exact match
|
|
||||||
return func
|
|
||||||
|
|
||||||
|
|
||||||
class PatternBase:
|
class PatternBase:
|
||||||
|
@ -500,19 +489,20 @@ class PatternBase:
|
||||||
def __init__(self, pattern):
|
def __init__(self, pattern):
|
||||||
self.pattern_orig = pattern
|
self.pattern_orig = pattern
|
||||||
self.match_count = 0
|
self.match_count = 0
|
||||||
|
pattern = normalize_path(pattern)
|
||||||
if sys.platform in ('darwin',):
|
|
||||||
pattern = unicodedata.normalize("NFD", pattern)
|
|
||||||
|
|
||||||
self._prepare(pattern)
|
self._prepare(pattern)
|
||||||
|
|
||||||
@normalized
|
def match(self, path, normalize=True):
|
||||||
def match(self, path):
|
"""match the given path against this pattern.
|
||||||
matches = self._match(path)
|
|
||||||
|
|
||||||
|
If normalize is True (default), the path will get normalized using normalize_path(),
|
||||||
|
otherwise it is assumed that it already is normalized using that function.
|
||||||
|
"""
|
||||||
|
if normalize:
|
||||||
|
path = normalize_path(path)
|
||||||
|
matches = self._match(path)
|
||||||
if matches:
|
if matches:
|
||||||
self.match_count += 1
|
self.match_count += 1
|
||||||
|
|
||||||
return matches
|
return matches
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
|
Loading…
Reference in a new issue