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.
|
By default, the templates files are located in the [templates](./templates) directory.
|
||||||
You can use template inheritance but not yet data injection.
|
You can use template inheritance but not yet data injection.
|
||||||
|
|
||||||
### Configuration
|
### Configuration
|
||||||
|
|
||||||
The configuration file ([config.json](./config.json)) is mandatory and should resemble:
|
The configuration file ([config.json](./config.json)) is mandatory and should resemble:
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,29 @@
|
||||||
import json
|
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
|
name: str
|
||||||
templatesDir: str
|
|
||||||
templates: list[str]
|
templates: list[str]
|
||||||
staticFilesDir: list[str]
|
static_files_dir: list[str]
|
||||||
outDir: str
|
out_dir: str
|
||||||
|
templates_dir: str
|
||||||
|
|
||||||
|
|
||||||
def load() -> Config:
|
def load() -> Config:
|
||||||
with open("config.json", "r") as f:
|
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.config import load
|
||||||
from lib.engine import FileSystemRenderer
|
from lib.engine import FileSystemRenderer
|
||||||
|
|
||||||
NAME = "name"
|
|
||||||
TEMPLATES = "templates"
|
|
||||||
TEMPLATES_DIR = "templatesDir"
|
|
||||||
STATIC_FILES_DIR = "staticFilesDir"
|
|
||||||
OUT_DIR = "outDir"
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
try:
|
try:
|
||||||
|
|
@ -22,23 +16,23 @@ def main():
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
data = {}
|
data = {}
|
||||||
destination_path = config[OUT_DIR]
|
destination_path = config.out_dir
|
||||||
fs = FileSystemRenderer(config.setdefault(TEMPLATES_DIR, TEMPLATES))
|
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):
|
if os.path.exists(destination_path) and os.path.isdir(destination_path):
|
||||||
shutil.rmtree(destination_path)
|
shutil.rmtree(destination_path)
|
||||||
os.mkdir(destination_path)
|
os.mkdir(destination_path)
|
||||||
|
|
||||||
for template in config[TEMPLATES]:
|
for template in config.templates:
|
||||||
logger.info(f"📃Render '{template}'")
|
logger.info(f"📃Render '{template}'")
|
||||||
with open(os.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)))
|
f.write(fs.render(template, data.get(template)))
|
||||||
|
|
||||||
logger.info("⏩ Start copying staticfiles to build")
|
logger.info("⏩ Start copying staticfiles to build")
|
||||||
for folder in config[STATIC_FILES_DIR]:
|
for folder in config.static_files_dir:
|
||||||
shutil.copytree(folder, os.path.join(config[OUT_DIR], folder))
|
shutil.copytree(folder, os.path.join(config.out_dir, folder))
|
||||||
|
|
||||||
logger.info("🎉 Done…")
|
logger.info("🎉 Done…")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue