Make loading templates and rules cleaner
This commit is contained in:
parent
d0e9aad1ff
commit
0fcf7a8319
1 changed files with 19 additions and 10 deletions
27
reactbot.py
27
reactbot.py
|
@ -137,19 +137,28 @@ class ReactBot(Plugin):
|
||||||
self.templates = {}
|
self.templates = {}
|
||||||
self.on_external_config_update()
|
self.on_external_config_update()
|
||||||
|
|
||||||
def on_external_config_update(self) -> None:
|
@staticmethod
|
||||||
self.config.load_and_update()
|
def _parse_variables(data: Dict[str, Any]) -> Dict[str, JinjaTemplate]:
|
||||||
self.templates = {name: Template(type=EventType.find(tpl.get("type", "m.room.message")),
|
return {name: JinjaTemplate(var_tpl) for name, var_tpl
|
||||||
variables={name: JinjaTemplate(var_tpl) for name, var_tpl
|
in data.get("variables", {}).items()}
|
||||||
in tpl.get("variables", {}).items()},
|
|
||||||
|
def _make_template(self, tpl: Dict[str, Any]) -> Template:
|
||||||
|
return Template(type=EventType.find(tpl.get("type", "m.room.message")),
|
||||||
|
variables=self._parse_variables(tpl),
|
||||||
content=tpl.get("content", {})).init()
|
content=tpl.get("content", {})).init()
|
||||||
for name, tpl in self.config["templates"].items()}
|
|
||||||
self.rules = {name: Rule(rooms=set(rule.get("rooms", [])),
|
def _make_rule(self, rule: Dict[str, Any]) -> Rule:
|
||||||
|
return Rule(rooms=set(rule.get("rooms", [])),
|
||||||
matches=[re.compile(match) for match in rule.get("matches")],
|
matches=[re.compile(match) for match in rule.get("matches")],
|
||||||
type=EventType.find(rule["type"]) if "type" in rule else None,
|
type=EventType.find(rule["type"]) if "type" in rule else None,
|
||||||
template=self.templates[rule["template"]],
|
template=self.templates[rule["template"]],
|
||||||
variables={name: JinjaTemplate(template) for name, template
|
variables=self._parse_variables(rule))
|
||||||
in rule.get("variables", {}).items()})
|
|
||||||
|
def on_external_config_update(self) -> None:
|
||||||
|
self.config.load_and_update()
|
||||||
|
self.templates = {name: self._make_template(tpl)
|
||||||
|
for name, tpl in self.config["templates"].items()}
|
||||||
|
self.rules = {name: self._make_rule(rule)
|
||||||
for name, rule in self.config["rules"].items()}
|
for name, rule in self.config["rules"].items()}
|
||||||
|
|
||||||
@event.on(EventType.ROOM_MESSAGE)
|
@event.on(EventType.ROOM_MESSAGE)
|
||||||
|
|
Loading…
Reference in a new issue