Initial commit
App works locally. No intention to bring this live.
This commit is contained in:
45
config.py
Normal file
45
config.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import json
|
||||
import os
|
||||
from dataclasses import asdict, dataclass
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def _tafa_dir() -> Path:
|
||||
p = Path.home() / ".tafa"
|
||||
p.mkdir(parents=True, exist_ok=True)
|
||||
return p
|
||||
|
||||
|
||||
def config_path() -> Path:
|
||||
return _tafa_dir() / "config.json"
|
||||
|
||||
|
||||
@dataclass
|
||||
class AppConfig:
|
||||
sources: list[str]
|
||||
date_start: str
|
||||
date_end: str
|
||||
destination: str
|
||||
|
||||
|
||||
def load_config() -> AppConfig | None:
|
||||
path = config_path()
|
||||
if not path.exists():
|
||||
return None
|
||||
try:
|
||||
data = json.loads(path.read_text())
|
||||
return AppConfig(
|
||||
sources=data["sources"],
|
||||
date_start=data["date_start"],
|
||||
date_end=data["date_end"],
|
||||
destination=data["destination"],
|
||||
)
|
||||
except (KeyError, json.JSONDecodeError, TypeError):
|
||||
return None
|
||||
|
||||
|
||||
def save_config(cfg: AppConfig) -> None:
|
||||
path = config_path()
|
||||
tmp = path.with_suffix(".tmp")
|
||||
tmp.write_text(json.dumps(asdict(cfg), indent=2))
|
||||
os.replace(tmp, path)
|
||||
Reference in New Issue
Block a user