From cada565403635fa0ca7e811e1516108857445f3b Mon Sep 17 00:00:00 2001 From: Ruidy Date: Tue, 13 Jun 2023 14:55:51 +0200 Subject: [PATCH] encapsulate the config parsing --- README.md | 1 + lib/config.py | 26 ++++++++++++++++++++------ lib/main.py | 18 ++++++------------ 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 5472954..1cfe229 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ The entry point is located in the [main file](./lib/main.py). It should not be m By default, the templates files are located in the [templates](./templates) directory. You can use template inheritance but not yet data injection. + ### Configuration The configuration file ([config.json](./config.json)) is mandatory and should resemble: diff --git a/lib/config.py b/lib/config.py index a96eb1d..7b97423 100644 --- a/lib/config.py +++ b/lib/config.py @@ -1,15 +1,29 @@ import json -from typing import TypedDict +from dataclasses import dataclass + +NAME = "name" +TEMPLATES = "templates" +TEMPLATES_DIR = "templatesDir" +STATIC_FILES_DIR = "staticFilesDir" +OUT_DIR = "outDir" -class Config(TypedDict): +@dataclass(frozen=True) +class Config: name: str - templatesDir: str templates: list[str] - staticFilesDir: list[str] - outDir: str + static_files_dir: list[str] + out_dir: str + templates_dir: str def load() -> Config: with open("config.json", "r") as f: - return json.loads(f.read()) + raw_config = json.loads(f.read()) + return Config( + name=raw_config[NAME], + templates=raw_config[TEMPLATES], + static_files_dir=raw_config[STATIC_FILES_DIR], + out_dir=raw_config.setdefault(OUT_DIR, "dist"), + templates_dir=raw_config.setdefault(TEMPLATES_DIR, "templates"), + ) diff --git a/lib/main.py b/lib/main.py index e1ed37e..544091e 100644 --- a/lib/main.py +++ b/lib/main.py @@ -7,12 +7,6 @@ from loguru import logger from lib.config import load from lib.engine import FileSystemRenderer -NAME = "name" -TEMPLATES = "templates" -TEMPLATES_DIR = "templatesDir" -STATIC_FILES_DIR = "staticFilesDir" -OUT_DIR = "outDir" - def main(): try: @@ -22,23 +16,23 @@ def main(): sys.exit() data = {} - destination_path = config[OUT_DIR] - fs = FileSystemRenderer(config.setdefault(TEMPLATES_DIR, TEMPLATES)) + destination_path = config.out_dir + fs = FileSystemRenderer(config.templates_dir) - logger.info(f"🏁 Start building {config[NAME]}") + logger.info(f"🏁 Start building {config.name}") if os.path.exists(destination_path) and os.path.isdir(destination_path): shutil.rmtree(destination_path) os.mkdir(destination_path) - for template in config[TEMPLATES]: + for template in config.templates: logger.info(f"📃Render '{template}'") with open(os.path.join(destination_path, template), "w") as f: f.write(fs.render(template, data.get(template))) logger.info("⏩ Start copying staticfiles to build") - for folder in config[STATIC_FILES_DIR]: - shutil.copytree(folder, os.path.join(config[OUT_DIR], folder)) + for folder in config.static_files_dir: + shutil.copytree(folder, os.path.join(config.out_dir, folder)) logger.info("🎉 Done…")