Pi66

Home | Repos | About | Blog | Gaza

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

About | Files | Commits | Refs | License


fix: use .get() with defaults for all config fields

Author: pi66
Date: 2026-06-27 22:38
commit: c85ffb90

Stat

M src/config.py 59 +26 -33

Diff

diff --git a/src/config.py b/src/config.py
index 366c215..86816a8 100644
--- a/src/config.py
+++ b/src/config.py
@@ -1,7 +1,6 @@
 from os import makedirs, path
 from os.path import exists
 from tomllib import loads
-
 from .fileops import read_file
 from .log import GRAY
 from .modules import Config, Extras, Project, Tree, Webview
@@ -10,8 +9,8 @@ def init_config_struct(project_name:str) -> Config:
     """Build a Config with default values for a newly initialized project."""
     return Config(
         project=Project(
-            name = project_name,
-            version = "0.1.0",
+            name        = project_name,
+            version     = "0.1.0",

...
Show all
diff --git a/src/config.py b/src/config.py
index 366c215..86816a8 100644
--- a/src/config.py
+++ b/src/config.py
@@ -1,7 +1,6 @@
 from os import makedirs, path
 from os.path import exists
 from tomllib import loads
-
 from .fileops import read_file
 from .log import GRAY
 from .modules import Config, Extras, Project, Tree, Webview
@@ -10,8 +9,8 @@ def init_config_struct(project_name:str) -> Config:
     """Build a Config with default values for a newly initialized project."""
     return Config(
         project=Project(
-            name = project_name,
-            version = "0.1.0",
+            name        = project_name,
+            version     = "0.1.0",
             description = "Add your description here",
         ),
         tree = Tree(
@@ -22,18 +21,17 @@ def init_config_struct(project_name:str) -> Config:
             plugins   = "src/plugins.py",
         ),
         webview = Webview(
-            host = "localhost",
-            port = 8866,
-            dev_tools = "true",
+            host        = "localhost",
+            port        = 8866,
+            dev_tools   = False,
             html_path   = "/",
             static_path = "/static"
         ),
-        extras= Extras(
+        extras = Extras(
             highlight = "monokai"
         )
     )

- def render_config(config:Config) -> str: return f"""[project] name = "{config.project.name}" @@ -48,9 +46,9 @@ dest = "{config.tree.dest}" plugins = "{config.tree.plugins}"

[webview] -host = "{config.webview.host}" -port = {config.webview.port} -dev_tools = {config.webview.dev_tools} +host = "{config.webview.host}" +port = {config.webview.port} +dev_tools = {str(config.webview.dev_tools).lower()} html_path = "{config.webview.html_path}" static_path = "{config.webview.static_path}"

@@ -65,26 +63,22 @@ def find_project_from_path(project_path: str): raise Exception(f"cannot find `config.toml` in: {GRAY(project_path)}")

def load_tree_config(project_path) -> Tree: - config_raw_content = read_file(path.join(project_path,"config.toml")) - config = loads(config_raw_content) - markdown = path.join(project_path, config["tree"]["markdown"]) - static = path.join(project_path, config["tree"]["static"]) - templates = path.join(project_path, config["tree"]["templates"]) - dest = path.join(project_path, config["tree"]["dest"]) - plugins = path.join(project_path, config["tree"]["plugins"]) + config_raw_content = read_file(path.join(project_path, "config.toml")) + config = loads(config_raw_content) + markdown = path.join(project_path, config["tree"].get("markdown", "src/md")) + static = path.join(project_path, config["tree"].get("static", "src/static")) + templates = path.join(project_path, config["tree"].get("templates", "src/templates")) + dest = path.join(project_path, config["tree"].get("dest", "src/dest")) + plugins = path.join(project_path, config["tree"].get("plugins", "src/plugins.py"))

 if not exists(markdown):
     raise FileNotFoundError(f"markdown directory does not exist: {markdown}")

- if not exists(static): raise FileNotFoundError(f"static directory does not exist: {static}") - if not exists(templates): raise FileNotFoundError(f"templates directory does not exist: {templates}") - if not exists(plugins): raise FileNotFoundError(f"plugins file does not exist: {plugins}") - if not exists(dest): makedirs(dest)

@@ -96,21 +90,20 @@ def load_tree_config(project_path) -> Tree: plugins = plugins, )

- def load_webview_config(project_path:str) -> Webview: - config_raw_content = read_file(path.join(project_path,"config.toml")) - config = loads(config_raw_content) + config_raw_content = read_file(path.join(project_path, "config.toml")) + config = loads(config_raw_content) return Webview( - host = config["webview"]["host"], - port = config["webview"]["port"], - dev_tools = config["webview"]["dev_tools"], - html_path = config["webview"]["html_path"], - static_path = config["webview"]["static_path"], + 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(project_path:str) -> Extras: - config_raw_content = read_file(path.join(project_path,"config.toml")) - config = loads(config_raw_content) + config_raw_content = read_file(path.join(project_path, "config.toml")) + config = loads(config_raw_content) return Extras( - highlight = config["extras"]["highlight"], + highlight = config["extras"].get("highlight", "monokai"), )

// Created by Pi66

// Copyright ©Pi66 2026