Pi66

merodi
A markdown-based static site generator.
git clone https://git.pi66.xyz/merodi

← back to log

refactor:simplify config loading

author: pixel
date: 2026-07-22 09:36
hash: c0452310a4e16fe50447de48d3ef61c0c465cb0d

Diffstat:

M

src/config.py
53 +++++++++++-------------------
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
diff --git a/src/config.py b/src/config.py
index c5233e9..47340df 100644
--- a/src/config.py
+++ b/src/config.py
@@ -8,21 +8,17 @@ from .hooks import hook_call
 from .api import api


-def find_project_from_path(project_path: str):
+def find_project_from_path(project_path):
     if not path.exists(project_path):
         raise Exception("project does not exist")
     if not path.exists(path.join(project_path, "config.toml")):
         raise Exception(f"cannot find `config.toml` in: {GRAY(project_path)}")

-def load_project_config(config) -> Project:
-    return Project(
-        name        = config["project"].get("name", "project"),
-        version     = config["project"].get("version", "0.1.0"),
-        description = config["project"].get("description", "Add your description here"),
-    ) 
+def load_project_config(c) -> Project:
+    return Project(name=c["project"]["name"], version=c["project"]["version"], description=c["project"]["description"])

-def load_tree_config(config) -> Tree:
-    t = config["tree"]
+def load_tree_config(c) -> Tree:
+    t = c["tree"]
     markdown, static, templates = t["markdown"], t["static"], t["templates"]
     draft_dest, release_dest, plugins = t["draft_dest"], t["release_dest"], t["plugins"]
     for name, p in [("markdown", markdown), ("static", static), ("templates", templates), ("plugins", plugins)]:
@@ -30,37 +26,26 @@ def load_tree_config(config) -> Tree:
             raise FileNotFoundError(f"{name} directory does not exist: {p}")
     return Tree(markdown=markdown, static=static, templates=templates, draft_dest=draft_dest, release_dest=release_dest, plugins=plugins)

+def load_webview_config(c) -> Webview:
+    w = c["webview"]
+    return Webview(host=w["host"], port=w["port"], dev_tools=w["dev_tools"], html_path=w["html_path"], static_path=w["static_path"])

-def load_webview_config(config) -> Webview:
-    return Webview(
-        host        = config["webview"].get("host",        "localhost"),
-        port        = config["webview"].get("port",        8866),
-        dev_tools   = config["webview"].get("dev_tools",   False),
-        html_path   = config["webview"].get("html_path",   "/"),
-        static_path = config["webview"].get("static_path", "/static"),
-    )
+def load_extras_config(c) -> Extras:
+    return Extras(highlight=c["extras"]["highlight"])

-def load_extras_config(config) -> Extras:
-    return Extras(
-        highlight = config["extras"].get("highlight", "monokai"),
-    )
-
-def load_cache_config(config) -> Cache:
-    return Cache(
-        hash = config["cache"].get("hash", ".hash.cache"),
-    )
+def load_cache_config(c) -> Cache:
+    return Cache(hash=c["cache"]["hash"])

 def load_config() -> Config:
-    config_raw_content = read_file("config.toml")
-    config_loaded = loads(config_raw_content)
+    raw = read_file("config.toml")
+    c = loads(raw)
     config = Config(
-        project = load_project_config(config_loaded),
-        tree    = load_tree_config(config_loaded),
-        webview = load_webview_config(config_loaded),
-        extras  = load_extras_config(config_loaded),
-        cache   = load_cache_config(config_loaded),
+        project=load_project_config(c),
+        tree=load_tree_config(c),
+        webview=load_webview_config(c),
+        extras=load_extras_config(c),
+        cache=load_cache_config(c),
     )
     config = hook_call("on_config_load", config) or config
     api.config = config
-
     return config