-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuleParser.py
More file actions
38 lines (29 loc) · 1.11 KB
/
RuleParser.py
File metadata and controls
38 lines (29 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import json
from Rule import Rule
from Keywords import *
from RuleManager import RuleManager
class RuleParser:
filepath: str
def __init__(self, filepath) -> None:
self.filepath = filepath
def parse(self) -> RuleManager:
rule_manager = RuleManager()
with open(self.filepath) as f:
data = json.load(f)
rules = data[RULES]
for name in rules:
rule_json = rules[name]
rule: Rule = self._parse_rule(None, rule_json, name)
rule_manager.add(rule)
return rule_manager
def _parse_rule(self, parent: Rule | None, rule_json: dict, name: str) -> Rule:
tag = rule_json.get(TAG, "")
aliases = rule_json.get(ALIASES, [])
path: str = rule_json.get(PATH, "")
rule = Rule(name, tag, path, aliases, parent=parent)
# rule.resolve_placeholders()
children = rule_json.get(CHILD_RULES)
if children is not None:
for child_name in children:
rule.add_child(self._parse_rule(rule, children[child_name], child_name))
return rule