-
Notifications
You must be signed in to change notification settings - Fork 4
/
todo.py
123 lines (107 loc) · 3.78 KB
/
todo.py
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
import argparse
import json
from datetime import datetime
import os
class Task:
def __init__(self, task_id, job, time = datetime.now(), completed = False):
self.task_id = task_id
self.job = job
self.time = time
self.complete = completed
def __str__(self):
s = str(self.task_id) + ". " + self.job + "\t" + str(self.time) + "\t"
if self.complete:
s = s + "Complete"
else:
s = s + "Not Complete"
return s
class Job:
def __init__(self):
self._directory = os.path.join(os.path.expanduser('~'), '.config', 'todo')
self._filepath = os.path.join(self._directory, '.todo')
self._open_file()
self._task_number = self._tasks[0] + 1
def print_tasks(self):
if self._fname.closed:
self._open_file()
for task in self._tasks[1:]:
print (task)
json.dump(self.json(), self._fname)
self._fname.close()
def add(self, arg):
if self._fname.closed:
self._open_file()
self._tasks[0] += 1
entry_number = self._tasks[0]
self._tasks.append(Task(self._task_number, arg))
json.dump(self.json(), self._fname)
self._fname.close()
def _open_file(self):
self._tasks = []
if not os.path.exists(self._directory):
os.makedirs(self._directory)
self._tasks = [0]
elif not os.path.exists(self._filepath):
self._tasks = [0]
self.fname = open(self._filepath, 'w+')
else:
self._fname = open(self._filepath, 'a+')
if not self._tasks:
try:
# error is possible in json.load if fname is empty
data = json.load(self._fname)
self._tasks = [data[0]]
# converting all the string versions of the tasks to
# Task instances
for task_str in data[1:]:
task_id, job, time, complete = task_str
time = datetime.strptime(time, "%Y-%m-%d %H:%M:%S.%f")
self._tasks.append(Task(task_id, job, time, complete))
except ValueError:
self._tasks = [0]
self._fname = open(self._filepath, 'w+')
def remove(self, args):
t = self._find_task(args)
self._tasks.remove(t)
json.dump(self.json(), self._fname)
self._fname.close()
def check_status(self, args):
t = self._find_index(args)
return self._tasks[t].complete
def completed(self, args):
if self._fname.closed:
self._open_file()
t = self._find_task(args)
t.complete = not t.complete
json.dump(self.json(), self._fname)
self._fname.close()
def _find_task(self, args):
for t in self._tasks[1:]:
if t.task_id == int(args):
return t
def json(self):
lst = []
lst.append(self._tasks[0])
for task in self._tasks[1:]:
x = [task.task_id, task.job, str(task.time), task.complete]
lst.append(x)
return lst
if __name__ == '__main__':
job = Job()
parser = argparse.ArgumentParser()
parser.add_argument("-a", "--add", help = "add a task")
parser.add_argument("-r", "--remove", help = "remove a task")
parser.add_argument("-l", "--list", action = "store_true", help = "print tasks list")
parser.add_argument("-c", "--complete", help = "mark a task as completed")
args = parser.parse_args()
if args.add:
job.add(args.add)
job.print_tasks()
elif args.remove:
job.remove(args.remove)
print("List of tasks")
job.print_tasks()
elif args.list:
job.print_tasks()
elif args.complete:
job.completed(args.complete)