-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmytask.py
More file actions
226 lines (177 loc) · 6.78 KB
/
mytask.py
File metadata and controls
226 lines (177 loc) · 6.78 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
from argparse import ArgumentParser
import os
import json
root_path = os.path.dirname(os.path.abspath(__file__)) + os.sep
tasklists_path = root_path + 'tasklists' + os.sep
license_path = root_path + 'LICENSE'
readme_path = root_path + 'README.md'
last_path = root_path + 'last'
def load_list(name):
if not os.path.exists(tasklists_path + name + '.json'):
print(f"Task list '{name}' does not exist.")
exit(1)
with open(tasklists_path + name + '.json') as f:
return json.loads(f.read())
def save_list(name, tasks):
with open(tasklists_path + name + '.json', 'w') as f:
f.write(json.dumps(tasks, indent=4))
def get_working_list():
with open(last_path) as f:
current_task = f.read().strip()
if current_task == '':
print("No task list set. Use --newlist to create a new task list.")
exit(1)
if not os.path.exists(tasklists_path + current_task + '.json'):
print(f"Task list '{current_task}' does not exist.")
exit(2)
return current_task
def create_new_list(name):
if os.path.exists(tasklists_path + name + '.json'):
print(f"Task list '{name}' already exists.")
return
save_list(name, {})
set_working_list(name)
def set_working_list(name):
with open(last_path, 'w') as f:
f.write(name)
def show_all_lists():
lists = [f[:-5] for f in os.listdir(tasklists_path) if f.endswith('.json')]
if not lists:
print("No task lists found.")
return
print("Available task lists:")
for lst in lists:
print(f"\t{lst}")
def show_current_list():
current_list = get_working_list()
tasks = load_list(current_list)
if not tasks:
print(f"Task list '{current_list}' is empty.")
return
print(f"Current task list '{current_list}':")
for task_id, task in tasks.items():
status = 'Complete' if task['done'] else 'Incomplete'
print(f"\t{task_id}: {task['task']} ({status})")
def delete_list(name):
if not os.path.exists(tasklists_path + name + '.json'):
print(f"Task list '{name}' does not exist.")
return
os.remove(tasklists_path + name + '.json')
if os.path.exists(last_path) and open(last_path).read().strip() == name:
with open(last_path, 'w') as f:
f.write('')
print(f"Task list '{name}' deleted.")
def delete_task(task_id):
tasks = load_list(get_working_list())
if str(task_id) not in tasks:
print(f"Task ID '{task_id}' does not exist.")
return
del tasks[str(task_id)]
save_list(get_working_list(), tasks)
def rename_task(task_id):
tasks = load_list(get_working_list())
if str(task_id) not in tasks:
print(f"Task ID '{task_id}' does not exist.")
return
old_task_name = tasks[str(task_id)]['task']
tasks[str(task_id)]['task'] = input('Enter new task name: ')
save_list(get_working_list(), tasks)
print(f"Task ID '{old_task_name}' renamed to '{task_id}'.")
def rename_list(name):
if not os.path.exists(tasklists_path + name + '.json'):
print(f"Task list '{name}' does not exist.")
return
new_name = input("Enter new name for the task list: ")
if os.path.exists(tasklists_path + new_name + '.json'):
print(f"Task list '{new_name}' already exists.")
return
os.rename(tasklists_path + name + '.json', tasklists_path + new_name + '.json')
if os.path.exists(last_path) and open(last_path).read().strip() == name:
with open(last_path, 'w') as f:
f.write(new_name)
print(f"Task list '{name}' renamed to '{new_name}'.")
def mark_task_done(task_id):
tasks = load_list(get_working_list())
if str(task_id) not in tasks:
print(f"Task ID '{task_id}' does not exist.")
return
tasks[str(task_id)]['done'] = True
with open(tasklists_path + get_working_list() + '.json', 'w') as f:
f.write(json.dumps(tasks, indent=4))
def undo_task(task_id):
tasks = load_list(get_working_list())
if str(task_id) not in tasks:
print(f"Task ID '{task_id}' does not exist.")
return
tasks[str(task_id)]['done'] = False
save_list(get_working_list(), tasks)
def add_task(task):
tasks = load_list(get_working_list())
task_id = str(len(tasks) + 1)
tasks[task_id] = {'task': task, 'done': False}
save_list(get_working_list(), tasks)
print(f"Task '{task}' added with ID '{task_id}'.")
def main():
parser = ArgumentParser(description="A simple CLI task manager.")
parser.add_argument('--license', action='store_true', help='Show license information')
parser.add_argument('--version', action='store_true', help='Show version information')
parser.add_argument('--doc', action='store_true', help='Show documentation')
parser.add_argument('--newlist', type=str, help='Create a new task list')
parser.add_argument('--setlist', type=str, help='Set the working task list')
parser.add_argument('--lists', action='store_true', help='Show all task lists')
parser.add_argument('--show', action='store_true', help='Show the current task list')
parser.add_argument('--deletelist', type=str, help='Delete a task list')
parser.add_argument('--delete', type=int, help='Delete a task from your list')
parser.add_argument('--rename', type=int, help='Edit a task in your list')
parser.add_argument('--renamelist', type=str, help='Edit the current task list')
parser.add_argument('--done', type=int, help='Mark a task as done')
parser.add_argument('--undo', type=int, help='Mark a done task as not done')
parser.add_argument('--add', type=str, help='Add a new task to your list')
args = parser.parse_args()
if args.license:
with open(license_path) as f:
print(f.read())
return
if args.version:
print('MyTask version 0.0.1')
return
if args.doc:
with open(readme_path) as f:
print(f.read())
return
if args.newlist:
create_new_list(args.newlist)
return
if args.setlist:
set_working_list(args.setlist)
return
if args.lists:
show_all_lists()
return
if args.show:
show_current_list()
return
if args.deletelist:
delete_list(args.deletelist)
return
if args.delete:
delete_task(args.delete)
return
if args.rename:
rename_task(args.rename)
return
if args.renamelist:
rename_list(args.renamelist)
return
if args.done:
mark_task_done(args.done)
return
if args.undo:
undo_task(args.undo)
return
if args.add:
add_task(args.add)
return
print("No valid arguments provided. Use --help for more information.")
if __name__ == "__main__":
main()