-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaskmanager.py
156 lines (122 loc) · 4.96 KB
/
taskmanager.py
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import json
import os
importance_weight = 1.0
urgence_weight = 1.0
class Task:
def __init__(self, tid, name, importance, urgence):
self.tid = tid
self.name = name
self.importance = importance
self.urgence = urgence
def to_json(self):
return {"id": self.tid, "name": self.name, "importance": self.importance, "urgence": self.urgence}
def __lt__(self, other):
self_value = ((importance_weight * self.importance) + (urgence_weight * self.urgence))
other_value = ((importance_weight * other.importance) + (urgence_weight * other.urgence))
return self_value < other_value
def __gt__(self, other):
self_value = ((importance_weight * self.importance) + (urgence_weight * self.urgence))
other_value = ((importance_weight * other.importance) + (urgence_weight * other.urgence))
return self_value > other_value
def __le__(self, other):
self_value = ((importance_weight * self.importance) + (urgence_weight * self.urgence))
other_value = ((importance_weight * other.importance) + (urgence_weight * other.urgence))
return self_value <= other_value
def __ge__(self, other):
self_value = ((importance_weight * self.importance) + (urgence_weight * self.urgence))
other_value = ((importance_weight * other.importance) + (urgence_weight * other.urgence))
return self_value >= other_value
def __eq__(self, other):
self_value = ((importance_weight * self.importance) + (urgence_weight * self.urgence))
other_value = ((importance_weight * other.importance) + (urgence_weight * other.urgence))
return self_value == other_value
def __ne__(self, other):
self_value = ((importance_weight * self.importance) + (urgence_weight * self.urgence))
other_value = ((importance_weight * other.importance) + (urgence_weight * other.urgence))
return self_value != other_value
class TaskList:
def __init__(self):
self.__task_id = 1
self.__cache_dir = ".cache/cache.json"
self.task_list = list()
self.__init_from_cache()
@staticmethod
def __in_cache(data, task):
for t1 in data["tasks"]:
if t1["id"] == task.tid:
return True
return False
def __index_of_task_list(self, tid):
for i in range(len(self.task_list)):
if tid == self.task_list[i].tid:
return i
return -1
@staticmethod
def __index_of_task_cache(data, tid):
tasks = data["tasks"]
for i in range(len(tasks)):
if tasks[i]["id"] == tid:
return i
return -1
def __init_from_cache(self):
if not os.path.exists(".cache/"):
os.makedirs(".cache/")
try:
cache_file = open(self.__cache_dir, "r")
except FileNotFoundError:
with open(self.__cache_dir, "x") as f:
f.write('{"tasks":[],"task_id":1}')
return
data = json.loads(cache_file.read())
self.__task_id = data["task_id"]
for task in data["tasks"]:
self.__add_task_wid(task["id"], task["name"], task["importance"], task["urgence"])
def __add_to_cache(self, task):
cache_file = open(self.__cache_dir, 'r')
data = json.load(cache_file)
cache_file.close()
if self.__in_cache(data, task):
return
data["tasks"].append(task.to_json())
data["task_id"] = self.__task_id
cache_file = open(self.__cache_dir, 'w')
json.dump(data, cache_file)
cache_file.close()
def add_task(self, name, importance, urgence):
task = Task(self.__task_id, name, importance, urgence)
self.task_list.append(task)
self.task_list.sort(reverse=True)
self.__task_id += 1
self.__add_to_cache(task)
def __add_task_wid(self, tid, name, importance, urgence):
task = Task(tid, name, importance, urgence)
self.task_list.append(task)
self.task_list.sort(reverse=True)
def __remove_from_cache(self, tid):
cache_file = open(self.__cache_dir, "r+")
data = json.load(cache_file)
cache_file.close()
idx = TaskList.__index_of_task_cache(data, tid)
if idx == -1:
return
data["tasks"].pop(idx)
cache_file = open(self.__cache_dir, 'w')
json.dump(data, cache_file)
cache_file.close()
def remove_task(self, tid):
idx = self.__index_of_task_list(tid)
if idx == -1:
return
self.task_list.pop(idx)
self.__remove_from_cache(tid)
def __clear_cache(self):
cache_file = open(self.__cache_dir, "r+")
data = json.load(cache_file)
cache_file.close()
data["tasks"].clear()
cache_file = open(self.__cache_dir, 'w')
json.dump(data, cache_file)
cache_file.close()
def clear_tasks(self):
self.task_list.clear()
self.__clear_cache()