From a5136d102b4b166985961b1119689fcea807d5c7 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Tue, 13 Jun 2023 14:35:05 +0200 Subject: [PATCH] use consistent naming convention for config keys set default in main instead of inside the renderer fix: renderer template folder setup --- README.md | 4 ++-- config.json | 2 +- lib/config.py | 4 ++-- lib/engine.py | 9 +++------ lib/main.py | 21 +++++++++++++-------- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 73c44c8..5472954 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ The configuration file ([config.json](./config.json)) is mandatory and should re ```json { "name": "VillaFleurie", - "templatesFolder": "templates", + "templatesDir": "templates", "templates": [ "index.html", "t2-corail.html", @@ -23,7 +23,7 @@ The configuration file ([config.json](./config.json)) is mandatory and should re "contact.html", "reservation.html" ], - "staticFiles": [], + "staticFilesDir": [], "outDir": "dist" } ``` diff --git a/config.json b/config.json index 155fdc4..176f6ee 100644 --- a/config.json +++ b/config.json @@ -7,7 +7,7 @@ "contact.html", "reservation.html" ], - "staticFiles": [ + "staticFilesDir": [ "images", "js", "css", diff --git a/lib/config.py b/lib/config.py index 91bcd17..a96eb1d 100644 --- a/lib/config.py +++ b/lib/config.py @@ -4,9 +4,9 @@ from typing import TypedDict class Config(TypedDict): name: str - templatesFolder: str + templatesDir: str templates: list[str] - staticFiles: list[str] + staticFilesDir: list[str] outDir: str diff --git a/lib/engine.py b/lib/engine.py index 41080cf..4c63148 100644 --- a/lib/engine.py +++ b/lib/engine.py @@ -1,19 +1,16 @@ -from dataclasses import dataclass from typing import Protocol from jinja2 import Environment, FileSystemLoader, select_autoescape -engine = Environment(loader=FileSystemLoader("templates"), autoescape=select_autoescape()) - class Renderer(Protocol): def render(self, template: str, context: dict | None = None) -> str: ... -@dataclass class FileSystemRenderer(Renderer): - path: str = "templates" + def __init__(self, path: str): + self.engine = Environment(loader=FileSystemLoader(path), autoescape=select_autoescape()) def render(self, template: str, context: dict | None = None) -> str: - return engine.get_template(template).render(context or {}) + return self.engine.get_template(template).render(context or {}) diff --git a/lib/main.py b/lib/main.py index 29ec149..e1ed37e 100644 --- a/lib/main.py +++ b/lib/main.py @@ -1,13 +1,18 @@ import os import shutil import sys -from os import path 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: @@ -17,23 +22,23 @@ def main(): sys.exit() data = {} - destination_path = config["outDir"] - fs = FileSystemRenderer(config.get("templatesFolder")) + destination_path = config[OUT_DIR] + fs = FileSystemRenderer(config.setdefault(TEMPLATES_DIR, TEMPLATES)) - 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(path.join(destination_path, template), "w") as f: + 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["staticFiles"]: - shutil.copytree(folder, f"{config['outDir']}/{folder}") + for folder in config[STATIC_FILES_DIR]: + shutil.copytree(folder, os.path.join(config[OUT_DIR], folder)) logger.info("🎉 Done…")