-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_cli.py
More file actions
183 lines (135 loc) · 5.25 KB
/
Copy pathtask_cli.py
File metadata and controls
183 lines (135 loc) · 5.25 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import json
import datetime
from pathlib import Path
class TaskCLI:
def __init__(self):
self.last_id = 0
self.task_list = []
self.task_file_path = Path("tasks.json")
self.open_task_file()
def open_task_file(self):
if self.task_file_path.exists():
with open(self.task_file_path, 'r') as file_reader:
task_db = dict(json.load(file_reader))
self.task_list = list(task_db["task-list"])
self.last_id = int(task_db["last-id"])
def write_task_file(self):
with open(self.task_file_path, "w") as file_writer:
file_writer.write(self.__str__())
def add(self, description: str) -> str:
"""
Add a new task
Parameters:
description (int): The task's description
Returns:
str: The message of succesfull add and the task's ID
"""
id_task = self.last_id + 1
new_task = {
"id": id_task,
"description": description,
"status": "todo",
"createdAt": str(datetime.datetime.now()),
"updatedAt": None
}
self.last_id = id_task
self.task_list.append(new_task)
self.write_task_file()
print(f"Task added successfully (ID: {id_task})")
def update(self, id_task: int, description: str) -> None:
"""
Update the description of some task, the task is updated by ID
Parameters:
id_task (int): Task Id
description (str): The new description
"""
if not self.task_file_path.exists():
print("Just maybe ... add a task first")
return
index = self.binary_search_task_list(id_task)
if index == None:
print("Erro to update task: ID not found")
else:
self.task_list[index]["description"] = description
self.task_list[index]["updatedAt"] = str(datetime.datetime.now())
self.write_task_file()
print(f"Task (ID:{id_task}) updated")
return
def delete(self, id_task: int) -> None:
"""
Delete task by ID
Parameters:
id_task (int): Task Id use to delete task from task list
"""
if not self.task_file_path.exists():
print("Just maybe ... add a task first")
return
index = self.binary_search_task_list(id_task)
if index == None:
print("Erro to update task: ID not found")
else:
self.task_list.pop(index)
self.write_task_file()
print(f"Task (ID: {id_task}) successfully deleted")
return
def update_status(self, id_task: int, status: str) -> None:
if not self.task_file_path.exists():
print("Just maybe ... add a task first")
return
if status == 'done' or status == 'in-progress':
index = self.binary_search_task_list(id_task)
if index == None:
print("Erro to change status from task: ID not found")
else:
self.task_list[index]["status"] = status
self.task_list[index]["updatedAt"] = str(datetime.datetime.now())
self.write_task_file()
print(f"Task (ID:{id_task}) changed status to: {status}")
return
else:
print(f"Erro to change status: {status} status not exist. Try 'done' or 'in-progress.")
return
def list_visualization(self, status=None) -> None:
"""
Show the tasks list
"""
if not self.task_file_path.exists():
print("The task list not exists. Use 'add' command to track your first task!")
return
count_success = False
for task in self.task_list:
id_d = f"ID: {task['id']}\n"
desc_d = f"DESCRIPTION: {task['description']}\n"
stat_d = f"STATUS: {task['status']}\n"
cre_d = f"CREATED AT: {task['createdAt']}\n"
upd_d = f"UPDATED AT: {task['updatedAt']}\n"
div_d = "-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n"
task_display = id_d + desc_d + stat_d + cre_d + upd_d + div_d
if status == None:
print(task_display)
count_success = True
elif task['status'] == status:
print(task_display)
count_success = True
if not count_success:
print(f"Error to display task by status: {status} not exists")
return
def binary_search_task_list(self, id_search):
start = 0
end = len(self.task_list) - 1
while start <= end:
middle = int((start + end)/2)
guess = self.task_list[middle]
if guess['id'] == id_search:
return middle
if guess['id'] > id_search:
end = middle - 1
else:
start = middle + 1
return None
def __str__(self):
t = {
"task-list": self.task_list,
"last-id": self.last_id
}
return json.dumps(t, indent=4)