-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.py
104 lines (92 loc) · 3.52 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
#!/usr/bin/python3
import argparse
import sys
import time
import datetime
def todo(args):
# condition for displaying help
if(args.o[0]=='help'):
help = """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"""
return help
# Addition of new todo
if(args.o[0]=='add'):
try:
with open('todo.txt',"a") as f:
f.write(args.o[1]+'\n')
return f'Added todo: "{args.o[1]}"'
except:
return 'Error: Missing todo string. Nothing added!'
#Deleting a todo
if(args.o[0]=='del' and len(sys.argv)==2):
return 'Error: Missing NUMBER for deleting todo.'
if(args.o[0]=='del' and len(sys.argv)==3):
with open('todo.txt',"r") as f:
reads = f.readlines()
if(len(reads)==0 or int(sys.argv[2])<1 or int(sys.argv[2])>len(reads)):
return f'Error: todo #{args.o[1]} does not exist. Nothing deleted.'
else:
reads.pop(int(args.o[1]))
with open('todo.txt',"w") as f:
for read in reads:
f.write(read)
return f'Deleted todo #{args.o[1]}\n'
#Displaying the todos
if(args.o[0]=='ls'):
with open('todo.txt','r') as f:
reads = f.readlines()
if(len(reads)==0):
return 'There are no pending todos!\n'
else:
reads.reverse()
str1=''
count = len(reads)
for read in reads:
str1+=f'[{count}] {read}'
count = count-1
return str1
#Marking todos as done
if(args.o[0]=='done' and len(sys.argv)==2):
return f'Error: Missing NUMBER for marking todo as done.\n'
if(args.o[0]=='done' and len(sys.argv)==3):
with open('todo.txt',"r") as f:
reads = f.readlines()
if(len(reads)==0 or int(sys.argv[2])<1 or int(sys.argv[2])>len(reads)):
return f'Error: todo #{sys.argv[2]} does not exist.'
else:
with open('done.txt',"a") as f:
f.write(reads[int(sys.argv[2])-1])
with open('todo.txt',"r") as f:
reads = f.readlines()
reads.pop(int(args.o[1])-1)
with open('todo.txt',"w") as f:
for read in reads:
f.write(read)
return f'Marked todo #{sys.argv[2]} as done.'
#Reporting the todos to user
if(args.o[0]=='report'):
with open('todo.txt','r') as f:
reads = f.readlines()
with open('done.txt','r') as f1:
reads1 = f1.readlines()
return f'{time.strftime("%Y-%m-%d")} Pending : {len(reads)} Completed : {len(reads1)} '
if __name__ == '__main__':
parser=argparse.ArgumentParser()
parser.add_argument('o', nargs='+')
if len(sys.argv)==1:
help = """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"""
print(help)
sys.exit(1)
args=parser.parse_args()
sys.stdout.write(str(todo(args)))