-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplan_manager.py
More file actions
63 lines (50 loc) · 1.66 KB
/
plan_manager.py
File metadata and controls
63 lines (50 loc) · 1.66 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
import os
import json
import time
PLANS_DIR = ".plans"
def ensure_plans_dir():
os.makedirs(PLANS_DIR, exist_ok=True)
def save_plan(name: str, task: str, plan_text: str) -> str:
ensure_plans_dir()
plan = {
"name": name,
"task": task,
"plan": plan_text,
"created_at": time.strftime("%Y-%m-%d %H:%M:%S"),
"status": "pending",
}
path = os.path.join(PLANS_DIR, f"{name}.json")
with open(path, "w", encoding="utf-8") as f:
json.dump(plan, f, indent=2)
return path
def load_plan(name: str) -> dict | None:
path = os.path.join(PLANS_DIR, f"{name}.json")
if not os.path.isfile(path):
return None
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
def list_plans() -> list[dict]:
ensure_plans_dir()
plans = []
for fname in sorted(os.listdir(PLANS_DIR)):
if fname.endswith(".json"):
path = os.path.join(PLANS_DIR, fname)
with open(path, "r", encoding="utf-8") as f:
plan = json.load(f)
plan["filename"] = fname
plans.append(plan)
return plans
def get_latest_plan() -> dict | None:
plans = list_plans()
pending = [p for p in plans if p.get("status") == "pending"]
if pending:
return pending[-1]
return plans[-1] if plans else None
def mark_plan_done(name: str) -> None:
path = os.path.join(PLANS_DIR, f"{name}.json")
if os.path.isfile(path):
with open(path, "r", encoding="utf-8") as f:
plan = json.load(f)
plan["status"] = "done"
with open(path, "w", encoding="utf-8") as f:
json.dump(plan, f, indent=2)