From 384fd21726e9a015c8fa45f36e721476c8438d31 Mon Sep 17 00:00:00 2001 From: lucasheld Date: Wed, 7 Sep 2022 13:04:16 +0200 Subject: [PATCH] update scripts --- scripts/build_inputs.py | 78 +++++++++++++++++ scripts/build_models.py | 86 ++++++++++++++++--- scripts/build_monitor_types.py | 25 ++++++ scripts/build_notification_info_return.py | 18 ---- scripts/build_notifications.py | 30 ++----- ...arams_map_notification_provider_options.j2 | 10 --- ...arams_map_notification_provider_options.py | 40 --------- ...build_params_map_notification_providers.py | 12 --- scripts/monitor_type.py.j2 | 8 ++ scripts/notification_providers.py.j2 | 10 +++ scripts/requirenents.txt | 1 + scripts/utils.py | 26 ++++++ 12 files changed, 231 insertions(+), 113 deletions(-) create mode 100644 scripts/build_inputs.py create mode 100644 scripts/build_monitor_types.py delete mode 100644 scripts/build_notification_info_return.py delete mode 100644 scripts/build_params_map_notification_provider_options.j2 delete mode 100644 scripts/build_params_map_notification_provider_options.py delete mode 100644 scripts/build_params_map_notification_providers.py create mode 100644 scripts/monitor_type.py.j2 create mode 100644 scripts/utils.py diff --git a/scripts/build_inputs.py b/scripts/build_inputs.py new file mode 100644 index 0000000..3b7924d --- /dev/null +++ b/scripts/build_inputs.py @@ -0,0 +1,78 @@ +import glob +import re + +from utils import deduplicate_list, parse_vue_template + + +def parse_model_keys(content, object_name): + match = re.findall(object_name + r"\.[0-9a-zA-Z_$]+", content) + keys = [] + for m in match: + key = m.replace(object_name + ".", "") + keys.append(key) + keys = deduplicate_list(keys) + return keys + + +def parse_proxy_keys(): + content = parse_vue_template("uptime-kuma/src/components/ProxyDialog.vue") + keys = parse_model_keys(content, "proxy") + return keys + + +def parse_notification_keys(): + content = parse_vue_template("uptime-kuma/src/components/NotificationDialog.vue") + keys = parse_model_keys(content, "notification") + return keys + + +def parse_settings_keys(): + all_keys = [] + for path in glob.glob('uptime-kuma/src/components/settings/*'): + content = parse_vue_template(path) + keys = parse_model_keys(content, "settings") + all_keys.extend(keys) + all_keys = deduplicate_list(all_keys) + return all_keys + + +def parse_monitor_keys(): + content = parse_vue_template("uptime-kuma/src/pages/EditMonitor.vue") + keys = parse_model_keys(content, "monitor") + return keys + + +def parse_status_page_keys(): + all_keys = ["id"] + + content = parse_vue_template("uptime-kuma/src/pages/StatusPage.vue") + keys = parse_model_keys(content, "config") + all_keys.extend(keys) + + content = parse_vue_template("uptime-kuma/src/pages/ManageStatusPage.vue") + keys = parse_model_keys(content, "statusPage") + all_keys.extend(keys) + + all_keys = deduplicate_list(all_keys) + return all_keys + + +def main(): + proxy_keys = parse_proxy_keys() + print("proxy:", proxy_keys) + + notification_keys = parse_notification_keys() + print("notification:", notification_keys) + + settings_keys = parse_settings_keys() + print("settings:", settings_keys) + + monitor_keys = parse_monitor_keys() + print("monitor:", monitor_keys) + + status_page_keys = parse_status_page_keys() + print("status_page:", status_page_keys) + + +if __name__ == "__main__": + main() diff --git a/scripts/build_models.py b/scripts/build_models.py index a351e2e..a976f8b 100644 --- a/scripts/build_models.py +++ b/scripts/build_models.py @@ -1,8 +1,10 @@ import re from pprint import pprint +from utils import deduplicate_list -def parse_data_keys(data): + +def parse_json_keys(data): keys = [] for line in data.split("\n"): line = line.strip() @@ -17,19 +19,28 @@ def parse_data_keys(data): return keys +# def parse_object_keys(code, object_name): +# match = re.findall(object_name + r'\.[0-9a-zA-Z_$]+', code) +# keys = [] +# for m in match: +# key = m.replace(object_name + ".", "") +# keys.append(key) +# return list(set(keys)) + + def parse_heartbeat(): with open('uptime-kuma/server/model/heartbeat.js') as f: content = f.read() all_keys = [] match = re.search(r'toJSON\(\) {\s+return.*{([^}]+)}', content) data = match.group(1) - keys = parse_data_keys(data) + keys = parse_json_keys(data) all_keys.extend(keys) match = re.search(r'toPublicJSON\(\) {\s+return.*{([^}]+)}', content) data = match.group(1) - keys = parse_data_keys(data) + keys = parse_json_keys(data) all_keys.extend(keys) - all_keys = list(set(all_keys)) + all_keys = deduplicate_list(all_keys) return all_keys @@ -38,7 +49,7 @@ def parse_incident(): content = f.read() match = re.search(r'toPublicJSON\(\) {\s+return.*{([^}]+)}', content) data = match.group(1) - keys = parse_data_keys(data) + keys = parse_json_keys(data) return keys @@ -49,9 +60,10 @@ def parse_monitor(): matches = re.findall(r'data = {([^}]+)}', content) all_keys = [] for match in matches: - keys = parse_data_keys(match) + keys = parse_json_keys(match) keys = [i for i in keys if i != "...data"] all_keys.extend(keys) + all_keys = deduplicate_list(all_keys) return all_keys @@ -60,23 +72,54 @@ def parse_proxy(): content = f.read() match = re.search(r'toJSON\(\) {\s+return.*{([^}]+)}', content) data = match.group(1) - keys = parse_data_keys(data) + keys = parse_json_keys(data) return keys +# def parse_function(regex_name, content): +# match = re.search(regex_name, content) +# name = match.group(0) +# rest = "".join(content.split(name)[1:]) +# +# brackets = 0 +# opening_bracket_found = False +# code = "" +# for i in rest: +# code += i +# if i == "{": +# opening_bracket_found = True +# brackets += 1 +# if i == "}": +# opening_bracket_found = True +# brackets -= 1 +# if opening_bracket_found and brackets == 0: +# break +# return code + + +# # input (add, edit proxy) +# def parse_proxy2(): +# with open('uptime-kuma/server/proxy.js') as f: +# content = f.read() +# +# code = parse_function(r'async save\([^)]+\) ', content) +# keys = parse_object_keys(code, "proxy") +# return keys + + def parse_status_page(): with open('uptime-kuma/server/model/status_page.js') as f: content = f.read() all_keys = [] match = re.search(r'toJSON\(\) {\s+return.*{([^}]+)}', content) data = match.group(1) - keys = parse_data_keys(data) + keys = parse_json_keys(data) all_keys.extend(keys) match = re.search(r'toPublicJSON\(\) {\s+return.*{([^}]+)}', content) data = match.group(1) - keys = parse_data_keys(data) + keys = parse_json_keys(data) all_keys.extend(keys) - all_keys = list(set(all_keys)) + all_keys = deduplicate_list(all_keys) return all_keys @@ -85,16 +128,37 @@ def parse_tag(): content = f.read() match = re.search(r'toJSON\(\) {\s+return.*{([^}]+)}', content) data = match.group(1) - keys = parse_data_keys(data) + keys = parse_json_keys(data) return keys +print("heartbeat") pprint(parse_heartbeat()) +print("") + +print("incident") pprint(parse_incident()) +print("") + +print("monitor") pprint(parse_monitor()) +print("") + +print("proxy") pprint(parse_proxy()) +print("") + +# print("prox2") +# pprint(parse_proxy2()) +# print("") + +print("status page") pprint(parse_status_page()) +print("") + +print("tag") pprint(parse_tag()) +print("") # TODO: diff --git a/scripts/build_monitor_types.py b/scripts/build_monitor_types.py new file mode 100644 index 0000000..242136c --- /dev/null +++ b/scripts/build_monitor_types.py @@ -0,0 +1,25 @@ +from bs4 import BeautifulSoup + +from utils import parse_vue_template, write_to_file + + +def parse_monitor_types(): + content = parse_vue_template("uptime-kuma/src/pages/EditMonitor.vue") + + soup = BeautifulSoup(content, "html.parser") + select = soup.find("select", id="type") + options = select.find_all("option") + + types = [] + for o in options: + type_ = o.attrs["value"] + types.append(type_) + return types + + +monitor_types = parse_monitor_types() + +write_to_file( + "monitor_type.py.j2", "./../uptime_kuma_api/monitor_type.py", + monitor_types=monitor_types +) diff --git a/scripts/build_notification_info_return.py b/scripts/build_notification_info_return.py deleted file mode 100644 index 7c84dfb..0000000 --- a/scripts/build_notification_info_return.py +++ /dev/null @@ -1,18 +0,0 @@ -from uptime_kuma_api import notification_provider_options - - -def build_providers(): - providers = [] - for provider_enum in notification_provider_options: - provider = provider_enum.__dict__["_value_"] - providers.append(provider) - return providers - - -for provider in build_providers(): - options = notification_provider_options[provider] - for option in options: - print(f'{option}:') - print(f' description: {provider} provider option.') - print(f' returned: if type is {provider}') - print(' type: str') diff --git a/scripts/build_notifications.py b/scripts/build_notifications.py index b3cf684..6a64f27 100644 --- a/scripts/build_notifications.py +++ b/scripts/build_notifications.py @@ -1,19 +1,9 @@ import glob import re -from pprint import pprint -import jinja2 from bs4 import BeautifulSoup -from uptime_kuma_api import convert_from_socket, params_map_notification_provider_options - - -def deduplicate_list(l): - out = [] - for i in l: - if i not in out: - out.append(i) - return out +from utils import deduplicate_list, write_to_file def build_notification_providers(): @@ -61,18 +51,14 @@ def build_notification_provider_conditions(): 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_options, conditions) return conditions -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) +notification_providers = build_notification_providers() +notification_provider_conditions = build_notification_provider_conditions() -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) +write_to_file( + "notification_providers.py.j2", "./../uptime_kuma_api/notification_providers.py", + notification_providers=notification_providers, + notification_provider_conditions=notification_provider_conditions +) diff --git a/scripts/build_params_map_notification_provider_options.j2 b/scripts/build_params_map_notification_provider_options.j2 deleted file mode 100644 index 3e36090..0000000 --- a/scripts/build_params_map_notification_provider_options.j2 +++ /dev/null @@ -1,10 +0,0 @@ -params_map_notification_provider_options = { -{%- for provider in notification_provider_map %} - {%- set options = notification_provider_map[provider] %} - '{{ provider }}': { - {%- for key, value in options.items() %} - '{{ key }}': '{{ value }}', - {%- endfor %} - }, -{%- endfor %} -} diff --git a/scripts/build_params_map_notification_provider_options.py b/scripts/build_params_map_notification_provider_options.py deleted file mode 100644 index da8fc0b..0000000 --- a/scripts/build_params_map_notification_provider_options.py +++ /dev/null @@ -1,40 +0,0 @@ -import re -import os -import jinja2 - -from uptime_kuma_api import params_map_notification_providers, notification_provider_options - - -def build_notification_provider_map(): - params_map_notification_provider_options = {} - - for provider_sock, provider_py in params_map_notification_providers.items(): - options_sock = notification_provider_options[provider_sock] - - params_map_notification_provider_options[provider_py] = {} - for option in options_sock: - option_orig = option - - # for example for rocket_chat - prefix = os.path.commonprefix([o.lower() for o in options_sock] + [provider_py]) - option = option[len(prefix):] - - option = re.sub('([A-Z]+)', r'_\1', option).lower() - - # for example for smtp - if option.startswith(provider_py): - option = option[len(provider_py):] - - option = provider_py + "_" + option - option = option.replace("__", "_") - - params_map_notification_provider_options[provider_py][option_orig] = option - return params_map_notification_provider_options - - -notification_provider_map = build_notification_provider_map() - -env = jinja2.Environment(loader=jinja2.FileSystemLoader("./")) -template = env.get_template("build_params_map_notification_provider_options.j2") -rendered = template.render(notification_provider_map=notification_provider_map) -print(rendered) diff --git a/scripts/build_params_map_notification_providers.py b/scripts/build_params_map_notification_providers.py deleted file mode 100644 index 0d6f9ad..0000000 --- a/scripts/build_params_map_notification_providers.py +++ /dev/null @@ -1,12 +0,0 @@ -import re - -from uptime_kuma_api import notification_provider_options - - -params_map_notification_providers = {} - -for notification_provider in notification_provider_options: - provider_name_orig = notification_provider.__dict__["_value_"] - provider_name = re.sub('([A-Z]+)', r'_\1', provider_name_orig).lower().replace(".", "_").strip("_") - params_map_notification_providers[provider_name_orig] = provider_name -print(params_map_notification_providers) diff --git a/scripts/monitor_type.py.j2 b/scripts/monitor_type.py.j2 new file mode 100644 index 0000000..61b69d5 --- /dev/null +++ b/scripts/monitor_type.py.j2 @@ -0,0 +1,8 @@ +from enum import Enum + + +class MonitorType(str, Enum): +{%- for name in monitor_types %} + {{ name.upper() }} = "{{ name }}" +{%- endfor %} + diff --git a/scripts/notification_providers.py.j2 b/scripts/notification_providers.py.j2 index 74897bb..896f05d 100644 --- a/scripts/notification_providers.py.j2 +++ b/scripts/notification_providers.py.j2 @@ -19,3 +19,13 @@ notification_provider_options = { {%- endfor %} } +notification_provider_conditions = { + {%- for provider in notification_provider_conditions %} + "{{ provider }}": { + {%- for key, value in notification_provider_conditions[provider].items() %} + "{{ key }}": {{ value }}, + {%- endfor %} + }, + {%- endfor %} +} + diff --git a/scripts/requirenents.txt b/scripts/requirenents.txt index 769ef83..c2d8902 100644 --- a/scripts/requirenents.txt +++ b/scripts/requirenents.txt @@ -1 +1,2 @@ Jinja2==3.1.2 +BeautifulSoup4==4.11.1 diff --git a/scripts/utils.py b/scripts/utils.py new file mode 100644 index 0000000..fcf52d1 --- /dev/null +++ b/scripts/utils.py @@ -0,0 +1,26 @@ +import re +import jinja2 + + +def deduplicate_list(l): + out = [] + for i in l: + if i not in out: + out.append(i) + return out + + +def parse_vue_template(path): + with open(path) as f: + vue = f.read() + match = re.search(r'', vue, re.MULTILINE) + template = match.group(0) + return template + + +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)