aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--hyprsunset_qt/config.py44
-rw-r--r--tests/test_config.py29
2 files changed, 72 insertions, 1 deletions
diff --git a/hyprsunset_qt/config.py b/hyprsunset_qt/config.py
index c2ef010..f8ad5cf 100644
--- a/hyprsunset_qt/config.py
+++ b/hyprsunset_qt/config.py
@@ -38,3 +38,47 @@ def parse_config(text: str) -> list[Profile]:
)
)
return profiles
+
+
+_HEADER = "# Managed by hyprsunset-qt. Edits here are overwritten on save.\n"
+
+
+def _fmt_gamma(g: float) -> str:
+ return f"{g:g}"
+
+
+def serialize(profiles: list[Profile]) -> str:
+ day = day_index(profiles)
+ night = night_index(profiles)
+ parts = [_HEADER]
+ for i, p in enumerate(profiles):
+ if i == day:
+ parts.append("\n# day profile -- sunrise")
+ elif i == night:
+ parts.append("\n# night profile -- sunset")
+ else:
+ parts.append("\n# profile")
+ lines = ["profile {", f" time = {p.time}"]
+ if p.identity:
+ lines.append(" identity = true")
+ if p.temperature is not None:
+ lines.append(f" temperature = {p.temperature}")
+ if p.gamma is not None:
+ lines.append(f" gamma = {_fmt_gamma(p.gamma)}")
+ lines.append("}")
+ parts.append("\n".join(lines))
+ return "\n".join(parts) + "\n"
+
+
+def day_index(profiles: list[Profile]) -> int | None:
+ for i, p in enumerate(profiles):
+ if p.identity:
+ return i
+ return None
+
+
+def night_index(profiles: list[Profile]) -> int | None:
+ for i, p in enumerate(profiles):
+ if p.temperature is not None:
+ return i
+ return None
diff --git a/tests/test_config.py b/tests/test_config.py
index 4568160..62acd97 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -1,4 +1,4 @@
-from hyprsunset_qt.config import Profile, parse_config
+from hyprsunset_qt.config import Profile, parse_config, serialize, day_index, night_index
def test_parse_two_profiles():
@@ -23,3 +23,30 @@ profile {
def test_parse_empty():
assert parse_config("") == []
+
+
+def test_serialize_roundtrip():
+ profiles = [
+ Profile(time="5:00", identity=True),
+ Profile(time="19:40", temperature=5500, gamma=0.8),
+ ]
+ text = serialize(profiles)
+ assert parse_config(text) == profiles
+ assert "identity = true" in text
+ assert "temperature = 5500" in text
+ assert "gamma = 0.8" in text
+ assert text.startswith("# Managed by hyprsunset-qt")
+
+
+def test_day_night_index():
+ profiles = [
+ Profile(time="5:00", identity=True),
+ Profile(time="19:40", temperature=5500, gamma=0.8),
+ ]
+ assert day_index(profiles) == 0
+ assert night_index(profiles) == 1
+
+
+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