-
Notifications
You must be signed in to change notification settings - Fork 0
/
todobase.py
79 lines (70 loc) · 2.1 KB
/
todobase.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
#!/usr/bin/env python
# encoding: utf-8
import os
import subprocess
class TodoLine (object):
def __init__ (self, line):
self.line = line
self.todonr = -1
self.pri = ''
line = line.strip().split(' ')
if len(line) < 2:
self.done=False
return
self.linenr = int(line[0])
self.done = line[1] == 'x'
if self.done:
self.donedate = line[2]
self.args = line[3:]
else:
self.args = line[1:]
msg = []
context = []
for e in self.args:
if e.startswith('[') and e.endswith(']') and e[1:-1].isdigit():
self.todonr = int(e[1:-1])
elif e.startswith('(') and e.endswith(')') and len(msg) == 0:
self.pri = e[1:-1]
elif e.startswith('@'):
context.append(e[1:])
else:
msg.append(e)
self.msg = ' '.join(msg)
self.context = '.'.join(context)
def __cmp__ (self, other):
if isinstance(other, TodoLine):
a = cmp(self.context, other.context)
if a == 0:
a = cmp(self.todonr, other.todonr)
return a
else:
return cmp(self.todonr, other)
def __unicode__ (self):
return '%s.%d %s' % (self.context, self.todonr, self.msg)
def __str__ (self):
return str(self.__unicode__())
def listidx (line, lsall=False):
line.append('\[[[:digit:]]*\]')
V = os.environ['TODOTXT_VERBOSE']
os.environ['TODOTXT_VERBOSE'] = '0'
stdout = call(lsall and 'listall' or 'list', line, ['-p'])
os.environ['TODOTXT_VERBOSE'] = V
lines = filter(lambda x: x.todonr>=0, [TodoLine(x) for x in stdout.split('\n')])
return lines
def findnextidx(line):
idx = [x.todonr for x in listidx([y for y in line if y.startswith('@')], lsall=True)]
if len(idx) == 0:
return 1
return max(idx) + 1
def call(cmdname, args, opts=[]):
cmd = [os.environ['TODO_FULL_SH']]
cmd += opts
cmd += ['command', cmdname]
cmd += args
shcall = subprocess.Popen(cmd, stdout=subprocess.PIPE)
stdout, x = shcall.communicate()
return stdout
def gettodoline(linenr):
f = open(os.environ['TODO_FILE'], 'r')
[f.readline() for i in range(linenr-1)]
return TodoLine("%d %s" % (linenr, f.readline().strip()))