mirror of
https://github.com/rjNemo/vf-site
synced 2026-06-06 01:16:38 +00:00
refactor
This commit is contained in:
parent
65604a72c7
commit
5e39e5920a
4 changed files with 29 additions and 27 deletions
2
Pipfile
2
Pipfile
|
|
@ -13,4 +13,4 @@ mypy = "*"
|
|||
ruff = "*"
|
||||
|
||||
[requires]
|
||||
python_version = "3.9"
|
||||
python_version = "3.11"
|
||||
|
|
|
|||
4
Pipfile.lock
generated
4
Pipfile.lock
generated
|
|
@ -1,11 +1,11 @@
|
|||
{
|
||||
"_meta": {
|
||||
"hash": {
|
||||
"sha256": "fcc9e26a9ee4645cb0e75631215df5ca39ce5517d62bd3970932fb1f7c9d69cb"
|
||||
"sha256": "4c4b43f1ede1c6a64f569dbd3bbfdc2c6aabfe004db06d43fe42496847026061"
|
||||
},
|
||||
"pipfile-spec": 6,
|
||||
"requires": {
|
||||
"python_version": "3.9"
|
||||
"python_version": "3.11"
|
||||
},
|
||||
"sources": [
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
46
lib/main.py
46
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:
|
||||
|
|
|
|||
Loading…
Reference in a new issue