michel-orgmode / michel / michel.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#!/usr/bin/env python
"""
michel-orgmode -- a script to push/pull an org-mode text file to/from a google
                  tasks list.

"""
from __future__ import with_statement
import gflags
import httplib2
from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run
from xdg.BaseDirectory import save_data_path #, save_config_path 
import argparse
import os.path
import sys
import re
import cPickle as pickle
import cStringIO
import diff3

class TasksTree(object):
    """
    Tree for holding tasks

    A TasksTree:
    - is a task (except the root, which just holds the list)
    - has subtasks
    - may have a task_id
    - may have a title
    """

    def __init__(self, title=None, task_id=None, task_notes=None, task_status=None):
        self.title = title
        self.task_id = task_id
        self.subtasks = []
        self.notes = task_notes
        # *status* usually takes on the value 'completed' or 'needsAction'
        self.status = task_status
        
    def __getitem__(self, key):
        return self.subtasks[key]
         
    def __setitem__(self, key, val):
        self.subtasks[key] = val
        
    def __delitem__(self, key):
        del(self.subtasks[key])
    
    def __len__(self):
        return len(self.subtasks)

    def get_task_with_id(self, task_id):
        """Returns the task of given id"""
        if self.task_id == task_id:
            return self
        else:
            # depth first search for id
            for subtask in self.subtasks:
                if subtask.get_task_with_id(task_id) is not None:
                    return subtask.get_task_with_id(task_id)
            # if there are no subtasks to search
            return None

    def add_subtask(self, title, task_id = None, parent_id = None,
            task_notes=None, task_status=None):
        """
        Adds a subtask to the tree
        - with the specified task_id
        - as a child of parent_id
        """
        if parent_id is None:
            self.subtasks.append(
                TasksTree(title, task_id, task_notes, task_status))
        else:
            if self.get_task_with_id(parent_id) is None:
                raise ValueError, "No element with suitable parent id"
            self.get_task_with_id(parent_id).add_subtask(title, task_id, None,
                    task_notes, task_status)
    
    def add_subtree(self, tree_to_add, include_root=False, root_title=None,
            root_notes=None):
        """Add *tree_to_add* as a subtree of this tree.
        
        If *include_root* is False, then the children of *tree_to_add* will be
        added as children of this tree's root node.  Otherwise, the root node
        of *tree_to_add* will be added as a child of this tree's root node.
        
        The *root_title* and *root_notes* arguments are optional, and can be
        used to set the title and notes of *tree_to_add*'s root node when
        *include_root* is True. 
        
        """
        if not include_root:
            self.subtasks.extend(tree_to_add.subtasks)
        else:
            if root_title is not None:
                tree_to_add.title = root_title
            if tree_to_add.title is None:
                tree_to_add.title = ""
                
            if root_notes is not None:
                tree_to_add.notes = root_notes
            
            self.subtasks.append(tree_to_add)
    
    def last(self, level):
        """Return the last task added at a given level of the tree.
        
        Level 0 is the "root" node of the tree, and there is only one node at
        this level, which contains all of the level 1 nodes (tasks/headlines).
        
        A TaskTree object is returned that corresponds to the last task having
        the specified level.  This TaskTree object will have the last task as
        the root node of the tree, and will maintain all of the node's
        descendants.
        
        """
        if level == 0:
            return self
        else:
            res = None
            for subtask in self.subtasks:
                x = subtask.last(level - 1)
                if x is not None:
                    res = x
            if res is not None:
                return res

    def push(self, service, list_id, parent = None, root=True):
        """Pushes the task tree to the given list"""
        # We do not want to push the root node
        if not root:
            insert_cmd_args = {'tasklist': list_id,
                               'body': {
                                           'title': self.title,
                                           'notes': self.notes,
                                           'status': self.status
                                       }
                              }
            if parent:
                insert_cmd_args['parent'] = parent
            res = service.tasks().insert(**insert_cmd_args).execute()
            self.task_id = res['id']
        # the API head inserts, so we insert in reverse.
        for subtask in reversed(self.subtasks):
            subtask.push(service, list_id, parent=self.task_id, root=False)

    def _lines(self, level):
        """Returns the sequence of lines of the string representation"""
        res = []
        for subtask in self.subtasks:
            #indentations = '\t' * level
            # add number of asterisks corresponding to depth of task, followed
            # by "DONE" if the task is marked as completed.
            done_string = ""
            if (subtask.status is not None) and (subtask.status == "completed"):
                done_string = " DONE"
            indentations = '*' * (level+1) + done_string + " "
            res.append(indentations + subtask.title)
            if subtask.notes is not None:
                notes = subtask.notes
                # add initial space to lines starting w/'*', so that it isn't treated as a task
                if notes.startswith("*"):
                    notes = " " + notes
                notes = notes.replace("\n*", "\n *")
                res.append(notes)
            subtasks_lines = subtask._lines(level + 1)
            res += subtasks_lines
        return res


    def __str__(self):
        """string representation of the tree.
        
        Only the root-node's children (and their descendents...) are printed,
        not the root-node itself.
        
        """
        # always add a trailing "\n" because text-files normally include a "\n"
        # at the end of the last line of the file.
        return '\n'.join(self._lines(0)) + "\n"

def database_read(key):
    """Fetch an object from the persistent database stored under *key*.
    
    Returns an object pulled out of the persistent database which was stored
    using *key*.  If no such key exists, None is returned.
    
    """
    db_path = os.path.join(save_data_path("michel"), "config_data.pkl")
    try:
        with open(db_path, "rb") as db_file:
            db_dict = pickle.load(db_file)
    except IOError: # typically because file doesn't exist
        db_dict = {}
    
    obj = db_dict.get(key)
    
    return obj

def database_write(key, obj):
    "Store an arbitrary object *obj* in the persistent database under *key*."
    db_path = os.path.join(save_data_path("michel"), "config_data.pkl")
    try:
        with open(db_path, "rb") as db_file:
            db_dict = pickle.load(db_file)
    except IOError: # typically because file doesn't exist
        db_dict = {}
    db_dict[key] = obj
    with open(db_path, "wb") as db_file:
        pickle.dump(db_dict, db_file, pickle.HIGHEST_PROTOCOL)

def database_delete(key):
    "Delete the object in the persistent database stored under *key*."
    db_path = os.path.join(save_data_path("michel"), "config_data.pkl")
    try:
        with open(db_path, "rb") as db_file:
            db_dict = pickle.load(db_file)
    except AttributeError: # typically because file doesn't exist
        db_dict = {}
    
    try:
        del(db_dict[key])
        with open(db_path, "wb") as db_file:
            pickle.dump(db_dict, db_file, pickle.HIGHEST_PROTOCOL)
    except KeyError:
        pass
    
def concatenate_trees(t1, t2):
    """Combine tree *t1*'s children with tree *t2*'s children.
    
    A tree is returned whose children include the children of *t1* and the
    children of *t2*.  The root node of the returned tree is a dummy node
    having no title.
    
    Note: children are treated as references, so modifying *t1* after creating
    the combined tree will also modify the combined tree.
    
    """
    joined_tree = TasksTree()
    joined_tree.add_subtree(t1)
    joined_tree.add_subtree(t2)
    
    return joined_tree

def treemerge(new_tree, old_tree, other_tree):
    old = str(old_tree)
    other = str(other_tree)
    new = str(new_tree)
    merged_text, conflict_occurred = diff3.merge3_text(new, old, other)
    
    merged_tree = parse_text(merged_text)
    
    return merged_tree, conflict_occurred
 
def get_service(profile_name=None):
    """
    Handle oauth's shit (copy-pasta from
    http://code.google.com/apis/tasks/v1/using.html)
    Yes I do publish a secret key here, apparently it is normal
    http://stackoverflow.com/questions/7274554/why-google-native-oauth2-flow-require-client-secret
    """
    if profile_name is None:
        profile_name = '__default'
    FLAGS = gflags.FLAGS
    FLOW = OAuth2WebServerFlow(
            client_id='617841371351.apps.googleusercontent.com',
            client_secret='_HVmphe0rqwxqSR8523M6g_g',
            scope='https://www.googleapis.com/auth/tasks',
            user_agent='michel/0.0.1')
    FLAGS.auth_local_webserver = False
    auth_filename = profile_name + "_oauth.dat"
    storage = Storage(os.path.join(save_data_path("michel"), auth_filename))
    credentials = storage.get()
    if credentials is None or credentials.invalid == True:
        credentials = run(FLOW, storage)
    http = httplib2.Http()
    http = credentials.authorize(http)
    return build(serviceName='tasks', version='v1', http=http)

def get_list_id(service, list_name=None):
    if list_name is None:
        list_id = "@default"
    else:
        # look up id by list name
        tasklists = service.tasklists().list().execute()
        for tasklist in tasklists['items']:
            if tasklist['title'] == list_name:
                list_id = tasklist['id']
                break
        else:
            # no list with the given name was found
            print '\nERROR: No google task-list named "%s"\n' % list_name
            sys.exit(2)

    return list_id

def get_gtask_list_as_tasktree(list_name=None, profile=None):
    """Get a TaskTree object representing a google tasks list.
    
    The Google Tasks list named *list_name* is retrieved, and converted into a
    TaskTree object which is returned.  If *list_name* is not specified, then
    the default Google-Tasks list will be used.
    
    """
    service = get_service(profile)
    list_id = get_list_id(service, list_name)
    tasks = service.tasks().list(tasklist=list_id).execute()
    tasks_tree = TasksTree()
    tasklist = [t for t in tasks.get('items', [])]
    fail_count = 0
    while tasklist != [] and fail_count < 1000 :
        t = tasklist.pop(0)
        try:
            tasks_tree.add_subtask(t['title'].encode('utf-8'), t['id'],
                    t.get('parent'), t.get('notes'), t.get('status'))
        except ValueError:
            fail_count += 1
            tasklist.append(t)
 
    return tasks_tree

def print_todolist(list_name=None, profile=None):
    """Print an orgmode-formatted string representing a google tasks list.
    
    The Google Tasks list named *list_name* is used.  If *list_name* is not
    specified, then the default Google-Tasks list will be used.
    
    """
    tasks_tree = get_gtask_list_as_tasktree(list_name, profile)
    print(tasks_tree)
    
def write_todolist(orgfile_path, list_name=None, profile=None):
    """Create an orgmode-formatted file representing a google tasks list.
    
    The Google Tasks list named *list_name* is used.  If *list_name* is not
    specified, then the default Google-Tasks list will be used.
    
    """
    tasks_tree = get_gtask_list_as_tasktree(list_name, profile)
    f = open(orgfile_path, 'wb')
    f.write(str(tasks_tree))
    f.close()

def erase_todolist(list_id, profile=None):
    """Erases the todo list of given id"""
    service = get_service(profile)
    tasks = service.tasks().list(tasklist=list_id).execute()
    for task in tasks.get('items', []):
        service.tasks().delete(tasklist=list_id,
                task=task['id']).execute()


def parse_path(path):
    """Parses an org-mode file and returns a tree"""
    file_lines = open(path, "r").readlines()
    file_text = "".join(file_lines)
    return parse_text(file_text)
    
def parse_text(text):
    """Parses an org-mode formatted block of text and returns a tree"""
    # create a (read-only) file object containing *text*
    f = cStringIO.StringIO(text)
    
    headline_regex = re.compile("^(\*+ )( *)(DONE )?")
    tasks_tree = TasksTree()
    
    indent_level = 0
    last_indent_level = 0
    seen_first_task = False
    task_notes = None
    task_title = None
    for n, line in enumerate(f):
        matches = headline_regex.findall(line)
        line = line.rstrip("\n")
        try:
            # assign task_depth; root depth starts at 0
            num_asterisks_and_space = len(matches[0][0])
            
            # if we get to this point, then it means that a new task is
            # starting on this line -- we need to add the last-parsed task
            # to the tree (if this isn't the first task encountered)
            
            if seen_first_task:
                # add the task to the tree
                tasks_tree.last(indent_level).add_subtask(
                        title=task_title,
                        task_notes=task_notes,
                        task_status=task_status)
            else:
                if task_notes is not None:
                    # this means there was some text at the beginning of the
                    # file before any headline was encountered.  We create a
                    # dummy headline to contain this text.
                    tasks_tree.last(0).add_subtask(
                            title="",
                            task_notes=task_notes,
                            task_status=None)
                # this is the first task, so skip adding a last-task to the
                # tree, and record that we've encountered our first task
                seen_first_task = True
            
            indent_level = num_asterisks_and_space - 2
            
            # strip off asterisks-and-space prefix
            line = line[num_asterisks_and_space:]
            
            if matches[0][2] == 'DONE ':
                task_status = 'completed'
                # number of spaces preceeding 'DONE' and after
                # asterisks+single-space
                num_extra_spaces = len(matches[0][1])
                # remove the '[ ...]DONE ' from the line
                line = line[num_extra_spaces + len('DONE '):]
            else:
                task_status = 'needsAction'
            
            task_title = line
            task_notes = None
        except IndexError:
            # this is not a task, but a task-notes line
            if task_notes is None:
                task_notes = line
            else:
                task_notes += "\n" + line
        
        assert indent_level <= last_indent_level + 1, ("line %d: "
                "subtask has no parent task" % n)
        last_indent_level = indent_level
    # END: for loop
    
    # add the last task to the tree, since the for loop won't be iterated
    # again after the last line of the file (tasks are added at beginning
    # of the for loop)
    if task_title is not None:
        tasks_tree.last(indent_level).add_subtask(
                title=task_title,
                task_notes=task_notes,
                task_status=task_status)
    else: # there are no headlines in the org-file; create a dummy headline
        tasks_tree.last(0).add_subtask(
                title="",
                task_notes=task_notes,
                task_status=None)

    f.close()
    return tasks_tree

def push_todolist(path, list_name, profile=None):
    """Pushes the specified file to the specified todolist"""
    service = get_service(profile)
    list_id = get_list_id(service, list_name)
    tasks_tree = parse_path(path)
    erase_todolist(list_id, profile)
    tasks_tree.push(service, list_id)

def store_current_tree(tree, listname):
    "Store the current tree persistently for later use"
    if listname is None:
        listname = "_DEFAULT_"
    database_write(listname+"_tree", tree)

def get_last_tree(listname):
    if listname is None:
        listname = "_DEFAULT_"
    tree = database_read(listname+"_tree")
    return tree

def sync_todolist(path, list_name, profile=None):
    """Synchronizes the specified file with the specified todolist"""
    gtasks_tree = get_gtask_list_as_tasktree(list_name, profile)
    orgfile_tree = parse_path(path)
    orig_tree = get_last_tree(list_name)
    if orig_tree is None:
        # by default use the gtasks tree if no original tree is available
        orig_tree = gtasks_tree
    
    merged_tree, conflict_occurred = treemerge(orgfile_tree, orig_tree, gtasks_tree)
    
    if conflict_occurred:
        conflicted_filename = path + ".conflicted"
        open(conflicted_filename, "wb").write(str(merged_tree))
        print "\nWARNING:  Org-file and task-list could not be cleanly merged:  " \
              "the attempted merge can be found in '%s'.  Please " \
              "modify this file, copy it to '%s', and push '%s' back " \
              "to the desired GTasks list.\n" % (conflicted_filename, path, path)
        sys.exit(2)
    else:
        # store the successfully merged tree locally so we can use it as the
        # original/base tree in future 3-way merges.
        # TODO: do this also when pushing/pulling?
        store_current_tree(merged_tree, list_name)
        
        # write merged tree to tasklist
        service = get_service(profile)
        list_id = get_list_id(service, list_name)
        erase_todolist(list_id, profile)
        merged_tree.push(service, list_id)
        
        # write merged tree to orgfile
        f = open(path, 'wb')
        f.write(str(merged_tree))
        f.close()


def main():
    parser = argparse.ArgumentParser(description="Synchronize org-mode text" 
                                           "files with a google-tasks list.")
    
    action = parser.add_mutually_exclusive_group(required=True)
    action.add_argument("--push", action='store_true',
            help='replace LISTNAME with the contents of FILE.')
    action.add_argument("--pull", action='store_true',
            help='replace FILE with the contents of LISTNAME.')
    action.add_argument("--sync", action='store_true',
            help='synchronize changes between FILE and LISTNAME.')
    
    parser.add_argument('--orgfile',
            metavar='FILE',
            help='An org-mode file to push from / pull to')
    # TODO: replace the --profile flag with a URL-like scheme for specifying
    # data sources. (e.g. using file:///path/to/orgfile or
    # gtasks://profile/listname, and having only --from and --to flags)
    parser.add_argument('--profile',
            required=False,
            help='A user-defined name profile name to distinguish between '
                 'different google accounts')
    parser.add_argument('--listname',
            help='A GTasks list to pull from / push to (default list if empty)')
    
    args = parser.parse_args()
    
    if args.push and not args.orgfile:
        parser.error('--orgfile must be specified when using --push')
    if args.sync and not args.orgfile:
        parser.error('--orgfile must be specified when using --sync')
    
    if args.pull:
        if args.orgfile is None:
            print_todolist(args.listname, args.profile)
        else:
            write_todolist(args.orgfile, args.listname, args.profile)
    elif args.push:
        if not os.path.exists(args.orgfile):
            print("The org-file you want to push does not exist.")
            sys.exit(2)
        push_todolist(args.orgfile, args.listname, args.profile)
    elif args.sync:
        if not os.path.exists(args.orgfile):
            print("The org-file you want to synchronize does not exist.")
            sys.exit(2)
        sync_todolist(args.orgfile, args.listname, args.profile)

if __name__ == "__main__":
    main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.