Repository navigation
Expand file tree
/
Copy pathtask.py
More file actions
144 lines (106 loc) · 3.62 KB
/
Copy pathtask.py
File metadata and controls
144 lines (106 loc) · 3.62 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
import sys
import json
import os
from datetime import datetime
FILE_PATH = 'tasks.json'
def load_tasks():
if not os.path.exists(FILE_PATH):
return []
try:
with open(FILE_PATH, 'r', encoding='utf-8') as f:
return json.load(f)
except (json.JSONDecodeError, OSError):
return []
def save_tasks(tasks):
with open(FILE_PATH, 'w', encoding='utf-8') as f:
json.dump(tasks, f, indent=4, ensure_ascii=False)
def get_current_time():
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
def add_task(description):
tasks = load_tasks()
new_id = max([task['id'] for task in tasks], default=0) + 1
now = get_current_time()
new_task = {
'id': new_id,
'description': description,
'status': 'todo',
'createdAt': now,
'updatedAt': now
}
tasks.append(new_task)
save_tasks(tasks)
print(f'Task added successfully (ID: {new_id})')
def update_task(task_id, new_description):
tasks = load_tasks()
for task in tasks:
if task['id'] == task_id:
task['description'] = new_description
task['updatedAt'] = get_current_time()
save_tasks(tasks)
print(f'Task {task_id} updated successfully!')
return
print(f'Error: Task with ID {task_id} not found.')
def delete_task(task_id):
tasks = load_tasks()
updated_tasks = [t for t in tasks if t['id'] != task_id]
if len(tasks) == len(updated_tasks):
print(f'Error: Task with ID {task_id} not found.')
return
deleted_task = next(t for t in tasks if t['id'] == task_id)
save_tasks(updated_tasks)
print(f'Task "{deleted_task["description"]}" deleted!')
def mark_status(task_id, status):
tasks = load_tasks()
for task in tasks:
if task['id'] == task_id:
task['status'] = status
task['updatedAt'] = get_current_time()
save_tasks(tasks)
print(f'Task {task_id} marked as {status}.')
return
print(f'Error: Task with ID {task_id} not found.')
def list_tasks(status_filter=None):
tasks = load_tasks()
if status_filter:
tasks = [t for t in tasks if t['status'] == status_filter]
if not tasks:
print('No tasks found.')
return
print('\nTasks:')
for task in tasks:
print(f"[{task['id']}] {task['description']} | Status: {task['status']} | Created: {task['createdAt']}")
def main():
args = sys.argv[1:]
if not args:
print("Usage: python task.py <command> [arguments]")
return
command = args[0].lower()
if command == 'add' and len(args) > 1:
add_task(args[1])
elif command == 'update' and len(args) > 2:
try:
update_task(int(args[1]), args[2])
except ValueError:
print("Error: Task ID must be an integer.")
elif command == 'delete' and len(args) > 1:
try:
delete_task(int(args[1]))
except ValueError:
print("Error: Task ID must be an integer.")
elif command == 'mark-in-progress' and len(args) > 1:
try:
mark_status(int(args[1]), 'in-progress')
except ValueError:
print("Error: Task ID must be an integer.")
elif command == 'mark-done' and len(args) > 1:
try:
mark_status(int(args[1]), 'done')
except ValueError:
print("Error: Task ID must be an integer.")
elif command == 'list':
status = args[1] if len(args) > 1 else None
list_tasks(status)
else:
print("Invalid command or missing arguments.")
if __name__ == '__main__':
main()