This commit is contained in:
SG
2025-06-18 15:26:11 +03:00
parent 3abe3f2fac
commit 15879e66b7
2 changed files with 35 additions and 31 deletions

View File

@@ -1,4 +1,6 @@
import os, yaml
import os, sys, yaml
from jinja2 import Environment, FileSystemLoader
from pathlib import Path
# Main config section
#class Config:
@@ -33,6 +35,33 @@ class Config:
if config:
self.update_from_dict(config)
# Get the script base_dir and check for templates dir
if Path(f'themes/{self.theme}/templates').exists():
self.base_dir = "."
elif getattr(sys, 'frozen', False):
# Running fron Pyinstaller-packed binary
self.base_dir = Path(sys._MEIPASS)
else:
# Running as a plain Python app
self.base_dir = Path(__file__).resolve().parent
# Prepare template rendering engine
if Path(f"themes/{self.theme}/templates").exists():
# Use the templates from the initialized site instance and the installed theme
jinja_env_dir = f"themes/{self.theme}/templates"
#logger.debug(f"Using locally initialized templates from {jinja_env_dir}")
else:
# Use default templates from the default theme shipped with the app
# i.e. PyInstaller-packed single executable
#logger.debug("Using shipped default temlpates.")
jinja_env_dir = f"{self.base_dir}/themes/default/templates"
self.env = Environment(loader=FileSystemLoader(f"{jinja_env_dir}"))
self.env.globals['header_image'] = f"{self.base_dir}/themes/{self.theme}/static/images/header.jpg"
self.index_template = self.env.get_template("index.html")
self.content_item_template = self.env.get_template("content_item.html")
def update_from_dict(self, config_dict):
for key, value in config_dict.items():
setattr(self, key, value)