mirror of
https://github.com/chatmail/core.git
synced 2026-04-02 05:22:14 +03:00
add update.py that takes a folder with yaml-md-files as argument
This commit is contained in:
107
src/provider/update.py
Executable file
107
src/provider/update.py
Executable file
@@ -0,0 +1,107 @@
|
||||
#!/usr/bin/env python3
|
||||
# if the yaml import fails, run "pip install pyyaml"
|
||||
|
||||
import sys
|
||||
import os
|
||||
import yaml
|
||||
|
||||
out_all = ""
|
||||
out_domains = ""
|
||||
count = 1
|
||||
|
||||
|
||||
def cleanstr(s):
|
||||
s = s.strip()
|
||||
s = s.replace("\n", " ")
|
||||
s = s.replace("\\", "\\\\")
|
||||
s = s.replace("\"", "\\\"")
|
||||
return s
|
||||
|
||||
|
||||
def file2url(f):
|
||||
f = f[f.rindex("/")+1:]
|
||||
f = f.replace(".md", "")
|
||||
f = f.replace(".", "-")
|
||||
return "https://providers.delta.chat/" + f
|
||||
|
||||
|
||||
def process_data(data, file):
|
||||
global out_all, out_domains, count
|
||||
status = data.get("state", "")
|
||||
out_provider = " static ref P" + str(count) + ": Provider = Provider {\n"
|
||||
if status == "OK" or status == "PREPARATION" or status == "BROKEN":
|
||||
out_provider += " status: Status::" + status + ",\n"
|
||||
else:
|
||||
print("ERROR: bad state")
|
||||
return
|
||||
|
||||
comment = ""
|
||||
if not "domains" in data:
|
||||
print("ERROR: no domains found")
|
||||
return
|
||||
for domain in data["domains"]:
|
||||
out_domains += " (\"" + domain + "\", &*P" + str(count) + "),\n"
|
||||
comment += domain + ", "
|
||||
|
||||
out_server = ""
|
||||
if "server" in data:
|
||||
for s in data["server"]:
|
||||
hostname = cleanstr(s.get("hostname", ""))
|
||||
port = int(s.get("port", ""))
|
||||
if hostname == "" or hostname.count(".") < 1 or port <= 0:
|
||||
print("ERROR: bad hostname or port")
|
||||
return
|
||||
out_server += " Server {"
|
||||
out_server += " protocol: " + s.get("type", "<bad type>").upper() + ","
|
||||
out_server += " socket: " + s.get("socket", "<bad socket>").upper() + ","
|
||||
out_server += " hostname: \"" + hostname + "\","
|
||||
out_server += " port: " + str(port) + ","
|
||||
out_server += " username_pattern: " + s.get("username_pattern", "EMAIL").upper()
|
||||
out_server += " }\n"
|
||||
|
||||
out_provider += " before_login_hint: \"" + cleanstr(data.get("before_login_hint", "")) + "\",\n"
|
||||
out_provider += " after_login_hint: \"" + cleanstr(data.get("after_login_hint", "")) + "\",\n"
|
||||
out_provider += " overview_page: \"" + file2url(file) + "\",\n"
|
||||
out_provider += " server: vec![\n" + out_server + " ],\n"
|
||||
out_provider += " };\n\n"
|
||||
|
||||
out_all += " // " + comment.strip(", ") + "\n" + out_provider
|
||||
|
||||
|
||||
def process_file(file):
|
||||
global count
|
||||
print("processing file: " + file)
|
||||
with open(file) as f:
|
||||
# load_all() loads "---"-separated yamls -
|
||||
# by coincidence, this is also the frontmatter separator :)
|
||||
data = next(yaml.load_all(f, Loader=yaml.SafeLoader))
|
||||
process_data(data, file)
|
||||
count += 1
|
||||
|
||||
|
||||
def process_dir(dir):
|
||||
print("processing directory: " + dir)
|
||||
files = [f for f in os.listdir(dir) if f.endswith(".md")]
|
||||
for f in files:
|
||||
process_file(os.path.join(dir, f))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) < 2:
|
||||
raise SystemExit("need argument: path to dir with md/yaml files")
|
||||
|
||||
out_all = ("// file generated by src/provider/update.py\n\n"
|
||||
"use crate::provider::*;\n"
|
||||
"use crate::provider::Protocol::*;\n"
|
||||
"use crate::provider::Socket::*;\n"
|
||||
"use crate::provider::UsernamePattern::*;\n"
|
||||
"use std::collections::HashMap;\n\n"
|
||||
"lazy_static::lazy_static! {\n\n")
|
||||
|
||||
process_dir(sys.argv[1])
|
||||
|
||||
out_all += " pub static ref PROVIDER_DATA: HashMap<&'static str, &'static Provider> = [\n"
|
||||
out_all += out_domains;
|
||||
out_all += " ].iter().copied().collect();\n}\n"
|
||||
|
||||
print(out_all)
|
||||
Reference in New Issue
Block a user