-
Notifications
You must be signed in to change notification settings - Fork 1
/
todo.py
executable file
·267 lines (190 loc) · 5 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
#!/usr/bin/python
import argparse, re, os, ConfigParser as configparser
import fileinput, sys, datetime
def get_option(parser, section, option):
try:
value = parser.get(section, option)
except configparser.NoSectionError:
parser.add_section(section)
value = get_option(parser, section, option)
return value
def load_config(configfile='~/.todoconfig'):
loc = {}
glob = {}
fix = os.path.expanduser
parser = configparser.ConfigParser({'dir':'~','filename':'TODO','donefilename':'DONE'})
parser.read(fix(configfile))
glob['dir'] = fix(get_option(parser, 'global', 'dir'))
glob['filename'] = fix(get_option(parser, 'global', 'filename'))
glob['donefilename'] = fix(get_option(parser, 'global', 'donefilename'))
loc['filename'] = fix(get_option(parser, 'local', 'filename'))
loc['donefilename'] = fix(get_option(parser, 'local', 'donefilename'))
loc['dir'] = os.getcwd();
return glob, loc
def parsetask(line):
done, line = parsedone( line )
if re.match(r'\A *[\-xX ] ', line):
line = re.sub(r'\A *[\-xX ] ', "", line)
return done, line
def parsedone(line):
done = False
if re.match(r'\A *[xX] ', line):
done = True
return done, line
def parsefile(f):
tasks = []
linenum = 1
line = f.readline()
while line:
line = line.rstrip()
if line != "":
if re.match(r' [xX \-] ', line):
task.subtasks.append(Task(line, linenum))
else:
task = Task(line, linenum)
tasks.append(task)
line = f.readline()
linenum += 1
return tasks
def parsetasknum(tasknum):
parts = tasknum.split('.')
if len(parts) == 2:
return int(parts[0]), int(parts[1])
else:
return int(parts[0]), False
def marklinedone(furl, lineno):
for line in fileinput.input(furl, inplace=1):
if fileinput.filelineno() == lineno:
line = re.sub(r'\A( *)[-xX]? *',r'\1X ', line)
sys.stdout.write(line)
def removedone(furl, linenums):
for line in fileinput.input(furl, inplace=1):
if fileinput.filelineno() in linenums:
line = ""
sys.stdout.write(line)
class Task(object):
"""docstring for Task"""
def __init__(self, line, lineno):
self.done, self.task = parsetask(line)
self.line = lineno
self.subtasks = []
def add_task(args, furl):
f = open(furl, 'a')
if len(args):
f.write('- '+str.join(' ',args)+'\n')
else:
print 'Add Mode (leave field blank to quit)'
while True:
tasktext = raw_input('add> ')
tasktext = tasktext.strip()
if tasktext.lower() in ('done', '', 'quit', 'exit'):
break
f.write('- '+tasktext+'\n')
f.close()
def list_tasks(furl):
f = open(furl, 'r')
i = 0
for t in parsefile(f):
i += 1
j = 0
if t.done:
mark = 'X'
else:
mark = str(i)
print mark, t.task
for st in t.subtasks:
j += 1
if st.done:
mark = 'X'
else:
mark = str(i)+"."+str(j)
print " "+ mark, st.task
if t.subtasks:
print ""
f.close()
def archive_tasks(options):
furl = os.path.join(options['dir'], options['filename'])
doneurl = os.path.join(options['dir'], options['donefilename'])
f = open(furl, 'r')
tasklist = parsefile(f)
f.close()
donelist = []
date = datetime.date.today()
datestr = date.strftime('%d-%b-%Y')
#make done list
for t in tasklist:
if t.done:
donelist.append(t)
#add done to done.txt
f = open(doneurl, 'a')
for t in donelist:
f.write(datestr +' '+ t.task + '\n')
f.close()
#remove done from todo.txt
nums = []
for t in donelist:
nums.append(t.line)
removedone(furl, nums)
def do_task(text, furl):
f = open(furl, 'r')
tasklist = parsefile(f)
f.close()
for t in text:
i, j = parsetasknum(t)
task = tasklist[i-1]
if j:
task = task.subtasks[j-1]
linenum = task.line
marklinedone(furl, linenum)
print 'task', t, 'marked as done'
def main():
parser = argparse.ArgumentParser(description='Task command line utility')
group = parser.add_mutually_exclusive_group()
group.add_argument('-g', '--global',
dest='globaltask',
action='store_true',
help='force use of users global todo.txt'
)
group.add_argument('-l', '--local',
dest='localtask',
action='store_true',
help='force use of directories local todo.txt'
)
parser.add_argument('command',
metavar='<command>',
choices=['add', 'a', 'do', 'list', 'ls', 'archive'],
default='ls', nargs='?',
action='store'
)
parser.add_argument('args',
metavar='<args>',
nargs='*', type=str,
help='depends on command'
)
args = parser.parse_args()
glob, loc = load_config()
if os.path.isfile('.todo'):
dontcare, loc = load_config('.todo')
options = {}
if args.localtask:
options = loc
elif args.globaltask:
options = glob
elif os.path.isfile(loc['filename']):
options = loc
else:
options = glob
path = os.path.join(options['dir'], options['filename'])
if not os.path.isfile(path):
f = open(path, 'w')
f.close()
if args.command in ('ls','list'):
list_tasks(path)
if args.command in ('add','a'):
add_task(args.args, path)
if args.command == 'do':
do_task(args.args, path)
if args.command == 'archive':
archive_tasks(options)
if __name__ == '__main__':
main()