Explorar o código

Fix patterns parsing

Pavel Andreev hai 6 meses
pai
achega
5d390d7953
Modificáronse 2 ficheiros con 10 adicións e 19 borrados
  1. 8 13
      borgmatic/actions/create.py
  2. 2 6
      tests/unit/actions/test_create.py

+ 8 - 13
borgmatic/actions/create.py

@@ -15,7 +15,7 @@ import borgmatic.hooks.dispatch
 logger = logging.getLogger(__name__)
 logger = logging.getLogger(__name__)
 
 
 
 
-def parse_pattern(pattern_line):
+def parse_pattern(pattern_line, default_style=borgmatic.borg.pattern.Pattern_style.NONE):
     '''
     '''
     Given a Borg pattern as a string, parse it into a borgmatic.borg.pattern.Pattern instance and
     Given a Borg pattern as a string, parse it into a borgmatic.borg.pattern.Pattern instance and
     return it.
     return it.
@@ -26,9 +26,10 @@ def parse_pattern(pattern_line):
         raise ValueError(f'Invalid pattern: {pattern_line}')
         raise ValueError(f'Invalid pattern: {pattern_line}')
 
 
     try:
     try:
-        (pattern_style, path) = remainder.split(':', maxsplit=1)
+        (parsed_pattern_style, path) = remainder.split(':', maxsplit=1)
+        pattern_style = borgmatic.borg.pattern.Pattern_style(parsed_pattern_style)
     except ValueError:
     except ValueError:
-        pattern_style = ''
+        pattern_style = default_style
         path = remainder
         path = remainder
 
 
     return borgmatic.borg.pattern.Pattern(
     return borgmatic.borg.pattern.Pattern(
@@ -60,11 +61,8 @@ def collect_patterns(config):
                 if pattern_line.strip()
                 if pattern_line.strip()
             )
             )
             + tuple(
             + tuple(
-                borgmatic.borg.pattern.Pattern(
-                    exclude_line.strip(),
-                    borgmatic.borg.pattern.Pattern_type.EXCLUDE,
-                    borgmatic.borg.pattern.Pattern_style.FNMATCH,
-                )
+                parse_pattern(f'{borgmatic.borg.pattern.Pattern_type.EXCLUDE.value} {
+                              exclude_line.strip()}', borgmatic.borg.pattern.Pattern_style.FNMATCH)
                 for exclude_line in config.get('exclude_patterns', ())
                 for exclude_line in config.get('exclude_patterns', ())
             )
             )
             + tuple(
             + tuple(
@@ -75,11 +73,8 @@ def collect_patterns(config):
                 if pattern_line.strip()
                 if pattern_line.strip()
             )
             )
             + tuple(
             + tuple(
-                borgmatic.borg.pattern.Pattern(
-                    exclude_line.strip(),
-                    borgmatic.borg.pattern.Pattern_type.EXCLUDE,
-                    borgmatic.borg.pattern.Pattern_style.FNMATCH,
-                )
+                parse_pattern(f'{borgmatic.borg.pattern.Pattern_type.EXCLUDE.value} {
+                    exclude_line.strip()}', borgmatic.borg.pattern.Pattern_style.FNMATCH)
                 for filename in config.get('exclude_from', ())
                 for filename in config.get('exclude_from', ())
                 for exclude_line in open(filename).readlines()
                 for exclude_line in open(filename).readlines()
                 if not exclude_line.lstrip().startswith('#')
                 if not exclude_line.lstrip().startswith('#')

+ 2 - 6
tests/unit/actions/test_create.py

@@ -47,9 +47,10 @@ def test_collect_patterns_parses_config_patterns():
 
 
 
 
 def test_collect_patterns_converts_exclude_patterns():
 def test_collect_patterns_converts_exclude_patterns():
-    assert module.collect_patterns({'exclude_patterns': ['/foo', '/bar']}) == (
+    assert module.collect_patterns({'exclude_patterns': ['/foo', '/bar', 'sh:**/baz']}) == (
         Pattern('/foo', Pattern_type.EXCLUDE, Pattern_style.FNMATCH),
         Pattern('/foo', Pattern_type.EXCLUDE, Pattern_style.FNMATCH),
         Pattern('/bar', Pattern_type.EXCLUDE, Pattern_style.FNMATCH),
         Pattern('/bar', Pattern_type.EXCLUDE, Pattern_style.FNMATCH),
+        Pattern('**/baz', Pattern_type.EXCLUDE, Pattern_style.SHELL),
     )
     )
 
 
 
 
@@ -88,11 +89,6 @@ def test_collect_patterns_reads_config_exclude_from_file():
     builtins.should_receive('open').with_args('file2.txt').and_return(
     builtins.should_receive('open').with_args('file2.txt').and_return(
         io.StringIO('/bar\n# comment\n\n   \n/baz')
         io.StringIO('/bar\n# comment\n\n   \n/baz')
     )
     )
-    flexmock(module).should_receive('parse_pattern').with_args('/bar').and_return(Pattern('/bar'))
-    flexmock(module).should_receive('parse_pattern').with_args('# comment').never()
-    flexmock(module).should_receive('parse_pattern').with_args('').never()
-    flexmock(module).should_receive('parse_pattern').with_args('   ').never()
-    flexmock(module).should_receive('parse_pattern').with_args('/baz').and_return(Pattern('/baz'))
 
 
     assert module.collect_patterns({'exclude_from': ['file1.txt', 'file2.txt']}) == (
     assert module.collect_patterns({'exclude_from': ['file1.txt', 'file2.txt']}) == (
         Pattern('/foo', Pattern_type.EXCLUDE, Pattern_style.FNMATCH),
         Pattern('/foo', Pattern_type.EXCLUDE, Pattern_style.FNMATCH),