summaryrefslogtreecommitdiff
path: root/raveos-center/raveos_center/beallitasok.py
blob: e041706eb1ffdf0525c48ea413a59dc990fee495 (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
import json
import os
import threading
from pathlib import Path

KONFIG = Path(os.environ.get("XDG_CONFIG_HOME", Path.home() / ".config")) / "raveos-center"
FAJL = KONFIG / "beallitasok.json"
_zar = threading.Lock()


def _olvas():
    try:
        return json.loads(FAJL.read_text())
    except (OSError, ValueError):
        return {}


def ertek(kulcs, alap=None):
    with _zar:
        return _olvas().get(kulcs, alap)


def beallit(kulcs, ertek_):
    with _zar:
        adat = _olvas()
        adat[kulcs] = ertek_
        KONFIG.mkdir(parents=True, exist_ok=True)
        ideiglenes = FAJL.with_suffix(".uj")
        ideiglenes.write_text(json.dumps(adat, ensure_ascii=False, indent=2))
        os.replace(ideiglenes, FAJL)