-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_task_tracker.py
More file actions
329 lines (283 loc) · 13 KB
/
cli_task_tracker.py
File metadata and controls
329 lines (283 loc) · 13 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# add description and rename it
import json
import sys
from typing import Dict
import datetime
def main():
json_file = "tasks.json"
args, do = extract_args()
# perform the task
execute_command(args, do, json_file)
def extract_args(): # Needs refactoring
raw_args = sys.argv[1:]
args: Dict = {"command": None, "args": []}
if raw_args[0] == "-h":
if len(raw_args) == 1:
print("""CLI task tracker""") # Add the help message (later)
else:
report_error(f"Arbitrary arguments are provided with help option.", "Argument")
elif raw_args[0] == "add":
args["command"] = "add"
if "-h" not in raw_args[1:] and "--help" not in raw_args[1:]:
if len(raw_args) == 2:
task = raw_args[1]
args["args"] = [task]
elif len(raw_args) == 4:
task_id_or_task = raw_args[1]
args["args"] = [int(task_id_or_task) if task_id_or_task.isdigit() else task_id_or_task]
if "-d" == raw_args[2]:
description = raw_args[3]
args["args"].append(description)
else:
report_error(f"Wrong number of arguments is provided.", "Argument")
return args, False
else:
report_error(f"Wrong number of arguments is provided.", "Argument")
return args, False
else:
if len(raw_args) != 2:
report_error(f"Arbitrary arguments are provided with help option.", "Argument")
return args, False
else:
print("Add a new task or a description to an existing task\n" + "Usage: add task\n"
+ "Note if there is a space in the input task wrap it in a quotes.\n"
+ "To add a description use add task_id -d 'the description'.")
return args, False
elif raw_args[0] == "delete":
args["command"] = "delete"
if "-h" not in raw_args[1:] and "--help" not in raw_args[1:]:
if len(raw_args) != 2:
report_error(f"Wrong number of arguments is provided {len(raw_args) - 1}."
+ " Provide only 1 argument.", "Argument")
return args, False
else:
task_id_to_delete = raw_args[1]
try:
task_id_to_delete = int(task_id_to_delete)
except ValueError:
report_error("ID must be an integer", "Type")
return args, False
else:
args["args"] = [task_id_to_delete]
else:
if len(raw_args) != 2:
report_error(f"Arbitrary arguments are provided with help option.", "Argument")
return args, False
else:
print("Delete an existing task by its ID\n" + "Usage: delete ID")
return args, False
elif raw_args[0] == "update":
args["command"] = "update"
if "-h" not in raw_args[1:] and "--help" not in raw_args[1:]:
if len(raw_args) != 3:
report_error(f"Wrong number of arguments is provided {len(raw_args) - 1}."
+ " Provide only 2 arguments.", "Argument")
return args, False
else:
args["args"] = []
task_id = raw_args[1]
try:
task_id = int(task_id)
except ValueError:
report_error("ID must be an integer", "Type")
return args, False
else:
args["args"].append(task_id)
new_task = raw_args[2]
args["args"].append(new_task)
else:
if len(raw_args) != 2:
report_error(f"Arbitrary arguments are provided with help option.", "Argument")
return args, False
else:
print("Update an existing task by its id. The new task will replace the old one.\n"
+ "Usage: update ID new-task")
return args, False
elif raw_args[0] == "list":
args["command"] = "list"
if "-h" not in raw_args[1:] and "--help" not in raw_args[1:]:
if len(raw_args) == 1:
args["args"] = ["all"] # default "all"
elif len(raw_args) == 2:
choices = ["all", "in-progress", "done", "todo"] # "to-do" == "undone"
if raw_args[1] not in choices:
report_error("Invalid status choose from: [all, in-progress, done, todo]",
"InvalidChoice")
return args, False
else:
args["args"] = [raw_args[1]] # Optional
else:
report_error(f"Wrong number of arguments is provided.", "Argument")
return args, False
else:
if len(raw_args) != 2:
report_error(f"Arbitrary arguments are provided with help option.", "Argument")
return args, False
else:
print("List the tasks by the given status. If no status provided all tasks will be listed\n"
+ "Usage: list [status]\n" + "Available status choices: in-progress, done, todo, all")
return args, False
elif raw_args[0] == "mark":
args["command"] = "mark"
if "-h" not in raw_args[1:] and "--help" not in raw_args[1:]:
if len(raw_args) == 2:
# ID argument
task_id_to_mark = raw_args[1]
try:
task_id_to_mark = int(task_id_to_mark)
except ValueError:
report_error("ID must be an integer", "Type")
return args, False
else:
args["args"] = [task_id_to_mark]
# Status argument
args["args"].append("done") # Optional, default "done"
elif len(raw_args) == 3:
args["command"] = "mark"
# ID argument
task_id_to_mark = raw_args[1]
try:
task_id_to_mark = int(task_id_to_mark)
except ValueError:
report_error("ID must be an integer", "Type")
return args, False
else:
args["args"] = [task_id_to_mark]
# Status argument
choices = ["in-progress", "done", "todo"]
# the last one to return the task to the basic state if it was marked by mistake
if raw_args[2] not in choices:
report_error("Invalid status choose from: [in-progress, done, todo]",
"InvalidChoice")
return args, False
else:
args["args"].append(raw_args[2]) # Optional
else:
report_error(f"Wrong number of arguments is provided.", "Argument")
return args, False
else:
if len(raw_args) != 2:
report_error(f"Arbitrary arguments are provided with help option.", "Argument")
return args, False
else:
print("Mark the tasks as in-progress, done, or todo." +
" If no choice provided the task will be marked as done\n"
+ "Usage: mark [as]\n" + "Available choices: in-progress, done, or todo")
return args, False
else:
report_error("No such argument.\n" + "Choose from: [add, delete, update, list, or mark]", "Argument")
return args, True
def execute_command(args, do, json_file):
data = get_data(json_file)
task_id = get_id(data)
if do:
if args["command"] == "add":
if len(args["args"]) == 1:
add_task(args["args"][0], task_id, data, json_file)
else:
if isinstance(args["args"][0], int):
add_description(args["args"][1], args["args"][0], data, json_file)
else: # It will be a str
add_task(args["args"][0], task_id, data, json_file)
add_description(args["args"][1], task_id, data, json_file)
elif args["command"] == "delete":
delete_task(args["args"][0], data, json_file)
elif args["command"] == "update":
update_task(args["args"][0], args["args"][1], data, json_file)
elif args["command"] == "list":
list_tasks(data, args["args"][0])
elif args["command"] == "mark":
mark_task(data, args["args"][0], args["args"][1], json_file)
else:
pass
# No need to do anything, because extract_args reports error and ignore the invalid command
def get_data(json_file):
try:
with open(json_file, "r") as file:
data = json.load(file)
return data
except (FileNotFoundError, json.JSONDecodeError):
write_to_json(json_file, {})
return {}
def get_id(data):
existing_ids = [int(key[1:]) for key in data.keys()] # Extract numerical IDs from keys
for num in range(1, len(data) + 1):
if num not in existing_ids: # Check for the first missing number
return num
return len(data) + 1 # If no missing values
def write_to_json(file_name, data):
with open(file_name, "w") as file:
json.dump(data, file, indent=4)
def add_task(item, task_id, data, json_file):
data[f"t{task_id}"] = {"id": task_id, "task": item, "status": "todo",
"description": "",
"CreatedAt": datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S"),
"UpdatedAt": None}
write_to_json(json_file, data)
print(f"Successfully added {item} (ID: {task_id})")
def add_description(description, task_id, data, json_file):
if data.get(f"t{task_id}", None) is not None:
data[f"t{task_id}"]["UpdatedAt"] = datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S")
data[f"t{task_id}"]["description"] = description
write_to_json(json_file, data)
print(f"Successfully added description for {data[f"t{task_id}"]["task"]} (ID: {task_id})")
else:
report_error(f"No task is associated with the ID {task_id}.", "ID")
def delete_task(task_id, data, json_file):
try:
item = data.pop(f"t{task_id}")
except KeyError:
report_error(f"No task is associated with the ID {task_id}.", "ID")
else:
write_to_json(json_file, data)
print(f"Successfully deleted {item['task']}")
def update_task(task_id, new_task, data, json_file):
old = data[f"t{task_id}"]["task"]
try:
data[f"t{task_id}"]["task"] = new_task
data[f"t{task_id}"]["UpdatedAt"] = datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S")
except KeyError:
report_error(f"No task is associated with the ID {task_id}.", "ID")
else:
write_to_json(json_file, data)
print(f"Successfully updated '{old}' to '{data[f't{task_id}']['task']}'!")
def sort_dict_data(task_dict):
return int(task_dict[0][1:])
def list_tasks(data, status):
if data:
# noinspection PyTypeChecker
sorted_data = sorted(data.items(), key=sort_dict_data)
count = 0
if status == "all":
count = len(sorted_data)
print(f"You have {count} task{"s" if count else ""}.")
print("ID. Task - Status")
for _, task_dict in sorted_data:
print(f"{task_dict["id"]}. {task_dict["task"]} - {task_dict["status"]}")
else:
tasks = []
for _, task_dict in sorted_data:
if task_dict["status"] == status:
tasks.append(task_dict)
count += 1
if tasks:
print(f"You have {count} task{"s" if count else ""} marked as {status}.")
print("ID Task")
for task in tasks:
print(f"{task["id"]}. {task["task"]}")
else:
print(f"No tasks marked as {status} to be listed.")
return count
else:
print("No tasks to be listed.")
def mark_task(data, task_id, new_status, json_file):
data[f"t{task_id}"]["status"] = new_status
write_to_json(json_file, data)
print(f"Successfully marked {data[f"t{task_id}"]["task"]} as {new_status}!")
def report_error(message, error_type=None):
sys.stderr.write(f"{error_type if error_type else ""}Error: {message}\n")
# to make it working properly with the shell change it to
# from rich import print; print(f"[red]{error_type if error_type else ""}Error: {message}\n[/red]")
if __name__ == "__main__":
main()
# complete readme