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
|
import json
import sys
import os
from json import JSONDecodeError
def add_todo(desc: str):
"""Add a new todo item
Args:
desc: The task description
"""
todos_file = _get_todos_file()
try:
with open(todos_file, "r") as f:
data = json.load(f)
except (FileNotFoundError, JSONDecodeError):
data = []
num = max([item["id"] for item in data] + [0]) + 1
data.append({"id": num, "desc": desc})
with open(todos_file, "w") as f:
json.dump(data, f)
print(f"Successfully added todo id={num}")
def del_todo(id: int):
"""Delete an existing todo item
Args:
id: The task id
"""
todos_file = _get_todos_file()
try:
with open(todos_file, "r") as f:
data = json.load(f)
except (FileNotFoundError, JSONDecodeError):
print("Empty todo list")
return
data = [item for item in data if item["id"] != id]
with open(todos_file, "w") as f:
json.dump(data, f)
print(f"Successfully deleted todo id={id}")
def list_todos():
"""Display the current todo list in json format."""
todos_file = _get_todos_file()
try:
with open(todos_file, "r") as f:
print(f.read())
except FileNotFoundError:
print("[]")
def clear_todos():
"""Delete the entire todo list."""
os.remove(_get_todos_file())
def _get_todos_file() -> str:
cache_dir=os.environ.get("LLM_BOT_CACHE_DIR", "/tmp")
if not os.path.exists(cache_dir):
os.makedirs(cache_dir, exist_ok=True)
return os.path.join(cache_dir, "todos.json")
|