python-gallery/gallery/sassy.py

53 lines
1.6 KiB
Python
Raw Normal View History

import datetime
now = datetime.datetime.now()
2023-01-01 14:31:28 +00:00
import sys
import shutil
2023-01-01 14:31:28 +00:00
import os
import sass
2023-01-01 14:31:28 +00:00
class compile():
2023-01-01 14:31:28 +00:00
def __init__(self, theme):
print(f"Loading '{theme}' theme...")
theme_path = os.path.join('./user', 'themes', theme, 'style.scss')
font_path = os.path.join('./user', 'themes', theme, 'fonts')
2023-01-01 14:31:28 +00:00
print(f"Theme path: {theme_path}")
2023-01-01 14:31:28 +00:00
if os.path.exists(theme_path):
2023-01-01 14:31:28 +00:00
self.sass = sass
self.loadTheme(theme_path)
self.loadFonts(font_path)
2023-01-01 14:31:28 +00:00
else:
print("No theme found!")
2023-01-01 14:31:28 +00:00
sys.exit(1)
print(f"{now.hour}:{now.minute}:{now.second} - Done!\n")
2023-01-01 14:31:28 +00:00
def loadTheme (self, theme):
with open('static/theme/style.css', 'w') as f:
2023-01-01 14:31:28 +00:00
try:
f.write(self.sass.compile(filename=theme, output_style='compressed'))
print("Compiled successfully to:", f.name)
2023-01-01 14:31:28 +00:00
except self.sass.CompileError as e:
print("Failed to compile!\n", e)
sys.exit(1)
def loadFonts (self, font_path):
dest = os.path.join('./static', 'theme', 'fonts')
if os.path.exists(dest):
print("Removing old fonts...")
try:
shutil.rmtree(dest)
except Exception as e:
print("Failed to remove old fonts!\n", e)
sys.exit(1)
try:
shutil.copytree(font_path, dest)
print("Copied fonts to:", dest)
except Exception as e:
print("Failed to copy fonts!\n", e)
sys.exit(1)