From c53cd6eec74b433225981779f0cf5c98ead10ac7 Mon Sep 17 00:00:00 2001 From: Maciej Urbanski Date: Thu, 9 Nov 2023 17:30:39 +0100 Subject: [PATCH] fix ScanPoliciesManager support for compiled regexes --- CHANGELOG.md | 3 +++ b2sdk/scan/policies.py | 6 ++++-- test/unit/scan/test_scan_policies.py | 12 ++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03028df24..6b3a6531a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed +* Fix ScanPoliciesManager support for compiled regexes + ### Infrastructure * Fix readthedocs build by updating to v2 configuration schema * Fix spellcheck erroring out on LICENSE file diff --git a/b2sdk/scan/policies.py b/b2sdk/scan/policies.py index e6a053965..349a76fda 100644 --- a/b2sdk/scan/policies.py +++ b/b2sdk/scan/policies.py @@ -43,7 +43,7 @@ def matches(self, s): return any(c.match(s) is not None for c in self._compiled_list) -def convert_dir_regex_to_dir_prefix_regex(dir_regex): +def convert_dir_regex_to_dir_prefix_regex(dir_regex: str | re.Pattern) -> str: """ The patterns used to match directory names (and file names) are allowed to match a prefix of the name. This 'feature' was unintentional, but is @@ -65,8 +65,10 @@ def convert_dir_regex_to_dir_prefix_regex(dir_regex): either the regex ends in '$' or does not. :param dir_regex: a regular expression string or literal - :type dir_regex: str + :return: a regular expression string which matches the directory prefix """ + if isinstance(dir_regex, re.Pattern): + dir_regex = dir_regex.pattern if dir_regex.endswith('$'): return dir_regex[:-1] + r'/' else: diff --git a/test/unit/scan/test_scan_policies.py b/test/unit/scan/test_scan_policies.py index fe79c9a4c..2c6cd2fc0 100644 --- a/test/unit/scan/test_scan_policies.py +++ b/test/unit/scan/test_scan_policies.py @@ -70,3 +70,15 @@ def test_illegal_timestamp(self, param, exception): } with pytest.raises(exception): ScanPoliciesManager(**kwargs) + + @pytest.mark.apiver(from_ver=2) + def test_re_pattern_argument_support(self): + kwargs = { + param: (re.compile(r".*"),) + for param in ( + "exclude_dir_regexes", + "exclude_file_regexes", + "include_file_regexes", + ) + } + ScanPoliciesManager(**kwargs)