87 lines
3.4 KiB
Python
87 lines
3.4 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
from dataclasses import asdict, dataclass, fields
|
|
from pathlib import Path
|
|
from typing import Optional
|
|
|
|
from .model import Snapshot, snapshot_from_dict, snapshot_to_dict
|
|
|
|
DEFAULT_URL = "https://meteoinfo.ru/pogoda/russia/moscow-area/moscow"
|
|
ANCHORS = ["top-left", "top-right", "bottom-left", "bottom-right"]
|
|
LAYERS = ["bottom", "normal", "top"]
|
|
TRAY_STYLES = ["stack", "pair", "wide"]
|
|
WINDOW_TYPES = ["auto", "normal", "dock", "desktop"]
|
|
|
|
CONFIG_DIR = Path(os.environ.get("XDG_CONFIG_HOME") or Path.home() / ".config") / "ruweather"
|
|
CACHE_DIR = Path(os.environ.get("XDG_CACHE_HOME") or Path.home() / ".cache") / "ruweather"
|
|
|
|
|
|
@dataclass
|
|
class Config:
|
|
station_url: str = DEFAULT_URL
|
|
update_minutes: int = 30
|
|
anchor: str = "bottom-right"
|
|
margin_x: int = 24
|
|
margin_y: int = 24
|
|
custom_pos: Optional[list] = None # [x, y] после перетаскивания мышью
|
|
font_size: int = 11
|
|
bg_opacity: int = 45 # % непрозрачности фона, 0 — совсем прозрачный (как conky)
|
|
text_color: str = "#e8e8e8"
|
|
layer: str = "bottom" # bottom — под окнами, normal, top — поверх
|
|
click_through: bool = False # клики проходят «сквозь» окно
|
|
forecast_rows: int = 4
|
|
visible: bool = True
|
|
window_type: str = "auto" # auto: в KDE — dock (не скрывается при «Показать рабочий стол»)
|
|
tray_style: str = "stack" # stack — один значок, pair — два значка рядом, wide — один широкий
|
|
tray_swap: bool = False # поменять местами два значка (порядок в трее задаёт сама панель)
|
|
|
|
@classmethod
|
|
def load(cls) -> "Config":
|
|
cfg = cls()
|
|
try:
|
|
data = json.loads((CONFIG_DIR / "config.json").read_text(encoding="utf-8"))
|
|
except (OSError, ValueError):
|
|
return cfg
|
|
known = {f.name: f for f in fields(cls)}
|
|
for key, value in data.items():
|
|
if key in known:
|
|
try:
|
|
setattr(cfg, key, value)
|
|
except Exception: # noqa: BLE001
|
|
pass
|
|
cfg.update_minutes = max(30, min(360, int(cfg.update_minutes)))
|
|
if cfg.anchor not in ANCHORS:
|
|
cfg.anchor = "bottom-right"
|
|
if cfg.layer not in LAYERS:
|
|
cfg.layer = "bottom"
|
|
if cfg.window_type not in WINDOW_TYPES:
|
|
cfg.window_type = "auto"
|
|
if cfg.tray_style not in TRAY_STYLES:
|
|
cfg.tray_style = "stack"
|
|
return cfg
|
|
|
|
def save(self) -> None:
|
|
CONFIG_DIR.mkdir(parents=True, exist_ok=True)
|
|
(CONFIG_DIR / "config.json").write_text(
|
|
json.dumps(asdict(self), ensure_ascii=False, indent=2), encoding="utf-8"
|
|
)
|
|
|
|
|
|
def load_cache() -> Optional[Snapshot]:
|
|
try:
|
|
return snapshot_from_dict(json.loads((CACHE_DIR / "snapshot.json").read_text(encoding="utf-8")))
|
|
except Exception: # noqa: BLE001
|
|
return None
|
|
|
|
|
|
def save_cache(snap: Snapshot) -> None:
|
|
try:
|
|
CACHE_DIR.mkdir(parents=True, exist_ok=True)
|
|
(CACHE_DIR / "snapshot.json").write_text(
|
|
json.dumps(snapshot_to_dict(snap), ensure_ascii=False), encoding="utf-8"
|
|
)
|
|
except OSError:
|
|
pass
|