1
0
mirror of https://github.com/gilbN/theme.park.git synced 2024-11-20 18:12:31 +01:00
theme.park/themes.py

150 lines
6.8 KiB
Python
Raw Normal View History

2022-03-22 10:05:36 +01:00
#! /usr/bin/env python3
from os import listdir, environ as env, chdir
from os.path import isdir, isfile, join, dirname, abspath
2021-11-01 22:35:58 +01:00
from json import dump, dumps, loads, load
2021-08-20 13:35:40 +02:00
import subprocess
2022-03-22 10:05:36 +01:00
chdir(dirname(abspath(__file__))) # Set working dir
2021-09-14 17:31:45 +02:00
2021-08-20 13:35:40 +02:00
def get_shas(output):
"""Returns a dict of CSS files and SHAs"""
2022-03-22 10:05:36 +01:00
output_lines = output.splitlines() if output else []
2021-08-20 13:35:40 +02:00
sha_dict = {}
for line in output_lines:
2021-10-30 01:40:03 +02:00
line = line.decode('utf-8').replace("0\t", "").split(" ")
2021-08-20 13:35:40 +02:00
sha = line[1]
2021-10-30 01:40:03 +02:00
css_file = [file for file in line[2].split("/") if "css" in file][-1]
2021-08-20 13:35:40 +02:00
sha_dict.update({css_file: sha})
return(sha_dict)
2021-09-14 17:31:45 +02:00
2021-08-20 13:35:40 +02:00
def create_addons_json():
2022-03-22 10:05:36 +01:00
addon_shas = subprocess.check_output(["git", "ls-files", "-s", "./css/addons/*.css"]) if isdir(".git") else []
2021-08-20 13:35:40 +02:00
SHAS = get_shas(addon_shas)
2021-09-14 17:31:45 +02:00
ADDONS = {"addons": {}}
2021-10-30 01:40:03 +02:00
addon_root = './css/addons'
2021-09-14 17:31:45 +02:00
addon_folders = [name for name in listdir(
addon_root) if isdir(join(addon_root, name))]
2021-08-16 01:30:43 +02:00
for app in addon_folders:
app_addons = [addon for addon in listdir(f"{addon_root}/{app}")]
ADDONS["addons"].update({
2021-09-14 17:31:45 +02:00
app: {
addon: {} for addon in app_addons
}
2021-08-16 01:30:43 +02:00
})
for addon in app_addons:
2021-09-14 17:31:45 +02:00
files = [file for file in listdir(
f"{addon_root}/{app}/{addon}") if isfile(join(f"{addon_root}/{app}/{addon}", file))]
2021-09-14 17:31:27 +02:00
if len([f for f in files if f.endswith('.css')]) > 1:
ADDONS["addons"][app][addon].update({
2021-10-30 01:40:03 +02:00
"css": [f"https://{DOMAIN}/css/addons/{app}/{addon}/{file}?sha={SHAS.get(file)}" for file in files if file.split(".")[1] == "css"]
2021-09-14 17:31:45 +02:00
}
2021-09-14 17:31:27 +02:00
)
else:
ADDONS["addons"][app].update({
2021-10-30 01:40:03 +02:00
addon: f"https://{DOMAIN}/css/addons/{app}/{addon}/{file}?sha={SHAS.get(file)}" for file in files if file.split(".")[1] == "css"
2021-09-14 17:31:45 +02:00
}
2021-09-14 17:31:27 +02:00
)
2021-08-16 01:30:43 +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-14 17:31:27 +02:00
ADDONS["addons"][app][addon].update({
2021-09-14 17:31:45 +02:00
dir: {
2021-10-30 01:40:03 +02:00
"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-09-14 17:31:45 +02:00
}
}
)
2021-10-30 15:44:50 +02:00
return dumps(ADDONS, sort_keys=True)
2021-08-16 01:30:43 +02:00
2021-09-14 17:31:45 +02:00
2021-10-28 21:33:49 +02:00
def create_json(app_folders: list = None, themes: list = None, community_themes: list = None, no_sub_folders=False):
2021-08-16 01:30:43 +02:00
if no_sub_folders:
2021-10-29 00:33:37 +02:00
THEMES_DICT = {}
2022-03-22 10:05:36 +01:00
theme_shas = subprocess.check_output(["git", "ls-files", "-s", "./css/theme-options/*.css"]) if isdir(".git") else []
community_theme_shas = subprocess.check_output(["git", "ls-files", "-s", "./css/community-theme-options/*.css"]) if isdir(".git") else []
2021-10-28 21:33:49 +02:00
THEME_SHAS = get_shas(theme_shas)
2021-10-29 00:33:37 +02:00
COMMUNITY_THEME_SHAS = get_shas(community_theme_shas)
THEMES = {
theme.split(".")[0].capitalize(): {
2021-10-30 01:40:03 +02:00
"url": f"https://{DOMAIN}/css/theme-options/{theme}?sha={THEME_SHAS.get(theme)}"
2021-10-29 00:33:37 +02:00
}for theme in themes
}
COMMUNITY_THEMES = {
theme.split(".")[0].capitalize(): {
2021-10-30 01:40:03 +02:00
"url": f"https://{DOMAIN}/css/community-theme-options/{theme}?sha={COMMUNITY_THEME_SHAS.get(theme)}"
2021-10-29 00:33:37 +02:00
}for theme in community_themes
}
2021-10-30 15:44:50 +02:00
THEMES_DICT.update(dict(sorted({
2021-10-29 00:33:37 +02:00
"themes": {
**THEMES
},
"community-themes": {
**COMMUNITY_THEMES
},
"all-themes": {
**THEMES, **COMMUNITY_THEMES
2021-10-28 21:33:49 +02:00
}
2021-10-30 15:44:50 +02:00
}.items())))
2021-10-29 00:33:37 +02:00
return dumps(THEMES_DICT)
2021-08-16 01:30:43 +02:00
else:
2021-08-20 13:35:40 +02:00
ADDONS = loads(create_addons_json())
2021-11-01 22:35:58 +01:00
APPS = {}
2022-03-22 10:05:36 +01:00
app_shas = subprocess.check_output(["git", "ls-files", "-s", "./css/base/*base.css"]) if isdir(".git") else []
2021-08-20 13:35:40 +02:00
SHAS = get_shas(app_shas)
2021-10-30 15:44:50 +02:00
APPS.update(dict(sorted({
2021-10-29 00:33:37 +02:00
"applications": {
app: {
2021-10-30 01:40:03 +02:00
"base_css": f"https://{DOMAIN}/css/base/{app}/{app}-base.css?sha={SHAS.get(f'{app}-base.css')}",
2021-10-29 00:33:37 +02:00
"addons": ADDONS["addons"][app] if app in ADDONS["addons"] else {}
2021-10-30 01:40:03 +02:00
} for app in app_folders if not isfile(f'./css/base/{app}/.deprecated')
2021-10-29 00:33:37 +02:00
}
2021-10-30 15:44:50 +02:00
}.items())))
2021-11-01 22:35:58 +01:00
APPS.update(dict(sorted({
"deprecated": {
app: {
"base_css": f"https://{DOMAIN}/css/base/{app}/{app}-base.css?sha={SHAS.get(f'{app}-base.css')}",
"addons": ADDONS["addons"][app] if app in ADDONS["addons"] else {}
} for app in app_folders if isfile(f'./css/base/{app}/.deprecated')
}
}.items())))
2021-10-28 21:33:49 +02:00
THEMES = loads(create_json(themes=themes, community_themes=community_themes, no_sub_folders=True))
2021-08-16 01:30:43 +02:00
APPS.update(ADDONS)
APPS.update(THEMES)
return dumps(APPS)
2021-11-01 22:35:58 +01:00
def create_theme_options():
2021-11-01 23:13:53 +01:00
def create_css(folder):
2021-11-01 22:35:58 +01:00
with open(f"{folder}/{app}/{theme.lower()}.css", "w") as create_app:
content = f'@import url("{applications[app]["base_css"]}");\n@import url("{themes[theme]["url"]}");'
create_app.write(content)
with open("themes.json") as themes:
data = load(themes)
themes = data["all-themes"]
applications = data["applications"]
for app in applications:
for theme in themes:
2022-01-04 01:02:21 +01:00
folders = ["./css/base"]
2021-11-01 22:35:58 +01:00
for folder in folders:
2021-11-01 23:13:53 +01:00
create_css(folder)
2021-11-01 22:35:58 +01:00
2022-03-22 10:05:36 +01:00
env_domain = env.get('TP_DOMAIN')
2021-11-01 22:35:58 +01:00
2021-09-14 17:31:45 +02:00
if __name__ == "__main__":
2021-10-30 01:40:03 +02:00
app_folders = [name for name in listdir('./css/base') if isdir(join('./css/base', name))]
themes = [name for name in listdir('./css/theme-options') if isfile(join('./css/theme-options', name))]
community_themes = [name for name in listdir('./css/community-theme-options') if isfile(join('./css/community-theme-options', name))]
2022-03-22 10:05:36 +01:00
develop = True if isdir(".git") and subprocess.check_output(["git", "symbolic-ref", "--short", "HEAD"]).decode('ascii').strip() == "develop" else False
if env_domain:
DOMAIN = env_domain
else:
with open("CNAME", "rt", closefd=True) as cname:
CNAME = cname.readline()
DOMAIN = CNAME if not develop else f"develop.{CNAME}"
2021-10-28 21:33:49 +02:00
apps = loads(create_json(app_folders=app_folders, themes=themes, community_themes=community_themes))
2021-08-16 01:30:43 +02:00
with open("themes.json", "w") as outfile:
2021-10-30 16:05:37 +02:00
dump(apps, outfile, indent=2, sort_keys=True)
2021-11-01 22:35:58 +01:00
create_theme_options()