summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--hyprsunset_qt/config.py17
-rw-r--r--tests/test_config.py18
2 files changed, 34 insertions, 1 deletions
diff --git a/hyprsunset_qt/config.py b/hyprsunset_qt/config.py
index f8ad5cf..a718861 100644
--- a/hyprsunset_qt/config.py
+++ b/hyprsunset_qt/config.py
@@ -4,6 +4,7 @@ from __future__ import annotations
import re
from dataclasses import dataclass
+from pathlib import Path
_BLOCK_RE = re.compile(r"profile\s*\{(.*?)\}", re.DOTALL)
@@ -82,3 +83,19 @@ def night_index(profiles: list[Profile]) -> int | None:
if p.temperature is not None:
return i
return None
+
+
+CONFIG_PATH = Path.home() / ".config" / "hypr" / "hyprsunset.conf"
+
+
+def read_config(path: Path = CONFIG_PATH) -> list[Profile]:
+ path = Path(path)
+ if not path.exists():
+ return []
+ return parse_config(path.read_text())
+
+
+def write_config(profiles: list[Profile], path: Path = CONFIG_PATH) -> None:
+ path = Path(path)
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text(serialize(profiles))
diff --git a/tests/test_config.py b/tests/test_config.py
index 62acd97..1847449 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -1,4 +1,5 @@
-from hyprsunset_qt.config import Profile, parse_config, serialize, day_index, night_index
+from pathlib import Path
+from hyprsunset_qt.config import Profile, parse_config, serialize, day_index, night_index, read_config, write_config, CONFIG_PATH
def test_parse_two_profiles():
@@ -50,3 +51,18 @@ def test_day_night_index():
def test_day_night_index_missing():
assert day_index([Profile(time="1:00", temperature=4000)]) is None
assert night_index([Profile(time="1:00", identity=True)]) is None
+
+
+def test_read_missing_returns_empty(tmp_path):
+ assert read_config(tmp_path / "nope.conf") == []
+
+
+def test_write_then_read(tmp_path):
+ path = tmp_path / "hyprsunset.conf"
+ profiles = [Profile(time="19:40", temperature=5500, gamma=0.8)]
+ write_config(profiles, path)
+ assert read_config(path) == profiles
+
+
+def test_default_path_is_hypr_conf():
+ assert str(CONFIG_PATH).endswith(".config/hypr/hyprsunset.conf")