--- /dev/null
+from __future__ import annotations
+from dataclasses import dataclass
+from pathlib import Path
+import tomlkit
+
+DEFAULT_CONFIG_PATH = Path.home() / ".config" / "my-publisher" / "config.toml"
+
+@dataclass
+class Config:
+ blog_repo: str = ""
+ transart_script: str = "/home/danix/bin/transart.py"
+ typora_bin: str = "typora"
+
+ def is_complete(self) -> bool:
+ return bool(self.blog_repo)
+
+ def save(self, path: Path | None = None) -> None:
+ import core.config as _mod
+ if path is None:
+ path = _mod.DEFAULT_CONFIG_PATH
+ path.parent.mkdir(parents=True, exist_ok=True)
+ doc = tomlkit.document()
+ doc.add("blog_repo", self.blog_repo)
+ doc.add("transart_script", self.transart_script)
+ doc.add("typora_bin", self.typora_bin)
+ path.write_text(tomlkit.dumps(doc))
+
+ @classmethod
+ def load(cls, path: Path | None = None) -> "Config":
+ import core.config as _mod
+ if path is None:
+ path = _mod.DEFAULT_CONFIG_PATH
+ if not path.exists():
+ return cls()
+ data = tomlkit.loads(path.read_text())
+ return cls(
+ blog_repo=str(data.get("blog_repo", "")),
+ transart_script=str(data.get("transart_script", "/home/danix/bin/transart.py")),
+ typora_bin=str(data.get("typora_bin", "typora")),
+ )
--- /dev/null
+import pytest
+from pathlib import Path
+import tempfile, os
+from core.config import Config, DEFAULT_CONFIG_PATH
+
+def test_config_loads_defaults_when_missing(tmp_path, monkeypatch):
+ monkeypatch.setattr("core.config.DEFAULT_CONFIG_PATH", tmp_path / "config.toml")
+ cfg = Config.load()
+ assert cfg.blog_repo == ""
+ assert cfg.transart_script == "/home/danix/bin/transart.py"
+ assert cfg.typora_bin == "typora"
+
+def test_config_round_trips(tmp_path, monkeypatch):
+ path = tmp_path / "config.toml"
+ monkeypatch.setattr("core.config.DEFAULT_CONFIG_PATH", path)
+ cfg = Config(blog_repo="/some/path", transart_script="/bin/x", typora_bin="typora")
+ cfg.save()
+ loaded = Config.load()
+ assert loaded.blog_repo == "/some/path"
+
+def test_config_is_complete_false_when_blog_repo_empty(tmp_path, monkeypatch):
+ monkeypatch.setattr("core.config.DEFAULT_CONFIG_PATH", tmp_path / "config.toml")
+ cfg = Config.load()
+ assert cfg.is_complete() is False
+
+def test_config_is_complete_true_when_all_set(tmp_path, monkeypatch):
+ monkeypatch.setattr("core.config.DEFAULT_CONFIG_PATH", tmp_path / "config.toml")
+ cfg = Config(blog_repo="/some/repo", transart_script="/bin/x", typora_bin="typora")
+ assert cfg.is_complete() is True