aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--hyprsunset_qt/settings.py53
-rw-r--r--tests/test_settings.py26
2 files changed, 79 insertions, 0 deletions
diff --git a/hyprsunset_qt/settings.py b/hyprsunset_qt/settings.py
new file mode 100644
index 0000000..d9cca39
--- /dev/null
+++ b/hyprsunset_qt/settings.py
@@ -0,0 +1,53 @@
+"""App settings stored in ~/.config/hyprsunset-qt/config (INI)."""
+
+from __future__ import annotations
+
+import configparser
+from dataclasses import dataclass
+from pathlib import Path
+
+SETTINGS_PATH = Path.home() / ".config" / "hyprsunset-qt" / "config"
+DEFAULT_CACHE = "~/.config/hyprsunset-qt/sun.json"
+
+
+@dataclass
+class Settings:
+ lat: str = ""
+ lon: str = ""
+ auto_detect: bool = True
+ cache_path: str = DEFAULT_CACHE
+ daemon_command: str = "hyprsunset"
+
+ @classmethod
+ def load(cls, path: Path = SETTINGS_PATH) -> "Settings":
+ path = Path(path)
+ if not path.exists():
+ s = cls()
+ s.save(path)
+ return s
+ cp = configparser.ConfigParser()
+ cp.read(path)
+ return cls(
+ lat=cp.get("location", "lat", fallback=""),
+ lon=cp.get("location", "lon", fallback=""),
+ auto_detect=cp.getboolean("location", "auto_detect", fallback=True),
+ cache_path=cp.get("cache", "path", fallback=DEFAULT_CACHE),
+ daemon_command=cp.get("daemon", "command", fallback="hyprsunset"),
+ )
+
+ def save(self, path: Path = SETTINGS_PATH) -> None:
+ path = Path(path)
+ path.parent.mkdir(parents=True, exist_ok=True)
+ cp = configparser.ConfigParser()
+ cp["location"] = {
+ "lat": self.lat,
+ "lon": self.lon,
+ "auto_detect": str(self.auto_detect).lower(),
+ }
+ cp["cache"] = {"path": self.cache_path}
+ cp["daemon"] = {"command": self.daemon_command}
+ with path.open("w") as f:
+ cp.write(f)
+
+ def cache_path_expanded(self) -> Path:
+ return Path(self.cache_path).expanduser()
diff --git a/tests/test_settings.py b/tests/test_settings.py
new file mode 100644
index 0000000..c56896a
--- /dev/null
+++ b/tests/test_settings.py
@@ -0,0 +1,26 @@
+from pathlib import Path
+from hyprsunset_qt.settings import Settings
+
+
+def test_defaults_created_on_first_run(tmp_path):
+ path = tmp_path / "config"
+ s = Settings.load(path)
+ assert path.exists()
+ assert s.auto_detect is True
+ assert s.daemon_command == "hyprsunset"
+
+
+def test_roundtrip_and_tilde_expansion(tmp_path):
+ path = tmp_path / "config"
+ s = Settings.load(path)
+ s.lat = "45.07"
+ s.lon = "7.68"
+ s.auto_detect = False
+ s.cache_path = "~/somewhere/sun.json"
+ s.save(path)
+
+ s2 = Settings.load(path)
+ assert s2.lat == "45.07"
+ assert s2.lon == "7.68"
+ assert s2.auto_detect is False
+ assert s2.cache_path_expanded() == Path.home() / "somewhere" / "sun.json"