-
Notifications
You must be signed in to change notification settings - Fork 6
/
todo.py
194 lines (169 loc) · 6.98 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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os.path
from datetime import datetime
def printHelp():
todohelp = \
"""Usage :-
$ ./todo add "todo item" # Add a new todo
$ ./todo ls # Show remaining todos
$ ./todo del NUMBER # Delete a todo
$ ./todo done NUMBER # Complete a todo
$ ./todo help # Show usage
$ ./todo report # Statistics"""
sys.stdout.buffer.write(todohelp.encode('utf8'))
def addToList(st):
if os.path.isfile('todo.txt'):
with open('todo.txt', 'r') as todoFileOri:
data = todoFileOri.read()
with open('todo.txt', 'w') as todoFileMod:
todoFileMod.write(st + '\n' + data)
else:
with open('todo.txt', 'w') as todoFile:
todoFile.write(st + '\n')
sys.stdout.buffer.write('Added todo: "{}"'.format(st).encode('utf8'
))
def showList():
if os.path.isfile('todo.txt'):
with open('todo.txt', 'r') as todoFileOri:
data = todoFileOri.readlines()
ct = len(data)
st = ''
for line in data:
st += '[{}] {}'.format(ct, line)
ct -= 1
sys.stdout.buffer.write(st.encode('utf8'))
else:
sys.stdout.buffer.write('There are no pending todos!'.encode('utf8'
))
def delFromList(num):
if os.path.isfile('todo.txt'):
with open('todo.txt', 'r') as todoFileOri:
data = todoFileOri.readlines()
ct = len(data)
if num > ct or num <= 0:
sys.stdout.buffer.write('Error: todo #{} does not exist. Nothing deleted.'.format(num).encode('utf8'
))
else:
with open('todo.txt', 'w') as todoFileMod:
for line in data:
if ct != num:
todoFileMod.write(line)
ct -= 1
sys.stdout.buffer.write('Deleted todo #{}'.format(num).encode('utf8'
))
else:
sys.stdout.buffer.write('Error: todo #{} does not exist. Nothing deleted.'.format(num).encode('utf8'
))
def markDone(num):
if os.path.isfile('todo.txt'):
with open('todo.txt', 'r') as todoFileOri:
data = todoFileOri.readlines()
ct = len(data)
if num > ct or num <= 0:
sys.stdout.buffer.write('Error: todo #{} does not exist.'.format(num).encode('utf8'
))
else:
with open('todo.txt', 'w') as todoFileMod:
if os.path.isfile('done.txt'):
with open('done.txt', 'r') as doneFileOri:
doneData = doneFileOri.read()
with open('done.txt', 'w') as doneFileMod:
for line in data:
if ct == num:
doneFileMod.write('x '
+ datetime.today().strftime('%Y-%m-%d'
) + ' ' + line)
else:
todoFileMod.write(line)
ct -= 1
doneFileMod.write(doneData)
else:
with open('done.txt', 'w') as doneFileMod:
for line in data:
if ct == num:
doneFileMod.write('x '
+ datetime.today().strftime('%Y-%m-%d'
) + ' ' + line)
else:
todoFileMod.write(line)
ct -= 1
sys.stdout.buffer.write('Marked todo #{} as done.'.format(num).encode('utf8'
))
else:
sys.stdout.buffer.write('Error: todo #{} does not exist.'.format(num).encode('utf8'
))
def generateReport():
countTodo = 0
countDone = 0
if os.path.isfile('todo.txt'):
with open('todo.txt', 'r') as todoFile:
todoData = todoFile.readlines()
countTodo = len(todoData)
if os.path.isfile('done.txt'):
countDone = 0
with open('done.txt', 'r') as doneFile:
doneData = doneFile.readlines()
for i in doneData:
temp = i.split()
if temp[1] == str(datetime.today().strftime('%Y-%m-%d'
)):
countDone += 1
st = datetime.today().strftime('%Y-%m-%d') \
+ ' Pending : {} Completed : {}'.format(countTodo, countDone)
sys.stdout.buffer.write(st.encode('utf8'))
def main():
if len(sys.argv) == 1:
printHelp()
elif sys.argv[1] == 'help':
if len(sys.argv) == 2:
printHelp()
else:
sys.stdout.buffer.write('Too Many Arguments for help! Please use "./todo help" for Usage Information'.encode('utf8'
))
elif sys.argv[1] == 'ls':
if len(sys.argv) == 2:
showList()
else:
sys.stdout.buffer.write('Too Many Arguments for ls! Please use "./todo help" for Usage Information'.encode('utf8'
))
elif sys.argv[1] == 'add':
if len(sys.argv) == 3:
addToList(sys.argv[2])
else:
sys.stdout.buffer.write('Error: Missing todo string. Nothing added!'.encode('utf8'
))
elif sys.argv[1] == 'del':
if len(sys.argv) == 3:
try:
val = int(sys.argv[2])
delFromList(int(sys.argv[2]))
except ValueError:
sys.stdout.buffer.write('Given arguement for del is not an integer! Please use "./todo help" for Usage Information'.encode('utf8'
))
else:
sys.stdout.buffer.write('Error: Missing NUMBER for deleting todo.'.encode('utf8'
))
elif sys.argv[1] == 'done':
if len(sys.argv) == 3:
try:
val = int(sys.argv[2])
markDone(int(sys.argv[2]))
except ValueError:
sys.stdout.buffer.write('Given arguement for done is not an integer! Please use "./todo help" for Usage Information'.encode('utf8'
))
else:
sys.stdout.buffer.write('Error: Missing NUMBER for marking todo as done.'.encode('utf8'
))
elif sys.argv[1] == 'report':
if len(sys.argv) == 2:
generateReport()
else:
sys.stdout.buffer.write('Too Many Arguments for report! Please use "./todo help" for Usage Information'.encode('utf8'
))
else:
sys.stdout.buffer.write('Option Not Available. Please use "./todo help" for Usage Information'.encode('utf8'
))
if __name__ == '__main__':
main()