This commit is contained in:
Ruidy 2023-07-05 16:10:18 +02:00
parent 65604a72c7
commit 5e39e5920a
4 changed files with 29 additions and 27 deletions

View file

@ -13,4 +13,4 @@ mypy = "*"
ruff = "*" ruff = "*"
[requires] [requires]
python_version = "3.9" python_version = "3.11"

4
Pipfile.lock generated
View file

@ -1,11 +1,11 @@
{ {
"_meta": { "_meta": {
"hash": { "hash": {
"sha256": "fcc9e26a9ee4645cb0e75631215df5ca39ce5517d62bd3970932fb1f7c9d69cb" "sha256": "4c4b43f1ede1c6a64f569dbd3bbfdc2c6aabfe004db06d43fe42496847026061"
}, },
"pipfile-spec": 6, "pipfile-spec": 6,
"requires": { "requires": {
"python_version": "3.9" "python_version": "3.11"
}, },
"sources": [ "sources": [
{ {

View file

@ -1,8 +1,6 @@
import tomllib import tomllib
from dataclasses import dataclass from dataclasses import dataclass
NAME = "name"
@dataclass(frozen=True) @dataclass(frozen=True)
class Config: class Config:
@ -17,7 +15,7 @@ def load() -> Config:
with open("config.toml", "rb") as f: with open("config.toml", "rb") as f:
raw_config = tomllib.load(f) raw_config = tomllib.load(f)
return Config( return Config(
name=raw_config[NAME], name=raw_config["name"],
static_dir="assets", static_dir="assets",
data_dir="data", data_dir="data",
out_dir="dist", out_dir="dist",

View file

@ -7,28 +7,22 @@ from time import perf_counter
from loguru import logger from loguru import logger
from lib.config import load from lib.config import Config, load
from lib.engine import FileSystemRenderer from lib.engine import FileSystemRenderer
def main(): def main():
start = perf_counter() start = perf_counter()
try: config = parse_config()
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) fs = FileSystemRenderer(config)
logger.info(f"🏁 Start building {config.name}") logger.info(f"🏁 Start building {config.name}")
clean_dist(config.out_dir)
clean_dist(destination_path)
for page in os.scandir(config.templates_dir): for page in os.scandir(config.templates_dir):
if page.is_file(): if page.is_file():
with open(os.path.join(destination_path, page.name), "w") as fd: with open(os.path.join(config.out_dir, page.name), "w") as fd:
data = parse_data(page, config.data_dir) data = parse_data(page, config.data_dir)
logger.info(f"📃 Render '{page.name}'") logger.info(f"📃 Render '{page.name}'")
fd.write(fs.render(page.name, data)) fd.write(fs.render(page.name, data))
@ -40,6 +34,16 @@ def main():
logger.info(f"🎉 Done… in {(end - start) * 1000:.2f} ms") 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)
else:
return config
def parse_data(page: os.DirEntry, data_dir: str) -> dict: def parse_data(page: os.DirEntry, data_dir: str) -> dict:
data_file_path = os.path.join(data_dir, f"{page.name.split('.')[0]}.toml") data_file_path = os.path.join(data_dir, f"{page.name.split('.')[0]}.toml")
data = {} data = {}