/
todo.py
267 lines (221 loc) · 7.39 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# conding: utf-8
import sys, re
import getopt
DEBUG = True
NONE_PRIORITY = "z" # 默认优先级
MAGIC_REPLACE_PLACEHOLDER = "%" # edit模式下代替原本文字
complete_reg = r"^x "
complete_prefix = "x"
priority_reg = r"^\([A-Z]\) "
date_reg = r"^[0-9]{4}-[0-9]{2}-[0-9]{2} "
project_reg = r"\+[^ ]+ {0,1}"
project_prefix = "+"
context_reg = r"@[^ ]+ {0,1}"
context_prefix = "@"
def is_int(str):
try:
int(str)
return True
except ValueError:
return False
class Task:
def __init__(self, text):
self.text = text
self.priority = NONE_PRIORITY
self.perpendedDate = None
self.projects = []
self.contexts = []
self.isCompleted = False
self.completedDate = None
self.taskContent = None
self.id = 0
def __str__(self):
return "#obj Task (" \
+ " isCompleted=" + str(self.isCompleted) \
+ ", completedDate=" + str(self.completedDate) \
+ ", priority=" + str(self.priority) \
+ ", perpendedDate=" + str(self.perpendedDate) \
+ ", projects=" + str(self.projects) \
+ ", contexts=" + str(self.contexts) \
+ ", taskContent=" + str(self.taskContent) \
+ ""
def parseTask(text):
rest = text
task = Task(text)
# -- Complete Tasks: 2 Format Rules --
# Rule 1: A completed task starts with an x.
m = re.search(complete_reg, rest)
if m is not None:
match = m.group(0)
task.isCompleted = True
rest = rest.replace(match, "")
#else:
#print( "not complete" )
if task.isCompleted:
m = re.search(date_reg, rest)
if m is not None:
match = m.group(0)
task.completedDate = match.strip().replace("(", "").replace(")", "")
rest = rest.replace(match, "")
#else:
# print( "no date" )
# -- Incomplete Tasks: 3 Format Rules --
# Rule 1: If priority exists, it ALWAYS appears first.
m = re.search(priority_reg, rest)
if m is not None:
match = m.group(0)
task.priority = match.strip().replace("(", "").replace(")", "")
rest = rest.replace(match, "")
#else:
#print( "no priotiry" )
# Rule 2: A task’s creation date may optionally appear directly after priority and a space.
m = re.search(date_reg, rest)
if m is not None:
match = m.group(0)
task.prependedDate = match.strip().replace("(", "").replace(")", "")
rest = rest.replace(match, "")
#else:
#print( "no date" )
# Rule 3: Contexts and Projects may appear anywhere in the line after priority/prepended date.
m = re.findall(project_reg, rest)
for item in m:
rest = rest.replace(item, "")
item = item.strip().replace(project_prefix, "")
task.projects.append( item )
m = re.findall(context_reg, rest)
for item in m:
rest = rest.replace(item, "")
item = item.strip().replace(context_prefix, "")
task.contexts.append( item )
task.textContent = rest.strip()
return task
def usage():
print( """USAGE: todo.py -a yourtask
options:
-h --help show help info
-a --add add a new task
-l --list list all tasks(sorted)
-o --origin show origin text
-i --input input text file name
""" )
def list(lines):
todoTasks = []
completedTasks = []
numOfLine = 1
for line in lines:
if line:
task = parseTask( line )
task.id = numOfLine
#print( task )
if not task.isCompleted:
todoTasks.append(task)
else:
completedTasks.append(task)
numOfLine += 1
todoTasks = sorted(todoTasks, key=lambda task: task.priority) # sort by priority
#completedTasks = sorted(completedTasks, key=lambda task: task.completedDate, reverse=True) # sort by completedDate
tasks = todoTasks + completedTasks
for t in tasks:
#print( t )
if not t.isCompleted:
print(" " + str(t.id) + ": " + t.text.strip() )
def edit(filename, numOfLine, newText):
f = open(filename, "r+")
lines = f.readlines()
f.close()
f = open(filename, "w")
#print( lines )
#lines = lines[:numOfLine] + lines[numOfLine+1:]
try:
orig = lines[numOfLine-1].strip()
lines[numOfLine-1] = newText.replace(MAGIC_REPLACE_PLACEHOLDER, orig) + "\n"
f.writelines(lines)
except:
print(str(numOfLine) + " task is not exists")
finally:
f.close()
def getThingsDone(filename, numOfLine):
edit(filename, numOfLine, complete_prefix + " " + MAGIC_REPLACE_PLACEHOLDER)
def handleCommand(command, filename, args):
if command == "list":
f = open(filename, "r+")
list(f.readlines())
f.close()
elif command == "add":
text = " ".join(args)
f = open(filename, "a+")
f.write(text+"\n")
f.close()
# re-open
f = open(filename, "r+")
list(f.readlines())
f.close()
elif command == "origin":
f = open(filename, "r+")
for line in f:
print(line.strip())
f.close()
elif command == "done":
if not is_int(args[0]):
print("Number of line(args0) is not a integer" )
return
numOfLine = int(args[0])
getThingsDone(filename, numOfLine)
# re-open
f = open(filename, "r+")
list(f.readlines())
f.close()
elif command == "edit":
if not is_int(args[0]):
print("Number Of line(args0) is not a integer" )
return
numOfLine = int(args[0])
text = " ".join(args[1:])
edit(filename, numOfLine, text)
# re-open
f = open(filename, "r+")
list(f.readlines())
f.close()
elif command == "clean":
confirm = input("Are you sure? It will delete ALL Tasks! [Y/N]: ")
if confirm == "Y":
f = open(filename, "w")
# delete all lines
f.close()
elif command == "clear":
print( "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" )
else:
print("Cann't found command: %s " % command)
def main(argv):
try:
opts, args = getopt.getopt(argv, "a:hloi:", ["add=", "help", "list", "origin", "input="])
except getopt.GetoptError:
usage()
sys.exit(2)
_filename = "todo.txt"
_text = None
command = "list"
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
elif opt in ("-a", "--add"):
command = "add"
_text = arg
elif opt in ("-l", "--list"):
command = "list"
elif opt in ("-o", "--origin"):
command = "origin"
if opt in ("-i", "--input"):
_filename = arg
handleCommand(command, _filename, _text)
while True:
command = input(":")
tmp = command.split(" ")
command = tmp[0]
args = tmp[1:]
if command == "exit":
sys.exit()
handleCommand(command, _filename, args)
if __name__ == "__main__":
main(sys.argv[1:])