-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.py
404 lines (336 loc) · 12.3 KB
/
parser.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import os, re, sys, string, time
from todo import Todo
# A parser to read the todo.txt file
class TodotxtParser:
tracks_mapping = {
'description' : 'description',
'context' : 'context',
'project' : 'project',
}
todo_dir ="~/.todo"
todo_file ="$TODO_DIR/todo.txt"
done_file ="$TODO_DIR/done.txt"
report_file ="$TODO_DIR/report.txt"
todo_actions_dir ="$TODO_DIR/actions"
verbose = False
data = {}
def __init__(self, **kargs):
if 'verbose' in kargs and kargs['verbose'] == True:
self.verbose = True
self.expandTodoDir()
def completeTodo(self, line_number):
line = self.getLine(line_number)
today = time.strftime('%Y-%m-%d')
line = '\nx ' + today + ' ' + line + '\n'
self.removeTodo(line_number)
todo_file = open(self.getLocation('done'), 'a')
todo_file.write(line)
todo_file.close()
def fetchRemoteData(self):
self.remote_todos = self.remote_client.getTodos()
self.remote_projects = self.remote_client.getProjects()
self.remote_contexts = self.remote_client.getContexts()
def localTodoHasSameDescription(self,description):
todos = self.getTodos()
for [index, todo] in todos.items():
if todo.getDescription() == description:
return index
return False
def getTodoByTracksId(self, tracks_id):
todos = self.getTodos()
for [index, todo] in todos.items():
if todo.getTracksId() == tracks_id:
return index
return False
def exportToTracks(self, tracks_client, refetch = False):
if refetch:
self.fetchRemoteData()
added_projects = ['default']
remote_projects = [remote_project['name'] for remote_project in self.remote_projects]
new_projects = False
for project in self.getProjects():
if project not in remote_projects and project not in added_projects:
if self.verbose:
print "Adding local project %s to remote Tracks instance" % project
new_projects = True
tracks_client.addProject({'name' : project})
added_projects.append(project)
if new_projects:
self.remote_projects = self.remote_client.getProjects()
added_contexts = ['default']
remote_contexts = [remote_context['name'] for remote_context in self.remote_contexts]
new_contexts = False
for context in self.getContexts():
if context not in remote_contexts and context not in added_contexts:
if self.verbose:
print "Adding local context %s to remote Tracks instance" % context
new_contexts = True
tracks_client.addContext({'name' : context})
added_contexts.append(context)
if new_contexts:
self.remote_contexts = self.remote_client.getContexts()
remote_todos = [remote_todo['description'] for remote_todo in self.remote_todos]
todos = self.getTodos().items()
for [index, todo] in todos:
tracks_id = None
if todo.getDescription() not in remote_todos and todo.getTracksId() == None:
if self.verbose:
print "Adding local todo %s to remote Tracks instance" % todo.getDescription()
tracks_id = tracks_client.addTodo(todo)
self.data['todos'][index].setTracksId(tracks_id)
elif todo.getTracksId():
for remote_todo in self.remote_todos:
if remote_todo['id'] == todo.getTracksId():
if todo.isDone() and remote_todo['state'] != 'completed':
tracks_client.updateTodo(todo)
def importFromTracks(self, tracks_client):
if self.verbose:
print "Contacting remote Tracks instance"
self.remote_client = tracks_client
self.fetchRemoteData()
count = 0
for remote_todo in self.remote_todos:
index_of_local_instance = self.getTodoByTracksId(remote_todo['id'])
if index_of_local_instance:
if remote_todo['state'] == 'completed':
self.data['todos'][index_of_local_instance].setDone(True)
self.data['todos'][index_of_local_instance].setCompletedDate(remote_todo['completed-at'][0:10])
continue
# Update the tid on the local todo instead of creating a new record remotely
index_of_similar_todo = self.localTodoHasSameDescription(remote_todo['description'])
if index_of_similar_todo != False:
self.data['todos'][index_of_similar_todo].setTracksId(remote_todo['id'])
continue
new_todo = {}
new_todo['tracks_id'] = remote_todo['id']
for [old_name, new_name] in self.tracks_mapping.items():
count += 1
if old_name in remote_todo:
new_todo[new_name] = remote_todo[old_name]
if remote_todo['state'] == 'active':
if self.verbose:
print "Adding active todo"
self.addTodo(new_todo)
else:
if self.verbose:
print "Adding completed todo"
self.addTodo(new_todo, 'done')
# Doing this for now as a way to detect when a remote todo is set to 'done'
# Not great, but /todos/done.xml takes too long to parse
# without some ability to limit results
for index, local_todo in self.data['todos'].items():
if local_todo.getTracksId() != None and not local_todo.isDone():
remote_missing = True
for remote_todo in self.remote_todos:
if remote_todo['id'] == local_todo.getTracksId():
remote_missing = False
break
if remote_missing:
self.data['todos'][index].setDone(True)
self.data['todos'][index].setCompletedDate(time.strftime('%Y-%m-%d'))
def getLine(self, line_number):
todo_file = open(self.getLocation('todo'), 'r')
lines = todo_file.readlines()
todo_file.close()
return lines[line_number - 1]
def addTodo(self, data, todo_type = 'todo'):
todo = Todo(data)
# Replace spaces to underscores to avoid todo.txt limitation
if data['project']:
data['project'] = data['project'].replace(' ','_')
if data['context']:
data['context'] = data['context'].replace(' ','_')
next_id = self.getNextId()
if todo_type == 'todo':
todo.setDone(False)
elif todo_type == 'done':
todo.setDone()
todo.setCompletedDate(time.strftime('%Y-%m-%d'))
self.data['todos'][next_id] = todo
self.data['ids'].append(next_id)
if 'context' not in data:
todo.setContext('default')
if 'tracks_id' not in data:
todo.setTracksId(None)
# Replace spaces to underscores to avoid todo.txt limitation
if todo.getContext().replace(' ','_') not in self.data['contexts']:
self.addContext(todo.getContext().replace(' ','_'))
self.data['contexts'][data['context']].append(next_id)
if 'project' not in data:
data['project'] = 'default'
if data['project'] not in self.data['projects']:
self.addProject(data['project'])
self.data['projects'][data['project']].append(next_id)
def addContext(self, context):
self.data['contexts'][context] = []
def addProject(self, project):
self.data['projects'][project] = []
def makeTodoLine(self, data):
new_todo = '\n' + data['description']
if data['context'] != None:
new_todo += " @" + data['context']
if data['project'] != None:
new_todo += " +" + data['project']
if 'tracks_id' in data:
new_todo += " tid:" + data['tracks_id']
return new_todo
def addTodoLine(self, data, todo_type = 'todo'):
todo_file = open(self.getLocation(todo_type), 'a')
new_todo = self.makeTodoLine(data)
todo_file.write(new_todo)
def removeTodo(self, line_number):
todo_file = open(self.getLocation('todo'), 'r')
lines = todo_file.readlines()
lines = [line.strip() for line in lines]
todo_file.close()
lines.pop(line_number - 1)
todo_text = string.join(lines, '\n') + '\n'
todo_file = open(self.getLocation('todo'), 'w')
todo_file.write(todo_text)
todo_file.close()
def getTodos(self, todo_type = None):
todos = self.data['todos'].items()
todo_set = {}
for [index, todo] in todos:
if todo_type == None:
todo_set[index] = todo
elif todo.isDone() == False and todo_type == 'todo':
todo_set[index] = todo
elif todo.isDone() == True and todo_type == 'done':
todo_set[index] = todo
return todo_set
def getData(self):
return self.data
def getContexts(self):
return self.data['contexts']
def getProjects(self):
return self.data['projects']
def getNextId(self):
return max(self.data['ids']) + 1
def load(self):
self.data = {
'todos' : {},
'projects' : {},
'contexts' : {},
'ids' : [],
}
todo_items = self.getRawTodos('todo')
done_items = self.getRawTodos('done')
id = 0
for item_list in [todo_items, done_items]:
for item in item_list:
id += 1
self.data['ids'].append(id)
todo = self.parseLine(item)
self.data['todos'][id] = todo
if todo.getProject() not in self.data['projects']:
self.data['projects'][todo.getProject()] = [id]
else:
self.data['projects'][todo.getProject()].append(id)
if todo.getContext() not in self.data['contexts']:
self.data['contexts'][todo.getContext()] = [id]
else:
self.data['contexts'][todo.getContext()].append(id)
def parseLine(self, item):
pattern = r'@(\w+?)( |$)'
m = re.search(pattern, item)
if m and m.group(1):
context = m.group(1)
else:
context = 'default'
item = re.sub(pattern, '', item).strip()
pattern = r'\+(\w+?)( |$)'
m = re.search(pattern, item)
if m and m.group(1):
project = m.group(1)
else:
project = 'default'
item = re.sub(pattern, '', item).strip()
pattern = r'^x '
m = re.search(pattern, item, re.IGNORECASE)
if m:
done = True
else:
done = False
item = re.sub(pattern, '', item).strip()
pattern = r'^(\d\d\d\d-\d\d-\d\d)'
m = re.search(pattern, item)
if m and m.group(1):
completed = m.group(1)
else:
completed = None
item = re.sub(pattern, '', item).strip()
pattern = r'tid\:(\w+)( |$)'
m = re.search(pattern, item)
if m and m.group(1):
tracks_id = m.group(1)
else:
tracks_id = None
item = re.sub(pattern, '', item).strip()
row = { 'description' : item,
'done' : done,
'context' : context,
'project' : project,
'completed' : completed,
'tracks_id' : tracks_id
}
todo = Todo(row)
return todo
def getTodoLines(self, type = 'todo'):
lines = ""
for [index, todo] in self.data['todos'].items():
line = todo.getTextLine()
if todo.isDone() == True and type =='done':
lines += line + "\n"
elif todo.isDone() == False and type == 'todo':
lines += line + "\n"
return lines.strip()
def writeData(self):
todo_todos = self.getTodos('todo')
self.writeTodosToFile(todo_todos)
done_todos = self.getTodos('done')
self.writeTodosToFile(done_todos, 'done')
def writeTodosToFile(self, todos, todo_type = 'todo'):
todo_file = open(self.getLocation(todo_type), 'w')
count = 0
for [index, todo] in todos.items():
count += 1
line = unicode(todo.getTextLine()).encode('utf8')
todo_file.write(line + '\n')
todo_file.close()
def setData(self, data):
if 'todos' in data:
for index, todo in data['todos'].items():
data['todos'][index] = Todo(todo)
self.data = data
def getRawData(self):
return self.data
def getRawTodos(self, type = 'todo'):
text = self.getRawText(type)
return text.strip().split("\n")
def getRawText(self, file):
f = open(self.getLocation(file), 'r')
text = f.read()
f.close()
return text
def getLocation(self, file):
if file == 'todo':
location = self.todo_file.replace('$TODO_DIR', self.todo_dir)
elif file == 'todo_dir':
location = self.todo_dir
elif file == 'done':
location = self.done_file.replace('$TODO_DIR', self.todo_dir)
elif file == 'report':
location = self.report_file.replace('$TODO_DIR', self.todo_dir)
else:
raise Exception("This is not a location type: " + file)
return location
def getConfig(self,key):
return getattr(self, key)
def expandTodoDir(self):
if '~' in self.todo_dir:
self.todo_dir = self.todo_dir.replace('~', '/home/' + os.getlogin())
def setConfig(self, data):
for key, value in data.items():
setattr(self, key, value)
self.expandTodoDir()