-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_manager.py
More file actions
119 lines (93 loc) · 3.27 KB
/
task_manager.py
File metadata and controls
119 lines (93 loc) · 3.27 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
# task_manager.py
from task import Task
import datetime
import pickle
from treelib import Tree
import random
# Module-level variables (instead of class variables)
tasks: list[Task] = []
task_tag_map: dict[int, Task] = {}
used_ids: set[int] = set()
def create_default_tasks():
"""Create default tasks for testing"""
task1 = Task("Do laundry", category="chores", due_date=None)
task2 = Task("essay project", category="esrm", due_date=datetime.date(2025, 11, 2))
task3 = Task("plan essay", category="esrm", due_date=datetime.date(2025, 10, 30))
task4 = Task("plan essay 2", category="esrm", due_date=datetime.date(2025, 10, 31))
task5 = Task("read books", category="esrm", due_date=datetime.date(2025, 10, 30))
task2.add_child(task3)
task2.add_child(task4)
task3.add_child(task5)
tasks.append(task1)
tasks.append(task2)
tasks.append(task3)
tasks.append(task4)
tasks.append(task5)
def get_task_map() -> dict[int, Task]:
return task_tag_map
def save_all_task_files():
"""Save tasks, tag map, and categories to files"""
with open("tasks.pkl", "wb") as f:
pickle.dump(tasks, f)
with open("tagmap.pkl", "wb") as f:
pickle.dump(task_tag_map, f)
with open("categories.pkl", "wb") as f:
categories = get_task_categories()
pickle.dump(categories, f)
def load_tasks_from_file() -> list[Task]:
"""Load tasks from pickle files or create defaults"""
global tasks, task_tag_map, used_ids
try:
with open("tasks.pkl", "rb") as f:
tasks = pickle.load(f)
tasks_copy = tasks
print("Loaded tasks from file")
with open("tagmap.pkl", "rb") as f:
task_tag_map = pickle.load(f)
used_ids = set(task_tag_map.keys())
print("Loaded tag map from file")
return tasks_copy
except FileNotFoundError:
print("No saved tasks found, creating default tasks")
create_default_tasks()
def build_tree():
"""Build and display task tree"""
tree = Tree()
tree.create_node("Root", "root")
for task in tasks:
tree.create_node(
task._task_name,
task._task_id,
parent=task._parent_id if task._parent_id is not None else "root",
)
tree.show()
def assign_tags():
"""Assign random tag IDs to TODO tasks"""
for task in tasks:
if task._status == Task.Status.TODO:
id = random.randint(0, 300)
if id not in used_ids:
task_tag_map[id] = task
task._tag_id = id
used_ids.add(id)
with open("tagmap.pkl", "wb") as f:
pickle.dump(task_tag_map, f)
def relinquish_tag(id: int):
"""Remove a tag from the tag map"""
task_tag_map.pop(id, None)
used_ids.discard(id)
def get_task_categories() -> list[str]:
"""Get all unique task categories"""
categories = set()
for task in tasks:
if task._task_category is not None:
categories.add(task._task_category)
return list(categories)
if __name__ == "__main__":
load_tasks_from_file()
if not task_tag_map: # Check if empty, not None
assign_tags()
for task in tasks:
print(f"tag id: {task._tag_id}")
print("\n".join(task.get_receipt()))
save_all_task_files()