uptime-kuma-api/scripts/build_notifications.py

79 lines
2.5 KiB
Python
Raw Normal View History

2022-07-02 16:00:54 +02:00
import glob
import re
2022-07-10 18:07:11 +02:00
from pprint import pprint
2022-07-02 16:00:54 +02:00
import jinja2
2022-07-10 18:07:11 +02:00
from bs4 import BeautifulSoup
2022-07-02 16:00:54 +02:00
2022-07-10 18:07:11 +02:00
from uptime_kuma_api import convert_from_socket, params_map_notification_provider
2022-07-02 16:00:54 +02:00
def deduplicate_list(l):
out = []
for i in l:
if i not in out:
out.append(i)
return out
2022-07-10 18:07:11 +02:00
def build_notification_providers():
providers = []
for path in glob.glob('uptime-kuma/server/notification-providers/*'):
with open(path) as f:
content = f.read()
match = re.search(r'class [^ ]+ extends NotificationProvider {', content)
if match:
match = re.search(r'name = "([^"]+)";', content)
name = match.group(1)
2022-07-02 16:00:54 +02:00
2022-07-10 18:07:11 +02:00
inputs = re.findall(r'notification\.([^ ,.;})\]]+)', content)
inputs = deduplicate_list(inputs)
inputs = [i.strip() for i in inputs]
2022-07-02 16:00:54 +02:00
2022-07-10 18:07:11 +02:00
providers.append({
"name": name,
"inputs": inputs,
})
return providers
2022-07-02 16:00:54 +02:00
2022-07-10 18:07:11 +02:00
def build_notification_provider_conditions():
conditions = {}
for path in glob.glob('uptime-kuma/src/components/notifications/*'):
if path.endswith("index.js"):
continue
with open(path) as f:
content = f.read()
match = re.search(r'<template>[\s\S]+</template>', content, re.MULTILINE)
html = match.group(0)
soup = BeautifulSoup(html, "html.parser")
inputs = soup.find_all("input")
for input in inputs:
condition = {}
attrs = input.attrs
v_model = attrs.get("v-model")
min_ = attrs.get("min")
max_ = attrs.get("max")
if min_:
condition["min"] = int(min_)
if max_:
condition["max"] = int(max_)
param_name = re.match(r'\$parent.notification.(.*)$', v_model).group(1)
if condition:
conditions[param_name] = condition
conditions = convert_from_socket(params_map_notification_provider, conditions)
return conditions
2022-07-02 16:00:54 +02:00
def write_to_file(template, destination, **kwargs):
env = jinja2.Environment(loader=jinja2.FileSystemLoader("./"))
template = env.get_template(template)
rendered = template.render(**kwargs)
with open(destination, "w") as f:
f.write(rendered)
2022-07-10 18:07:11 +02:00
conditions = build_notification_provider_conditions()
pprint(conditions)
# notification_providers = build_notification_providers()
# write_to_file("notification_providers.py.j2", "./../uptimekumaapi/notification_providers.py", notification_providers=notification_providers)