-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
42 lines (32 loc) · 1.01 KB
/
utils.py
File metadata and controls
42 lines (32 loc) · 1.01 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
#!/usr/bin/env python3
"""Shared utilities for the wellbeing kit."""
import json
from datetime import datetime, date
from pathlib import Path
WELLBEING_DIR = Path(__file__).resolve().parent
STATE_FILE = WELLBEING_DIR / "state.json"
CONFIG_FILE = WELLBEING_DIR / "config.json"
def load_state():
"""Load persistent state from state.json."""
try:
return json.loads(STATE_FILE.read_text())
except Exception:
return {}
def save_state(state):
"""Write state back to state.json."""
STATE_FILE.write_text(json.dumps(state, indent=2))
def load_config():
"""Load configuration from config.json."""
try:
return json.loads(CONFIG_FILE.read_text())
except Exception:
return {}
def already_sent_today(state, key):
"""Check if a nudge identified by key was already sent today."""
sent_at = state.get(key)
if not sent_at:
return False
try:
return datetime.fromisoformat(sent_at).date() == date.today()
except Exception:
return False