Initial commit

This commit is contained in:
Lina Rusakova committed 2026-11-11 01:26:17 +03:00
commit efa05921fc
10 files changed
+1063

No files matched your search

+77
View File
@@ -0,0 +1,77 @@
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"]
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
@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"
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