2021-08-16 01:46:04 +02:00
|
|
|
from os import listdir
|
|
|
|
from os.path import isdir, isfile, join
|
2021-09-20 23:56:27 +02:00
|
|
|
from json import dump, dumps, loads
|
2021-08-20 13:38:10 +02:00
|
|
|
import subprocess
|
2021-08-16 01:46:04 +02:00
|
|
|
|
2021-09-20 23:56:27 +02:00
|
|
|
|
2021-08-20 13:38:10 +02:00
|
|
|
def get_shas(output):
|
|
|
|
"""Returns a dict of CSS files and SHAs"""
|
|
|
|
output_lines = output.splitlines()
|
|
|
|
sha_dict = {}
|
|
|
|
for line in output_lines:
|
2021-09-20 23:56:27 +02:00
|
|
|
line = line.decode('utf-8').replace("\t", "").split(" ")
|
2021-08-20 13:38:10 +02:00
|
|
|
sha = line[1]
|
|
|
|
css_file = [file for file in line[2].split("/") if "css" in file][0]
|
|
|
|
sha_dict.update({css_file: sha})
|
|
|
|
return(sha_dict)
|
|
|
|
|
2021-09-20 23:56:27 +02:00
|
|
|
|
2021-08-20 13:38:10 +02:00
|
|
|
def create_addons_json():
|
2021-09-20 23:56:27 +02:00
|
|
|
addon_shas = subprocess.check_output(
|
|
|
|
["git", "ls-files", "-s", "./CSS/addons/*.css"])
|
2021-08-20 13:38:10 +02:00
|
|
|
SHAS = get_shas(addon_shas)
|
2021-09-20 23:56:27 +02:00
|
|
|
ADDONS = {"addons": {}}
|
2021-08-16 01:46:04 +02:00
|
|
|
addon_root = './CSS/addons'
|
2021-09-20 23:56:27 +02:00
|
|
|
addon_folders = [name for name in listdir(
|
|
|
|
addon_root) if isdir(join(addon_root, name))]
|
2021-08-16 01:46:04 +02:00
|
|
|
for app in addon_folders:
|
|
|
|
app_addons = [addon for addon in listdir(f"{addon_root}/{app}")]
|
|
|
|
ADDONS["addons"].update({
|
2021-09-20 23:56:27 +02:00
|
|
|
app: {
|
|
|
|
addon: {} for addon in app_addons
|
|
|
|
}
|
2021-08-16 01:46:04 +02:00
|
|
|
})
|
|
|
|
for addon in app_addons:
|
|
|
|
files = [file for file in listdir(
|
|
|
|
f"{addon_root}/{app}/{addon}") if isfile(join(f"{addon_root}/{app}/{addon}", file))]
|
2021-09-20 23:56:27 +02:00
|
|
|
if len([f for f in files if f.endswith('.css')]) > 1:
|
|
|
|
ADDONS["addons"][app][addon].update({
|
|
|
|
"css": [f"https://{DOMAIN}/CSS/addons/{app}/{addon}/{file}?sha={SHAS.get(file)}" for file in files if file.split(".")[1] == "css"]
|
2021-08-16 01:46:04 +02:00
|
|
|
}
|
2021-09-20 23:56:27 +02:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
ADDONS["addons"][app].update({
|
|
|
|
addon: f"https://{DOMAIN}/CSS/addons/{app}/{addon}/{file}?sha={SHAS.get(file)}" for file in files if file.split(".")[1] == "css"
|
|
|
|
}
|
|
|
|
)
|
2021-08-16 01:46:04 +02:00
|
|
|
extra_dirs = [dir for dir in listdir(
|
|
|
|
f"{addon_root}/{app}/{addon}") if isdir(join(f"{addon_root}/{app}/{addon}", dir))]
|
|
|
|
if extra_dirs:
|
|
|
|
for dir in extra_dirs:
|
|
|
|
extra_dir_files = [file for file in listdir(
|
|
|
|
f"{addon_root}/{app}/{addon}/{dir}") if isfile(join(f"{addon_root}/{app}/{addon}/{dir}", file))]
|
2021-09-20 23:56:27 +02:00
|
|
|
ADDONS["addons"][app][addon].update({
|
|
|
|
dir: {
|
|
|
|
"css": [f"https://{DOMAIN}/CSS/addons/{app}/{addon}/{dir}/{extra_file}?sha={SHAS.get(extra_file)}" for extra_file in extra_dir_files if extra_file.split(".")[1] == "css"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2021-08-16 01:46:04 +02:00
|
|
|
return dumps(ADDONS)
|
|
|
|
|
2021-09-20 23:56:27 +02:00
|
|
|
|
|
|
|
def create_json(app_folders: list = None, themes: list = None, no_sub_folders=False):
|
2021-08-16 01:46:04 +02:00
|
|
|
if no_sub_folders:
|
|
|
|
THEMES = {}
|
2021-09-20 23:56:27 +02:00
|
|
|
theme_shas = subprocess.check_output(
|
|
|
|
["git", "ls-files", "-s", "./CSS/variables/*.css"])
|
2021-08-20 13:38:10 +02:00
|
|
|
SHAS = get_shas(theme_shas)
|
2021-08-16 01:46:04 +02:00
|
|
|
for theme in themes:
|
|
|
|
THEMES.update({
|
|
|
|
"themes": {
|
|
|
|
theme.split(".")[0].capitalize(): {
|
2021-09-20 23:56:27 +02:00
|
|
|
"url": f"https://{DOMAIN}/CSS/variables/{theme}?sha={SHAS.get(theme)}"
|
2021-08-16 01:46:04 +02:00
|
|
|
}for theme in themes
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return dumps(THEMES)
|
|
|
|
else:
|
2021-08-20 13:38:10 +02:00
|
|
|
ADDONS = loads(create_addons_json())
|
2021-09-20 23:56:27 +02:00
|
|
|
APPS = {"applications": {}}
|
|
|
|
app_shas = subprocess.check_output(
|
|
|
|
["git", "ls-files", "-s", "./CSS/themes/*base.css"])
|
2021-08-20 13:38:10 +02:00
|
|
|
SHAS = get_shas(app_shas)
|
2021-08-16 01:46:04 +02:00
|
|
|
for app in app_folders:
|
|
|
|
APPS.update({
|
2021-09-20 23:56:27 +02:00
|
|
|
"applications": {
|
2021-08-16 01:46:04 +02:00
|
|
|
app: {
|
2021-08-20 13:38:10 +02:00
|
|
|
"base_css": f"https://{DOMAIN}/CSS/themes/{app}/{app}-base.css?sha={SHAS.get(f'{app}-base.css')}",
|
2021-08-16 01:46:04 +02:00
|
|
|
"addons": ADDONS["addons"][app] if app in ADDONS["addons"] else {}
|
2021-09-20 23:56:27 +02:00
|
|
|
} for app in app_folders
|
2021-08-16 01:46:04 +02:00
|
|
|
}
|
2021-09-20 23:56:27 +02:00
|
|
|
}
|
2021-08-16 01:46:04 +02:00
|
|
|
)
|
2021-09-20 23:56:27 +02:00
|
|
|
THEMES = loads(create_json(themes=themes, no_sub_folders=True))
|
2021-08-16 01:46:04 +02:00
|
|
|
APPS.update(ADDONS)
|
|
|
|
APPS.update(THEMES)
|
|
|
|
return dumps(APPS)
|
|
|
|
|
2021-09-20 23:56:27 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
app_folders = [name for name in listdir(
|
|
|
|
'./CSS/themes') if isdir(join('./CSS/themes', name))]
|
|
|
|
themes = [name for name in listdir(
|
|
|
|
'./CSS/variables') if isfile(join('./CSS/variables', name))]
|
|
|
|
DOMAIN = open("CNAME", "rt", closefd=True).readline()
|
|
|
|
apps = loads(create_json(app_folders, themes))
|
2021-08-16 01:46:04 +02:00
|
|
|
|
|
|
|
with open("themes.json", "w") as outfile:
|
2021-09-20 23:56:27 +02:00
|
|
|
dump(apps, outfile, indent=2)
|