refactor(config): move loading of patterns from config to a function

This commit is contained in:
Rémi Alvergnat
2021-02-18 23:37:13 +01:00
parent 0823e37b38
commit 80b97a450f
2 changed files with 33 additions and 15 deletions
+31
View File
@@ -0,0 +1,31 @@
"""
Config module.
"""
from rebulk import Rebulk
_regex_prefix = 're:'
def load_config_patterns(rebulk: Rebulk, config: dict):
"""
Load patterns defined in given config.
:param rebulk: Rebulk builder to use.
:param config: dict containing pattern definition.
:return:
"""
for value, items in config.items():
patterns = items if isinstance(items, list) else [items]
for pattern in patterns:
if isinstance(pattern, dict):
kwargs = dict(pattern)
pattern = kwargs.pop('pattern')
else:
kwargs = {}
regex = kwargs.pop('regex', False)
if not regex and pattern.startswith(_regex_prefix):
regex = True
pattern = pattern[len(_regex_prefix):]
if regex:
rebulk.regex(pattern, value=value, **kwargs)
else:
rebulk.string(pattern, value=value, **kwargs)
+2 -15
View File
@@ -9,6 +9,7 @@ from rebulk import Rebulk
from rebulk.rules import Rule, RemoveMatch
from ..common.pattern import is_disabled
from ...config import load_config_patterns
from ...rules.common import seps, dash
from ...rules.common.validators import seps_before, seps_after
@@ -25,21 +26,7 @@ def streaming_service(config): # pylint: disable=too-many-statements,unused-arg
rebulk = rebulk.string_defaults(ignore_case=True).regex_defaults(flags=re.IGNORECASE, abbreviations=[dash])
rebulk.defaults(name='streaming_service', tags=['source-prefix'])
regex_prefix = 're:'
for value, items in config.items():
patterns = items if isinstance(items, list) else [items]
for pattern in patterns:
if isinstance(pattern, dict):
kwargs = dict(pattern)
pattern = kwargs.pop('pattern')
else:
kwargs = {}
regex = kwargs.pop('regex', False)
if regex or pattern.startswith(regex_prefix):
rebulk.regex(pattern[len(regex_prefix):], value=value, **kwargs)
else:
rebulk.string(pattern, value=value, **kwargs)
load_config_patterns(rebulk, config)
rebulk.rules(ValidateStreamingService)