-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcurrent_display_state.py
More file actions
140 lines (117 loc) · 3.97 KB
/
current_display_state.py
File metadata and controls
140 lines (117 loc) · 3.97 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import json
from datetime import datetime
from config import DB_PATH
from db_manager import connect
DISPLAY_STATE_VERSION = 1
def empty_current_display_state() -> dict:
return {
"snapshot_version": DISPLAY_STATE_VERSION,
"has_data": False,
"updated_at": "",
"time": "",
"slot": "",
"category": "",
"setup": "",
"punchline": "",
"data": {},
}
def _stringify(value) -> str:
if value is None:
return "--"
return str(value)
def _join_parts(*parts: str) -> str:
return " | ".join(part for part in parts if part)
def _normalize_dashboard_fields(category: str, data: dict) -> tuple[str, str]:
if category == "pokemon":
types = data.get("types") or []
type_text = " / ".join(str(value) for value in types if value) or "Unknown"
return (
str(data.get("name", "Pokemon unavailable")),
_join_parts(
type_text,
f"HP {_stringify(data.get('hp'))}",
f"ATK {_stringify(data.get('attack'))}",
f"DEF {_stringify(data.get('defense'))}",
),
)
if category == "weather":
return (
str(data.get("location", "Unknown location")),
_join_parts(
str(data.get("condition", "Unknown")),
f"{_stringify(data.get('temperature_f'))}F",
f"Wind {_stringify(data.get('wind_mph'))} mph",
),
)
if category == "joke":
if data.get("type") == "twopart":
return (
str(data.get("setup") or ""),
str(data.get("delivery") or ""),
)
return (
str(data.get("text") or ""),
"",
)
if category == "science":
symbol = str(data.get("symbol", "?"))
atomic_number = _stringify(data.get("atomic_number"))
return (
f"{data.get('name', 'Unknown')} ({symbol})",
f"Atomic {atomic_number}",
)
return ("", "")
def normalize_current_display_state(
payload: dict, updated_at: str | None = None
) -> dict:
category = str(payload.get("category", ""))
data = payload.get("data") or {}
snapshot_time = updated_at or datetime.now().isoformat(timespec="seconds")
setup, punchline = _normalize_dashboard_fields(category, data)
return {
"snapshot_version": DISPLAY_STATE_VERSION,
"has_data": True,
"updated_at": snapshot_time,
"time": str(payload.get("time", "")),
"slot": str(payload.get("slot_key", "")),
"category": category,
"setup": setup,
"punchline": punchline,
"data": data,
}
def save_current_display_state(
payload: dict, db_path: str = DB_PATH, updated_at: str | None = None
) -> dict:
state = normalize_current_display_state(payload, updated_at=updated_at)
serialized_state = json.dumps(state, sort_keys=True)
conn = connect(db_path)
try:
conn.execute(
"""
INSERT INTO current_display_state (id, state_json, updated_at)
VALUES (1, ?, ?)
ON CONFLICT(id) DO UPDATE
SET state_json = excluded.state_json,
updated_at = excluded.updated_at
""",
(serialized_state, state["updated_at"]),
)
conn.commit()
return state
finally:
conn.close()
def load_current_display_state(db_path: str = DB_PATH) -> dict:
conn = connect(db_path)
try:
cur = conn.cursor()
cur.execute("SELECT state_json FROM current_display_state WHERE id = 1")
row = cur.fetchone()
if row is None or not row["state_json"]:
return empty_current_display_state()
state = json.loads(row["state_json"])
empty_state = empty_current_display_state()
empty_state.update(state)
empty_state["has_data"] = bool(state.get("has_data"))
return empty_state
finally:
conn.close()