aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-07-17 09:33:16 +0200
committerDanilo M. <danix@danix.xyz>2026-07-17 09:33:16 +0200
commit73840b30b9943e9b7de03e6172dbe7a51582ff94 (patch)
tree50f76381d696b26f9c707f925af2c79b73e003b7
parent14c2236a733a65a58bf2cc8ccb6406718635cf49 (diff)
downloadhyprsunset-qt-73840b30b9943e9b7de03e6172dbe7a51582ff94.tar.gz
hyprsunset-qt-73840b30b9943e9b7de03e6172dbe7a51582ff94.zip
feat: parse hyprsunset.conf profiles
Implement minimal config parser with Profile dataclass and parse_config function. Regex-based parsing of hyprsunset.conf format with support for time, identity, temperature, and gamma fields. Foundation for later tasks (serialize, read/write, validation). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
-rw-r--r--hyprsunset_qt/config.py40
-rw-r--r--tests/test_config.py25
2 files changed, 65 insertions, 0 deletions
diff --git a/hyprsunset_qt/config.py b/hyprsunset_qt/config.py
new file mode 100644
index 0000000..c2ef010
--- /dev/null
+++ b/hyprsunset_qt/config.py
@@ -0,0 +1,40 @@
+"""Parse and serialize hyprsunset.conf profile blocks."""
+
+from __future__ import annotations
+
+import re
+from dataclasses import dataclass
+
+_BLOCK_RE = re.compile(r"profile\s*\{(.*?)\}", re.DOTALL)
+
+
+@dataclass
+class Profile:
+ time: str = ""
+ identity: bool = False
+ temperature: int | None = None
+ gamma: float | None = None
+
+
+def _get(body: str, key: str) -> str | None:
+ m = re.search(rf"^\s*{key}\s*=\s*(.+?)\s*$", body, re.MULTILINE)
+ return m.group(1) if m else None
+
+
+def parse_config(text: str) -> list[Profile]:
+ profiles: list[Profile] = []
+ for block in _BLOCK_RE.finditer(text):
+ body = block.group(1)
+ time = _get(body, "time") or ""
+ identity = (_get(body, "identity") or "").lower() == "true"
+ temp = _get(body, "temperature")
+ gamma = _get(body, "gamma")
+ profiles.append(
+ Profile(
+ time=time,
+ identity=identity,
+ temperature=int(temp) if temp is not None else None,
+ gamma=float(gamma) if gamma is not None else None,
+ )
+ )
+ return profiles
diff --git a/tests/test_config.py b/tests/test_config.py
new file mode 100644
index 0000000..4568160
--- /dev/null
+++ b/tests/test_config.py
@@ -0,0 +1,25 @@
+from hyprsunset_qt.config import Profile, parse_config
+
+
+def test_parse_two_profiles():
+ text = """
+profile {
+ time = 5:00
+ identity = true
+}
+
+profile {
+ time = 19:40
+ temperature = 5500
+ gamma = 0.8
+}
+"""
+ profiles = parse_config(text)
+ assert profiles == [
+ Profile(time="5:00", identity=True, temperature=None, gamma=None),
+ Profile(time="19:40", identity=False, temperature=5500, gamma=0.8),
+ ]
+
+
+def test_parse_empty():
+ assert parse_config("") == []