-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.py
executable file
·174 lines (126 loc) · 3.46 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
#!/usr/bin/python3
import datetime as dt
from sys import argv
from os import path, system
import os
#### TODO App
class ToDo:
"""docstring for ToDo"""
def __init__(self):
self.usage = '''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'''
def __repr__(self):
return self.usage
def __str__(self):
return self.usage
def write_in_file(self, file, content):
if not path.exists(file):
system('touch '+file)
with open(file, 'a+') as f:
if type(content) == list:
for i in content:
f.write(i)
else:
f.write(content)
def add(self, task):
with open('todo.txt', 'a+') as f:
f.write(task+'\n')
print(f'Added todo: "{task}"')
def ls(self):
## print in reversed order
len_ = len(self.read('todo.txt'))
if not len_:
print("There are no pending todos!")
return
contents = self.read('todo.txt')
count = len(contents)
for line in reversed(contents):
print("[{}] {}".format(count, line.strip()))
count -= 1
def done(self, num):
todo_len = len(self.read('todo.txt'))
if todo_len and todo_len >= num and num != 0:
cntnt = self.read('todo.txt', num-1)
self.write_in_file('done.txt', cntnt)
# delete from todo
contents = self.read('todo.txt')
os.unlink('todo.txt')
for ind, content in enumerate(contents):
if ind != num-1:
# print('writing', content)
self.write_in_file('todo.txt', content)
if num == 1:
os.system('touch todo.txt')
print(f"Marked todo #{num} as done.")
else:
print(f"Error: todo #{num} does not exist.")
def del_(self, num):
# delete from todo
todo_len = len(self.read('todo.txt'))
if todo_len >= num and num != 0:
contents = self.read('todo.txt')
os.unlink('todo.txt')
for ind, content in enumerate(contents):
if ind != num-1:
# print('writing', content)
self.write_in_file('todo.txt', content)
if todo_len == 1:
os.system('touch todo.txt')
print(f"Deleted todo #{num}")
else:
print(f"Error: todo #{num} does not exist. Nothing deleted.")
def read(self, file, num=None):
with open(file, 'r+') as f:
lines = f.readlines()
# print(lines)
if num is not None:
line = lines[num]
# print(num, line , lines)
return line
else:
# print('lines')
return lines
def trunc_file(self, file):
os.unlink(file)
os.system('touch ' + file)
def help(self):
print(self.usage)
def report(self):
date = dt.date.today()
print("{} Pending : {} Completed : {}".format(date.strftime("%Y-%m-%d"), \
len(self.read('todo.txt')), len(self.read('done.txt'))))
if __name__ == '__main__':
todo = ToDo()
# todo.trunc_file('todo.txt')
# todo.trunc_file('done.txt')
args = argv[1:]
len_ = len(args)
if len_ == 1:
if args[0] == 'ls':
todo.ls()
elif args[0] == 'report':
todo.report()
elif args[0] == 'help':
todo.help()
elif args[0] == 'add':
print("Error: Missing todo string. Nothing added!")
elif args[0] == 'del':
print("Error: Missing NUMBER for deleting todo.")
elif args[0] == 'done':
print("Error: Missing NUMBER for marking todo as done.")
else:
todo.help()
elif len_ == 2:
if args[0] == 'add':
todo.add(args[1])
elif args[0] == 'del':
todo.del_(int(args[1]))
elif args[0] == 'done':
todo.done(int(args[1]))
else:
todo.help()