From 5e39e5920a0441d8603cb085d7e3ba732ce5daf5 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Wed, 5 Jul 2023 16:10:18 +0200 Subject: [PATCH] refactor --- Pipfile | 2 +- Pipfile.lock | 4 ++-- lib/config.py | 4 +--- lib/main.py | 46 +++++++++++++++++++++++++--------------------- 4 files changed, 29 insertions(+), 27 deletions(-) diff --git a/Pipfile b/Pipfile index 9944413..91a865a 100644 --- a/Pipfile +++ b/Pipfile @@ -13,4 +13,4 @@ mypy = "*" ruff = "*" [requires] -python_version = "3.9" +python_version = "3.11" diff --git a/Pipfile.lock b/Pipfile.lock index 49b66a4..fdf5b74 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,11 +1,11 @@ { "_meta": { "hash": { - "sha256": "fcc9e26a9ee4645cb0e75631215df5ca39ce5517d62bd3970932fb1f7c9d69cb" + "sha256": "4c4b43f1ede1c6a64f569dbd3bbfdc2c6aabfe004db06d43fe42496847026061" }, "pipfile-spec": 6, "requires": { - "python_version": "3.9" + "python_version": "3.11" }, "sources": [ { diff --git a/lib/config.py b/lib/config.py index 6dc0dc7..20b49e3 100644 --- a/lib/config.py +++ b/lib/config.py @@ -1,8 +1,6 @@ import tomllib from dataclasses import dataclass -NAME = "name" - @dataclass(frozen=True) class Config: @@ -17,7 +15,7 @@ def load() -> Config: with open("config.toml", "rb") as f: raw_config = tomllib.load(f) return Config( - name=raw_config[NAME], + name=raw_config["name"], static_dir="assets", data_dir="data", out_dir="dist", diff --git a/lib/main.py b/lib/main.py index 4fbed41..bf5b21e 100644 --- a/lib/main.py +++ b/lib/main.py @@ -7,37 +7,41 @@ from time import perf_counter from loguru import logger -from lib.config import load +from lib.config import Config, load from lib.engine import FileSystemRenderer def main(): start = perf_counter() + config = parse_config() + + fs = FileSystemRenderer(config) + + logger.info(f"🏁 Start building {config.name}") + clean_dist(config.out_dir) + + for page in os.scandir(config.templates_dir): + if page.is_file(): + with open(os.path.join(config.out_dir, page.name), "w") as fd: + data = parse_data(page, config.data_dir) + logger.info(f"📃 Render '{page.name}'") + fd.write(fs.render(page.name, data)) + + logger.info("⏩ Copy static assets to build") + copy_tree(config.static_dir, os.path.join(config.out_dir)) + + end = perf_counter() + logger.info(f"🎉 Done… in {(end - start) * 1000:.2f} ms") + + +def parse_config() -> Config: try: config = load() except FileNotFoundError: logger.error("the configuration file 'config.toml' was not found. Please verify it exists at the root level") sys.exit(1) - - destination_path = config.out_dir - fs = FileSystemRenderer(config) - - logger.info(f"🏁 Start building {config.name}") - - clean_dist(destination_path) - - for page in os.scandir(config.templates_dir): - if page.is_file(): - with open(os.path.join(destination_path, page.name), "w") as fd: - data = parse_data(page, config.data_dir) - logger.info(f"📃Render '{page.name}'") - fd.write(fs.render(page.name, data)) - - logger.info("⏩ Copy static assets to build") - copy_tree(config.static_dir, os.path.join(config.out_dir)) - - end = perf_counter() - logger.info(f"🎉 Done… in {(end - start) * 1000:.2f} ms") + else: + return config def parse_data(page: os.DirEntry, data_dir: str) -> dict: