-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.py
More file actions
221 lines (169 loc) · 7.05 KB
/
Copy pathcore.py
File metadata and controls
221 lines (169 loc) · 7.05 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
import datetime
import json
class Task:
DEFAULT_DESCRIPTION = "No description provided"
def __init__(self, taskID = None, description = None, dateAdded = None, dueDate = None):
if taskID is None:
raise NotImplementedError
if description is None:
description = Task.DEFAULT_DESCRIPTION
if dateAdded is None:
dateAdded = datetime.datetime.now()
if dueDate is None:
dueDate = datetime.datetime.now() + datetime.timedelta(days = 2)
self.taskID = taskID
self.description = description
self.dateAdded = dateAdded
self.dueDate = dueDate
def print_info(self):
print("{} {} {} {}".format(str(self.taskID).ljust(35), self.description.ljust(35), str(self.dateAdded).ljust(35), str(self.dueDate).ljust(35)))
class TaskManager:
DEFAULT_FILE = "data.json"
def __init__(self, tasks = None, dataFile = None):
if tasks is None:
tasks = []
if dataFile is None:
dataFile = TaskManager.DEFAULT_FILE
self.tasks = tasks
self.dataFile = dataFile
self.load_tasks()
def load_tasks(self):
file = open(self.dataFile, "r")
task_list = json.loads(file.read())
for task in task_list:
self.tasks.append(Task(taskID = task['taskID'], description = task['description'], dateAdded = datetime.datetime.strptime(task['dateAdded'], '%Y-%m-%d %H:%M:%S.%f'), dueDate=datetime.datetime.strptime(task['dueDate'], '%Y-%m-%d %H:%M:%S.%f')))
def dump_tasks(self):
file = open(self.dataFile, "w")
assert file is not None
file.write(json.dumps([x.__dict__ for x in self.tasks], default = myconverter))
def show_all_tasks(self):
print("{} {} {} {}".format('Task ID'.ljust(35), 'Task Description'.ljust(35), 'Date Added'.ljust(35), 'Due Date'.ljust(35)))
for task in self.tasks:
task.print_info()
#get the lowest available ID
def get_new_ID(self):
i = 1
existingIDs = [x.taskID for x in self.tasks]
existingIDs.sort()
while i <= len(existingIDs):
if i != existingIDs[i-1]:
return i
i += 1
return i
class Parser:
VALID_COMMANDS = ['show day', 'show week', 'show all', 'help', 'add', 'update', 'exit', 'delete']
def __init__(self, taskManager = None):
if taskManager is None:
taskManager = TaskManager()
self.taskManager = taskManager
def processCommand(self, command):
if command not in Parser.VALID_COMMANDS:
print("No Command Found")
else:
if command == 'show day':
self.processShowRange(0)
elif command == 'show week':
self.processShowRange(6)
elif command == 'show all':
self.processShow()
elif command == 'help':
self.processHelp()
elif command == 'add':
self.processAdd()
elif command == 'exit':
self.processExit()
elif command == 'update':
self.processUpdate()
elif command == 'delete':
self.processDelete()
def processShow(self):
self.taskManager.show_all_tasks()
def processShowRange(self, days):
print("{} {} {} {}".format('Task ID'.ljust(35), 'Task Description'.ljust(35), 'Date Added'.ljust(35), 'Due Date'.ljust(35)))
for task in self.taskManager.tasks:
if task.dueDate < datetime.datetime.now() + datetime.timedelta(days = days):
task.print_info()
def processHelp(self):
print("Hello!")
print("List of available commands:")
for command in Parser.VALID_COMMANDS:
print(command)
def processAdd(self):
newTaskId = self.taskManager.get_new_ID()
print("New Task ID:" + str(newTaskId))
print("Enter task description:")
description = input()
dueDate = None
while True:
print("Enter due date in YYYY-MM-DD OR just press enter for a due date 2 days from now:")
date = input()
if self.checkValidDate(date):
dueDate = datetime.datetime.strptime(date + " 00:00:00.000001", '%Y-%m-%d %H:%M:%S.%f')
break
elif date == "":
break
task = Task(taskID = newTaskId, description = description, dueDate = dueDate)
self.taskManager.tasks.append(task)
def processDelete(self):
while True:
validInput = True
print("Enter task ID you wish to delete:")
try:
ID = int(input())
except ValueError:
print("That's not a task ID!")
validInput = False
finally:
if validInput:
break
existingIDs = [x.taskID for x in self.taskManager.tasks]
if ID not in existingIDs:
print("No such ID exists")
return
else:
for i in range(len(self.taskManager.tasks)):
if self.taskManager.tasks[i].taskID == ID:
self.taskManager.tasks.pop(i)
return
def processUpdate(self):
while True:
validInput = True
print("Enter task ID you wish to update:")
try:
ID = int(input())
except ValueError:
print("That's not a task ID!")
validInput = False
finally:
if validInput:
break
existingIDs = [x.taskID for x in self.taskManager.tasks]
if ID not in existingIDs:
print("No such ID exists")
return
else:
for i in range(len(self.taskManager.tasks)):
if self.taskManager.tasks[i].taskID == ID:
print("Enter updated description:")
description = input()
dueDate = None
while True:
print("Enter updated due date in YYYY-MM-DD OR just press enter for a due date 2 days from now:")
date = input()
if self.checkValidDate(date):
dueDate = datetime.datetime.strptime(date + " 00:00:00.000001", '%Y-%m-%d %H:%M:%S.%f')
break
elif date == "":
break
self.taskManager.tasks[i].description = description
self.taskManager.tasks[i].dueDate = dueDate
def processExit(self):
self.taskManager.dump_tasks()
exit()
def checkValidDate(self, date):
if len(date) == 10 and len(date.split('-')) == 3 and date[4] == '-' and date[7] == '-':
return True
return False
def myconverter(obj):
if isinstance(obj, datetime.datetime):
return obj.__str__()