python-gallery/gallery/utils/theme_manager.py

56 lines
1.7 KiB
Python
Raw Normal View History

2023-03-04 13:45:26 +00:00
"""
OnlyLegs - Theme Manager
"""
import os
import sys
import shutil
from datetime import datetime
import sass
def compile_theme(theme_name, app_path):
2023-03-04 13:45:26 +00:00
"""
Compiles the theme into the static folder
"""
print(f"Loading '{theme_name}' theme...")
2023-03-04 13:45:26 +00:00
# Set Paths
2023-04-01 17:40:42 +00:00
theme_source = os.path.join(app_path, 'themes', theme_name)
theme_destination = os.path.join(app_path, 'static', 'theme')
2023-03-04 13:45:26 +00:00
# If the theme doesn't exist, exit
2023-04-01 17:40:42 +00:00
if not os.path.exists(theme_source):
print("Theme does not exist!")
sys.exit(1)
2023-03-04 13:45:26 +00:00
# If the destination folder doesn't exist, create it
2023-04-01 17:40:42 +00:00
if not os.path.exists(theme_destination):
os.makedirs(theme_destination)
2023-03-04 13:45:26 +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'):
print("No sass file found!")
sys.exit(1)
2023-03-04 13:45:26 +00:00
# Compile the theme
2023-04-01 17:40:42 +00:00
with open(os.path.join(theme_destination, 'style.css'),
encoding='utf-8', mode='w+') as file:
try:
2023-04-01 17:40:42 +00:00
file.write(sass.compile(filename=os.path.join(theme_source, 'style.sass'),
output_style='compressed'))
except sass.CompileError as err:
print("Failed to compile!\n", err)
2023-03-04 13:45:26 +00:00
sys.exit(1)
print("Compiled successfully!")
2023-04-01 17:40:42 +00:00
# If the destination folder exists, remove it
2023-04-01 17:40:42 +00:00
if os.path.exists(os.path.join(theme_destination, 'fonts')):
2023-04-02 16:50:52 +00:00
shutil.rmtree(os.path.join(theme_destination, 'fonts'))
2023-03-20 17:57:47 +00:00
# Copy the fonts
2023-04-02 16:50:52 +00:00
shutil.copytree(os.path.join(theme_source, 'fonts'),
os.path.join(theme_destination, 'fonts'))
print("Fonts copied successfully!")
print(f"{datetime.now().hour}:{datetime.now().minute}:{datetime.now().second} - Done!\n")