import os, sys, yaml from jinja2 import Environment, FileSystemLoader from pathlib import Path class Config: def __init__(self): self.config_path = "config.yml" self.app_name = "microgen" self.app_description = "Simplistic static site generator" self.app_src_url = f"https://git.exocortex.ru/Exocortex/{self.app_name}" self.content_dir = 'content' self.templates_dir = 'templates' self.static_dir = 'static' self.output_dir = 'public' self.header_image = 'static/header.jpg' self.site_name = f"{self.app_name} library" self.theme = "default" self.debug = False if os.path.isfile (self.config_path): with open(self.config_path, "r") as f: config = yaml.safe_load(f) 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) def __repr__(self): return f"{self.__dict__}" def create_default_config(self): defaults = { "site_name": self.site_name, "theme": "default", "debug": False, "footer": "'' # Author / copyright / date / links, can be plaintext or valid HTML" } with open(self.config_path, mode="w", encoding="utf-8") as f: yaml.safe_dump(defaults, f) config = Config()