summaryrefslogtreecommitdiffstats
path: root/hyprsunset_qt/config.py
blob: c2ef010738125cdc86055ce20ac1252a5fab511b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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