Extract store helpers into storage_helpers.py
auto_out_path, load_store, save_store moved out of data_model.py into their own module. data_model.py now contains only value helpers and domain extractors. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+2
-1
@@ -58,7 +58,8 @@ from pathlib import Path
|
|||||||
import log_config
|
import log_config
|
||||||
import network
|
import network
|
||||||
import we_connect
|
import we_connect
|
||||||
from data_model import ALL_DOMAINS, auto_out_path, collect_snapshot, load_store, save_store
|
from data_model import ALL_DOMAINS, collect_snapshot
|
||||||
|
from storage_helpers import auto_out_path, load_store, save_store
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
import json
|
|
||||||
import logging
|
import logging
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -245,34 +243,3 @@ def collect_snapshot(vehicle, domains: list[str]) -> dict:
|
|||||||
if data is not None:
|
if data is not None:
|
||||||
snapshot[domain] = data
|
snapshot[domain] = data
|
||||||
return snapshot
|
return snapshot
|
||||||
|
|
||||||
|
|
||||||
# ── store helpers ─────────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
def auto_out_path(logs_dir: Path, vin: str) -> Path:
|
|
||||||
stamp = datetime.now(timezone.utc).strftime("%Y_%m_%d_%H_%M_%S")
|
|
||||||
return logs_dir / f"{stamp}_{vin}.json"
|
|
||||||
|
|
||||||
|
|
||||||
def load_store(path: Path, vin: str, interval: int) -> dict:
|
|
||||||
if path.exists():
|
|
||||||
try:
|
|
||||||
data = json.loads(path.read_text())
|
|
||||||
if isinstance(data, dict) and "records" in data:
|
|
||||||
return data
|
|
||||||
except json.JSONDecodeError:
|
|
||||||
log.warning("Output file is corrupt — starting fresh")
|
|
||||||
return {
|
|
||||||
"meta": {
|
|
||||||
"vin": vin,
|
|
||||||
"created_at": datetime.now(timezone.utc).isoformat(),
|
|
||||||
"interval_seconds": interval,
|
|
||||||
},
|
|
||||||
"records": [],
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def save_store(path: Path, store: dict, max_records: int | None) -> None:
|
|
||||||
if max_records and len(store["records"]) > max_records:
|
|
||||||
store["records"] = store["records"][-max_records:]
|
|
||||||
path.write_text(json.dumps(store, indent=2, default=str))
|
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
import json
|
||||||
|
import logging
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def auto_out_path(logs_dir: Path, vin: str) -> Path:
|
||||||
|
stamp = datetime.now(timezone.utc).strftime("%Y_%m_%d_%H_%M_%S")
|
||||||
|
return logs_dir / f"{stamp}_{vin}.json"
|
||||||
|
|
||||||
|
|
||||||
|
def load_store(path: Path, vin: str, interval: int) -> dict:
|
||||||
|
if path.exists():
|
||||||
|
try:
|
||||||
|
data = json.loads(path.read_text())
|
||||||
|
if isinstance(data, dict) and "records" in data:
|
||||||
|
return data
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
log.warning("Output file is corrupt — starting fresh")
|
||||||
|
return {
|
||||||
|
"meta": {
|
||||||
|
"vin": vin,
|
||||||
|
"created_at": datetime.now(timezone.utc).isoformat(),
|
||||||
|
"interval_seconds": interval,
|
||||||
|
},
|
||||||
|
"records": [],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def save_store(path: Path, store: dict, max_records: int | None) -> None:
|
||||||
|
if max_records and len(store["records"]) > max_records:
|
||||||
|
store["records"] = store["records"][-max_records:]
|
||||||
|
path.write_text(json.dumps(store, indent=2, default=str))
|
||||||
Reference in New Issue
Block a user