aboutsummaryrefslogtreecommitdiffstats
path: root/todo
blob: a4c57f182a88682e26cf9817568da70d7f395534 (plain)
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
#!/usr/bin/env python3
"""
Usage: todo [--id | --date | --priority] [--reverse] [--file <file>]

Options:
  -i --id        Sort by ID
  -d --date      Sort by date
  -p --priority  Sort by priority
  -r --reverse   Reverse sort order
  -f --file      Path to todo file
  -h --help      Show this screen
  -v --version   Show version
"""

from sys import exit
from collections import OrderedDict
from docopt import docopt
import pytoml


def sort_toml(toml_dict, sort_key, rev=False):
    return sorted(
            list(toml_dict.items()),
            key=lambda d: (sort_key not in d[1], d[1].get(sort_key, None)),
            reverse=rev)

def main():
    arguments = docopt(__doc__, version='todo 0.0.1.alpha')
    print(arguments)
    if arguments['--date']:
        sort_key = 'date'
    elif arguments['--priority']:
        sort_key = 'priority'
    else:
        sort_key = 'priority'
    rev = arguments['--reverse']

    with open("/home/kyrias/documents/notes/TODO.toml", "rt") as in_file:
        tasks = in_file.read()

    unsorted = OrderedDict(sorted(pytoml.loads(tasks).items()))
    sorted_toml = OrderedDict(sort_toml(unsorted, sort_key, rev))

    print(sorted_toml)

    for t_id in sorted_toml:
        task = sorted_toml[t_id]
        output = "{}. ".format(t_id)
        if 'priority' in task:
            output += "({}) ".format(task['priority'])
        if 'date' in task:
            output += "{}".format(task['date'])
        if 'description' in task:
            output += "\n    {}".format(task['description'])
        if 'url' in task:
            output += "\n    URL: {}".format(task['url'])
        if 'context' in task:
            output += "\n    Contexts: {}".format(task['context'])

        print(output)

if __name__ == '__main__':
    main()