mirror of
https://github.com/rjNemo/vf-site
synced 2026-06-12 12:06:39 +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 = "*"
|
ruff = "*"
|
||||||
|
|
||||||
[requires]
|
[requires]
|
||||||
python_version = "3.9"
|
python_version = "3.11"
|
||||||
|
|
|
||||||
4
Pipfile.lock
generated
4
Pipfile.lock
generated
|
|
@ -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": [
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -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",
|
||||||
|
|
|
||||||
46
lib/main.py
46
lib/main.py
|
|
@ -7,37 +7,41 @@ 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()
|
||||||
|
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:
|
try:
|
||||||
config = load()
|
config = load()
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
logger.error("the configuration file 'config.toml' was not found. Please verify it exists at the root level")
|
logger.error("the configuration file 'config.toml' was not found. Please verify it exists at the root level")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
else:
|
||||||
destination_path = config.out_dir
|
return config
|
||||||
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")
|
|
||||||
|
|
||||||
|
|
||||||
def parse_data(page: os.DirEntry, data_dir: str) -> dict:
|
def parse_data(page: os.DirEntry, data_dir: str) -> dict:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue