-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskTrackerCLI.py
More file actions
156 lines (129 loc) · 4.64 KB
/
TaskTrackerCLI.py
File metadata and controls
156 lines (129 loc) · 4.64 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
import json
import os
from datetime import datetime
APP_DIR = os.path.dirname(os.path.abspath(__file__))
JSON_DIR = os.path.join(APP_DIR, 'tasks.json')
def read_json():
if os.path.exists(JSON_DIR):
with open(JSON_DIR, 'r') as j:
return json.load(j)
else:
print("JSON file does not exist")
create_json_file()
with open(JSON_DIR, 'r') as j:
return json.load(j)
def write_json(data):
with open(JSON_DIR, "w") as f:
json.dump(data, f, indent=4)
def create_json_file():
with open(JSON_DIR, "w") as j:
json.dump({'tasks': []}, j, indent=4)
def create_task(task):
data = read_json()
tasks_id = [task['id'] for task in data['tasks']]
new_task = {'id': len(tasks_id)+1, \
'description': task, \
'status': 'new', \
'createdAt': datetime.now().strftime('%d-%m-%Y, %H:%M:%S.%f'), \
'updatedAt': datetime.now().strftime('%d-%m-%Y, %H:%M:%S.%f')
}
data['tasks'].append(new_task)
write_json(data)
print(f'Task {task} has been created')
def update_task(id, task):
data = read_json()
json_task = next((task for task in data['tasks'] if task['id'] == id), None)
if json_task:
json_task['description'] = task
json_task['updatedAt'] = datetime.now().strftime('%d-%m-%Y, %H:%M:%S.%f')
write_json(data)
print('Task updated:', task)
else:
print(f'ID {id} doesn´t exist')
def delete_task(id):
data = read_json()
if any(task['id'] == id for task in data['tasks']):
data['tasks'].pop(id - 1)
print(f'Task {id} deleted')
else:
print(f'Task {id} doesn´t exist')
write_json(data)
def format_response(task):
for key, value in task.items():
print(key, ':', value)
print()
def list_tasks(status):
data = read_json()
if status == 'all':
for task in data['tasks']:
format_response(task)
elif status == 'new':
for task in data['tasks']:
if task['status'] == 'new':
format_response(task)
elif status == 'in-progress':
for task in data['tasks']:
if task['status'] == 'in-progress':
format_response(task)
elif status == 'done':
for task in data['tasks']:
if task['status'] == 'done':
format_response(task)
else:
print(f'status {status} does not exist!')
def mark_status(status, id):
data = read_json()
json_task = next((task for task in data['tasks'] if task['id'] == id), None)
if json_task:
json_task['status'] = status
json_task['updatedAt'] = datetime.now().strftime('%d-%m-%Y, %H:%M:%S.%f')
write_json(data)
print(f'Task {id} status updated: {status}')
else:
print(f'ID {id} doesn´t exist')
def help_menu():
print('''
=========================================================================
Task CLI Help
=========================================================================
Commands:
add <task> - Add a new task
update <id> <desc> - Update the task description
delete <id> - Delete a task by ID
list <status> - List tasks by status (all, in-progress, done)
clear - Delete all tasks
mark-in-progress <id> - Mark a task as "in-progress"
mark-done <id> - Mark a task as "done"
exit - Close the program
==========================================================================
''')
def main():
while True:
command = input('\ntask-cli ').strip().lower()
command = command.split()
#TODO: Validar Inputs
match command:
case ['help']:
help_menu()
case ['add', *task]:
create_task(' '.join(task))
case ['update', id, *task]:
update_task(int(id), ' '.join(task))
case ['delete', id]:
delete_task(int(id))
case ['list', status]:
list_tasks(status)
case ['clear']:
create_json_file()
print('All tasks have been eliminated')
case ['mark-in-progress', id]:
mark_status('in-progress', int(id))
case ['mark-done', id]:
mark_status('done', int(id))
case ['exit']:
print('Closing program...')
break
case _:
print("Unkown command")
if __name__ == '__main__':
main()