-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
70 lines (53 loc) · 1.67 KB
/
main.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import fileinput
import importlib
import operator
import traceback
from functools import reduce
from typing import Any
import json
import yaml
class Config:
name: str
filter: dict[str, Any]
callables: dict[str, dict[str, Any]]
def __init__(self, name, filter, callables, **kwargs):
self.name = name
self.filter = filter
self.callables = callables
def __repr__(self):
return f"{self.name} {self.filter} {self.callables}"
def find(element, data):
keys = element.split(".")
rv = data
for key in keys:
if key in rv:
rv = rv[key]
else:
return
return rv
def parse(configs: dict[str, list[Config]], event: dict) -> None:
event_type = event.get("type", "")
for conf in configs.get(event_type, []):
if conf.filter:
for k, v in conf.filter.items():
if find(k, event) != v:
return
if conf.callables:
for k, v in conf.callables.items():
module = ".".join(k.split(".")[:-1])
function = k.split(".")[-1]
mod = importlib.import_module(module)
try:
getattr(mod, function)(event, **v)
except Exception as e:
print(traceback.format_exc())
if __name__ == "__main__":
configs: dict[str, list[Config]] = {}
with open("config.yaml", "r") as f:
conf = yaml.load(f, yaml.Loader)
for c in conf:
configs.setdefault(c.get("type"), []).append(Config(**c))
for line in fileinput.input():
print(line)
event = json.loads(line)
parse(configs, event)