-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_tracker.py
More file actions
267 lines (206 loc) · 8.46 KB
/
task_tracker.py
File metadata and controls
267 lines (206 loc) · 8.46 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
import argparse
from datetime import datetime
import json
import pandas as pd
file_path = "data.json"
# Get current date and time
date_time_now = datetime.now()
date_time_str = date_time_now.strftime("%d/%m/%Y %H:%M")
# Function to add a new task
def add(description):
# Load the JSON data from the file
with open(file_path, "r", encoding="utf-8") as file:
data = json.load(file) # Ensure this is a list
# If data is not a list, initialize it as a list
if not isinstance(data, list):
data = []
# Determine the new task ID
last_value = data[-1]["id"] if data else 0
new_id = last_value + 1
# Create a new task dictionary
new_task = {
"id": new_id,
"description": description,
"status": "to do",
"creatAt": date_time_str,
"updateAt": ""
}
# Append the new task to the list
data.append(new_task)
# Save the updated list of tasks back to the file
with open(file_path, "w", encoding="utf-8") as file:
json.dump(data, file, indent=4, ensure_ascii=False)
# Return the result
print(f"Task added successfully (ID: {new_id})")
# Function to delete the task
def delete(id_delete):
# Load the JSON data from the file
with open(file_path, "r") as file:
data = json.load(file) # Ensure this is a list
# If data is not a list, initialize it as a list
if not isinstance(data, list):
data = []
# Find the index of task list
for index, task in enumerate(data):
id_delete = int(id_delete)
if task.get('id') == id_delete:
index_delete = index
break
# Remove the task and return the result
if index_delete is not None:
removed_task = data.pop(index_delete)
print(f"Task of ID {removed_task['id']} removed successfully.")
else:
print(f"Task of ID {id_delete} not found.")
# Save the updated data back to the file
with open(file_path, "w", encoding="utf-8") as file:
json.dump(data, file, indent=4, ensure_ascii=False)
# Function to update the task
def update(id_update, description):
# Load the JSON data from the file
with open(file_path, "r", encoding="utf-8") as file:
data = json.load(file) # Ensure this is a list
# If data is not a list, initialize it as a list
if not isinstance(data, list):
data = []
# Identify the id and create the update
update = False
for index, task in enumerate(data):
id_update = int(id_update)
if task.get("id") == id_update:
task["description"] = description;
task["updateAt"] = date_time_str
update = True
break
#Return the result
if update:
print(f"Task of ID {id_update} updated successfully.")
else:
print(f"Task of ID {id_update} not found.")
# Save the updated list of tasks from the file
with open(file_path, "w", encoding="utf-8") as file:
json.dump(data, file, indent=4, ensure_ascii=False)
# Function to update the task status to in-progress
def mark_in_progess(id_update_status):
# Load the JSON data from the file
with open(file_path, "r",encoding="utf-8") as file:
data = json.load(file)
# If data is not a list, initialize it as a list
if not isinstance(data, list):
data = []
# Update the status of the task
update = False
for index, task in enumerate(data):
id_update_status = int(id_update_status)
if task.get("id") == id_update_status:
task["status"] = "in-progress";
task["updateAt"] = date_time_str
update = True
break
# Return the result
if update:
print(f"The task with ID {id_update_status} was updated successfully")
else:
print(f"The task with ID {id_update_status} is not found")
# Save the updatd list of tasks from the file
with open(file_path, "w", encoding="utf-8") as file:
json.dump(data, file, indent=4, ensure_ascii=False)
# Funcion mark-done
def mark_done(id_update_task):
with open(file_path, "r", encoding="utf-8") as file:
data = json.load(file)
if not isinstance(data, list):
data = []
update = False
for index, task in enumerate(data):
id_update_task = int(id_update_task)
if task.get("id") == id_update_task:
task["status"] = "done";
task["updateAt"] = date_time_str
update = True
break
if update:
print("The task with ID {}, is done".format(id_update_task))
else:
print("The task with ID {}, is not found".format(id_update_task))
with open(file_path, "w", encoding="utf-8") as file:
json.dump(data, file, indent=4, ensure_ascii=False)
def list_tasks():
with open(file_path, "r", encoding="utf-8") as file:
data = json.load(file)
df = pd.DataFrame(data)
print(df)
def list_done():
with open(file_path, "r", encoding="utf-8") as file:
data = json.load(file)
result = [d for d in data if d['status'] == "done"]
df = pd.DataFrame(result)
print(df)
def list_todo():
with open(file_path, "r", encoding="utf-8") as file:
data = json.load(file)
result = [d for d in data if d['status'] == "to do"]
df = pd.DataFrame(result)
print(df)
def list_in_progress():
with open(file_path, "r", encoding="utf-8") as file:
data = json.load(file)
result = [d for d in data if d['status'] == "in-progress"]
df = pd.DataFrame(result)
print(df)
# Main function to parse CLI arguments
def main():
parser = argparse.ArgumentParser(
prog="task-manager",
description="Task Manager CLI",
)
subparsers = parser.add_subparsers(dest="command", help="Available commands")
# Subparse for "add" command
parser_add = subparsers.add_parser("add", help="Add a new task")
parser_add.add_argument("description", type=str, help="The description of the new task")
# Subparse for "delete" command
parser_delete = subparsers.add_parser("delete", help="Delete a task")
parser_delete.add_argument("id_delete",type=str, help="task id to be deleted")
# Subparse for "update" command
parser_update = subparsers.add_parser("update", help="Update the task")
parser_update.add_argument("id_update", type=str, help="task id to be updated")
parser_update.add_argument("description", type=str, help="task description to be updated")
# Subparser for "mark-in-progress"
parser_mark_in_progress = subparsers.add_parser("mark_in_progress", help="Updated the task status")
parser_mark_in_progress.add_argument("id_update_status", type=str, help="task id to be updated")
# Subparser for "mark-done" command
parser_update_done = subparsers.add_parser("mark_done", help="Update the status with is done")
parser_update_done.add_argument("id_update_task", type=str, help="Task id for update with is done")
# Subparser for "list" command
parser_list_tasks = subparsers.add_parser("list", help="List all tasks")
# Subparser for "list_done" command
parser_list_done = subparsers.add_parser("list.done", help="All tasks with done status")
# Subparser for "list_done" command
parser_list_todo = subparsers.add_parser("list.todo", help="All tasks with to do status")
# Subparser for "list_done" command
parser_list_in_progress = subparsers.add_parser("list.inprogress", help="All tasks with in progress status")
# Parse the arguments
args = parser.parse_args()
# Execute the appropriate function based on the subcommand
if args.command == "add":
add(args.description)
elif args.command == "delete":
delete(args.id_delete)
elif args.command == "update":
update(args.id_update,args.description)
elif args.command == "mark_in_progress":
mark_in_progess(args.id_update_status)
elif args.command == "mark_done":
mark_done(args.id_update_task)
elif args.command == "list":
list(args.status)
elif args.command == "list.done":
list_done()
elif args.command == "list.todo":
list_todo()
elif args.command == "list.inprogress":
list_in_progress()
else:
parser.print_help()
if __name__ == "__main__":
main()