mirror of
https://github.com/rjNemo/vf-site
synced 2026-06-06 01:16:38 +00:00
encapsulate the config parsing
This commit is contained in:
parent
a5136d102b
commit
cada565403
3 changed files with 27 additions and 18 deletions
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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"),
|
||||
)
|
||||
|
|
|
|||
18
lib/main.py
18
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…")
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue