-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rule storage: Apply AbstractRepository pattern
Introduce a AbstractRepository as interface to the rules database and implement a YML storage that implements it. Additionally, a FakeStorage implements that interface to test diversion related code, without I/O calls and side effects. It keeps the rules in-memory for the test duration. Related #2675
- Loading branch information
Showing
4 changed files
with
102 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from pathlib import Path | ||
from typing import Dict | ||
|
||
import yaml | ||
|
||
|
||
class YmlRuleStorage: | ||
def __init__(self, path: Path): | ||
self._config_path = path | ||
|
||
def save(self, rules: Dict[str, str]) -> None: | ||
# This is a trick to show str/float/int lists in-line (inspired by https://stackoverflow.com/a/14001707) | ||
class inline_list(list): | ||
pass | ||
|
||
def blockseq_rep(dumper, data): | ||
return dumper.represent_sequence("tag:yaml.org,2002:seq", data, flow_style=True) | ||
|
||
yaml.add_representer(inline_list, blockseq_rep) | ||
format_settings = { | ||
"encoding": "utf-8", | ||
"explicit_start": True, | ||
"explicit_end": True, | ||
"default_flow_style": False, | ||
} | ||
with open(self._config_path, "w") as f: | ||
f.write("%YAML 1.3\n") # Write version manually | ||
yaml.dump_all(rules, f, **format_settings) | ||
|
||
def load(self) -> list: | ||
with open(self._config_path) as config_file: | ||
plain_rules = list(yaml.safe_load_all(config_file)) | ||
return plain_rules | ||
|
||
|
||
class FakeRuleStorage: | ||
def __init__(self, rules=None): | ||
if rules is None: | ||
self._rules = {} | ||
else: | ||
self._rules = rules | ||
|
||
def save(self, rules: dict) -> None: | ||
self._rules = rules | ||
|
||
def load(self) -> dict: | ||
return self._rules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters