فهرست منبع

Use constants for pattern style prefixes

The prefix used for pattern styles should be kept together with the
respective style implementation.
Michael Hanselmann 10 سال پیش
والد
کامیت
1fa4d2e516
1فایلهای تغییر یافته به همراه16 افزوده شده و 7 حذف شده
  1. 16 7
      borg/helpers.py

+ 16 - 7
borg/helpers.py

@@ -316,6 +316,8 @@ def normalized(func):
 class PatternBase:
 class PatternBase:
     """Shared logic for inclusion/exclusion patterns.
     """Shared logic for inclusion/exclusion patterns.
     """
     """
+    PREFIX = NotImplemented
+
     def __init__(self, pattern):
     def __init__(self, pattern):
         self.pattern_orig = pattern
         self.pattern_orig = pattern
         self.match_count = 0
         self.match_count = 0
@@ -360,6 +362,8 @@ class PathPrefixPattern(PatternBase):
     If a directory is specified, all paths that start with that
     If a directory is specified, all paths that start with that
     path match as well.  A trailing slash makes no difference.
     path match as well.  A trailing slash makes no difference.
     """
     """
+    PREFIX = "pp"
+
     def _prepare(self, pattern):
     def _prepare(self, pattern):
         self.pattern = os.path.normpath(pattern).rstrip(os.path.sep) + os.path.sep
         self.pattern = os.path.normpath(pattern).rstrip(os.path.sep) + os.path.sep
 
 
@@ -371,6 +375,8 @@ 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.
     """
     """
+    PREFIX = "fm"
+
     def _prepare(self, pattern):
     def _prepare(self, pattern):
         if pattern.endswith(os.path.sep):
         if pattern.endswith(os.path.sep):
             pattern = os.path.normpath(pattern).rstrip(os.path.sep) + os.path.sep + '*' + os.path.sep
             pattern = os.path.normpath(pattern).rstrip(os.path.sep) + os.path.sep + '*' + os.path.sep
@@ -390,6 +396,8 @@ class FnmatchPattern(PatternBase):
 class RegexPattern(PatternBase):
 class RegexPattern(PatternBase):
     """Regular expression to exclude.
     """Regular expression to exclude.
     """
     """
+    PREFIX = "re"
+
     def _prepare(self, pattern):
     def _prepare(self, pattern):
         self.pattern = pattern
         self.pattern = pattern
         self.regex = re.compile(pattern)
         self.regex = re.compile(pattern)
@@ -402,11 +410,12 @@ class RegexPattern(PatternBase):
         return (self.regex.search(path) is not None)
         return (self.regex.search(path) is not None)
 
 
 
 
-_DEFAULT_PATTERN_STYLE = "fm"
-_PATTERN_STYLES = {
-        "fm": FnmatchPattern,
-        "re": RegexPattern,
-        }
+_PATTERN_STYLES = set([
+    FnmatchPattern,
+    RegexPattern,
+])
+
+_PATTERN_STYLE_BY_PREFIX = dict((i.PREFIX, i) for i in _PATTERN_STYLES)
 
 
 
 
 def parse_pattern(pattern):
 def parse_pattern(pattern):
@@ -415,9 +424,9 @@ def parse_pattern(pattern):
     if len(pattern) > 2 and pattern[2] == ":" and pattern[:2].isalnum():
     if len(pattern) > 2 and pattern[2] == ":" and pattern[:2].isalnum():
         (style, pattern) = (pattern[:2], pattern[3:])
         (style, pattern) = (pattern[:2], pattern[3:])
     else:
     else:
-        style = _DEFAULT_PATTERN_STYLE
+        style = FnmatchPattern.PREFIX
 
 
-    cls = _PATTERN_STYLES.get(style, None)
+    cls = _PATTERN_STYLE_BY_PREFIX.get(style, None)
 
 
     if cls is None:
     if cls is None:
         raise ValueError("Unknown pattern style: {}".format(style))
         raise ValueError("Unknown pattern style: {}".format(style))