-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🧑🏼💻 [feat/0.8] Rules: Fallback rule implementation (aka rules that will run only if no other rule was executed) #592
base: 0.8
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,15 +4,37 @@ | |
|
||
from harp_apps.rules.constants import DEFAULT_LEVELS, DEFAULT_RULES_LEVELS | ||
from harp_apps.rules.models.compilers import BaseRuleSetCompiler | ||
from harp_apps.rules.models.patterns import Pattern | ||
|
||
|
||
DEFAULT_RULE_MATCHER = Pattern("__default__") | ||
|
||
|
||
class Itemize: | ||
def __init__(self, seq) -> None: | ||
self._seq = seq | ||
|
||
def __iter__(self): | ||
try: | ||
return iter(self._seq.items()) | ||
except AttributeError: | ||
return iter(self._seq) | ||
|
||
|
||
def _match_level(rules, against): | ||
default = None | ||
has_match = False | ||
for pattern, rules in rules: | ||
if pattern == DEFAULT_RULE_MATCHER: | ||
default = rules | ||
continue | ||
|
||
if pattern.match(against): | ||
if hasattr(rules, "items"): | ||
yield from rules.items() | ||
else: | ||
yield from rules | ||
has_match = True | ||
return Itemize(rules) | ||
|
||
if not has_match and default: | ||
return Itemize(default) | ||
|
||
|
||
def _rules_as_human_dict(rules: dict, *, show_scripts=True): | ||
|
@@ -62,6 +84,9 @@ def match(self, *args): | |
""" | ||
Match the given arguments against the rules. Each argument must match a "level" in this ruleset. | ||
""" | ||
if not self.rules: | ||
return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is that related to the current patch or a preexisting bug ? Asking because I want to make sure I understand what have caused the change in behaviour if it worked previously. Anyway, can't do real harm to check, but probably the empty rules should be iterable anyway (empty iterator). More a note for myself here than anything else. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This guards against the |
||
|
||
if len(args) != len(self._levels): | ||
raise ValueError(f"Expected {len(self._levels)} arguments, got {len(args)}") | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I understand, this changes the behaviour of rules, allowing only one match to be made amongst one level (by returning instead of yielding from). The «itemize» object brings more confusion than clarity, on top of that.
the current test suite is a bit incomplete on the rules side, but catches an issue there (see
harp_apps.rules.tests.test_models_rulesets.test_multilevel
).