Make loading templates and rules cleaner

This commit is contained in:
Tulir Asokan 2019-06-23 02:12:09 +03:00
parent d0e9aad1ff
commit 0fcf7a8319

View file

@ -137,19 +137,28 @@ class ReactBot(Plugin):
self.templates = {}
self.on_external_config_update()
def on_external_config_update(self) -> None:
self.config.load_and_update()
self.templates = {name: Template(type=EventType.find(tpl.get("type", "m.room.message")),
variables={name: JinjaTemplate(var_tpl) for name, var_tpl
in tpl.get("variables", {}).items()},
@staticmethod
def _parse_variables(data: Dict[str, Any]) -> Dict[str, JinjaTemplate]:
return {name: JinjaTemplate(var_tpl) for name, var_tpl
in data.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()
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")],
type=EventType.find(rule["type"]) if "type" in rule else None,
template=self.templates[rule["template"]],
variables={name: JinjaTemplate(template) for name, template
in rule.get("variables", {}).items()})
variables=self._parse_variables(rule))
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()}
@event.on(EventType.ROOM_MESSAGE)