2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
OnlyLegs - Theme Manager
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import shutil
|
|
|
|
from datetime import datetime
|
|
|
|
import sass
|
2023-04-03 00:42:42 +00:00
|
|
|
import json
|
2023-03-04 13:45:26 +00:00
|
|
|
|
|
|
|
|
2023-04-01 16:59:17 +00:00
|
|
|
def compile_theme(theme_name, app_path):
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
Compiles the theme into the static folder
|
|
|
|
"""
|
2023-04-01 16:59:17 +00:00
|
|
|
print(f"Loading '{theme_name}' theme...")
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-04-01 16:59:17 +00:00
|
|
|
# Set Paths
|
2023-04-01 17:40:42 +00:00
|
|
|
theme_source = os.path.join(app_path, 'themes', theme_name)
|
2023-04-03 00:42:42 +00:00
|
|
|
theme_target = os.path.join(app_path, 'static', 'theme')
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-04-01 16:59:17 +00:00
|
|
|
# If the theme doesn't exist, exit
|
2023-04-01 17:40:42 +00:00
|
|
|
if not os.path.exists(theme_source):
|
2023-04-01 16:59:17 +00:00
|
|
|
print("Theme does not exist!")
|
|
|
|
sys.exit(1)
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-04-01 16:59:17 +00:00
|
|
|
# If the destination folder doesn't exist, create it
|
2023-04-03 00:42:42 +00:00
|
|
|
if not os.path.exists(theme_target):
|
|
|
|
os.makedirs(theme_target)
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-04-01 16:59:17 +00:00
|
|
|
# Theme source file doesn't exist, exit
|
2023-04-01 17:40:42 +00:00
|
|
|
if not os.path.join(theme_source, 'style.sass'):
|
2023-04-01 16:59:17 +00:00
|
|
|
print("No sass file found!")
|
|
|
|
sys.exit(1)
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-04-03 00:42:42 +00:00
|
|
|
# Check if the theme has a manifest.json file
|
|
|
|
if os.path.exists(os.path.join(theme_source, 'manifest.json')):
|
|
|
|
source_info = json.load(open(os.path.join(theme_source, 'manifest.json'), encoding='utf-8', mode='r'))
|
|
|
|
else:
|
|
|
|
print("Theme lacks a manifest.json file, exiting as its required!")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Check if the theme is upto date
|
|
|
|
if os.path.exists(os.path.join(theme_target, 'manifest.json')):
|
|
|
|
target_info = json.load(open(os.path.join(theme_target, 'manifest.json'), encoding='utf-8', mode='r'))
|
|
|
|
|
|
|
|
if source_info['version'] == target_info['version'] and source_info['name'] == target_info['name']:
|
|
|
|
print("Theme is up to date!")
|
|
|
|
return
|
|
|
|
|
2023-04-01 16:59:17 +00:00
|
|
|
# Compile the theme
|
2023-04-03 00:42:42 +00:00
|
|
|
with open(os.path.join(theme_target, 'style.css'),
|
2023-04-01 17:40:42 +00:00
|
|
|
encoding='utf-8', mode='w+') as file:
|
2023-04-01 16:59:17 +00:00
|
|
|
try:
|
2023-04-01 17:40:42 +00:00
|
|
|
file.write(sass.compile(filename=os.path.join(theme_source, 'style.sass'),
|
|
|
|
output_style='compressed'))
|
2023-04-01 16:59:17 +00:00
|
|
|
except sass.CompileError as err:
|
|
|
|
print("Failed to compile!\n", err)
|
2023-03-04 13:45:26 +00:00
|
|
|
sys.exit(1)
|
2023-04-01 16:59:17 +00:00
|
|
|
print("Compiled successfully!")
|
2023-04-01 17:40:42 +00:00
|
|
|
|
2023-04-01 16:59:17 +00:00
|
|
|
# If the destination folder exists, remove it
|
2023-04-03 00:42:42 +00:00
|
|
|
if os.path.exists(os.path.join(theme_target, 'fonts')):
|
|
|
|
shutil.rmtree(os.path.join(theme_target, 'fonts'))
|
2023-03-20 17:57:47 +00:00
|
|
|
|
2023-04-01 16:59:17 +00:00
|
|
|
# Copy the fonts
|
2023-04-02 16:50:52 +00:00
|
|
|
shutil.copytree(os.path.join(theme_source, 'fonts'),
|
2023-04-03 00:42:42 +00:00
|
|
|
os.path.join(theme_target, 'fonts'))
|
2023-04-02 16:50:52 +00:00
|
|
|
print("Fonts copied successfully!")
|
2023-04-03 00:42:42 +00:00
|
|
|
|
|
|
|
# Copy the manifest
|
|
|
|
shutil.copyfile(os.path.join(theme_source, 'manifest.json'),
|
|
|
|
os.path.join(theme_target, 'manifest.json'))
|
|
|
|
print("Manifest copied successfully!")
|
2023-04-01 16:59:17 +00:00
|
|
|
|
|
|
|
print(f"{datetime.now().hour}:{datetime.now().minute}:{datetime.now().second} - Done!\n")
|