blob: 5d166541000f900f4892470540e3058e536c83fd (
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
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
|
#!/usr/bin/env python3
"""
Usage: todo_new [-f FILE] [-d DESC] [-D DATE] [-p PRIO] [-u URL] [-c CTX]...
Options:
-d --description DESC Description of the new todo entry
-p --priority PRIO Priority of the new todo entry
-D --date DATE Override the date field of the newtodo entry.
Default: today
-u --url URL Set a URL for the todo entry
-c --context CONTEXT Set the context for the todo entry. Can be
specified multiple times for multiple contexts
-f --file FILE Path to todo file.
Default: $XDG_DATA_HOME/todo.toml
-h --help Show this screen
-v --version Show version
"""
from datetime import date
from collections import OrderedDict
from os import getenv, path
from docopt import docopt
import toml
def sort_dict_by_int(d, rev):
'''Return dict sorted by int key'''
sorted_dict = [ (key, d[key]) for key in sorted(d, key=int, reverse=rev) ]
return OrderedDict(sorted_dict)
def find_free_id(d):
sorted_dict = sort_dict_by_int(d, False)
key, value = sorted_dict.popitem()
return int(key) + 1
class Args():
def __init__(self):
self.todo_file = None
self.description = None
self.priority = None
self.date = None
self.url = None
self.contexts = []
def parse_args(arguments):
args = Args()
if arguments['--file']:
args.todo_file = arguments['--file']
else:
data_home = getenv('XDG_DATA_HOME')
if data_home:
args.todo_file = data_home + '/todo.toml'
else:
home_dir = path.expanduser('~')
args.todo_file = home_dir + '/.local/share/todo.toml'
if arguments['--description']:
args.description = arguments['--description']
if arguments['--priority']:
args.priority = arguments['--priority']
if arguments['--date']:
args.date = arguments['--date']
else:
args.date = str(date.today())
if arguments['--url']:
args.url = arguments['--url']
for c in arguments['--context']:
args.contexts.append(c)
return args
def new_entry(entry_id, args):
id = str(entry_id)
entry = {id: {}}
if args.description:
entry[id].update({ 'description': args.description })
if args.date:
entry[id].update({ 'date': args.date })
if args.priority:
entry[id].update({ 'priority': args.priority })
if args.contexts:
entry[id].update({ 'context': args.contexts })
if args.url:
entry[id].update({ 'url': args.url })
return entry
def main():
arguments = docopt(__doc__, version='todo 0.0.1.alpha')
args = parse_args(arguments)
print(arguments)
todo_dict = toml.load(args.todo_file)
free_id = find_free_id(todo_dict)
entry = new_entry(free_id, args)
entry_toml = toml.dumps(entry)
with open(args.todo_file, 'a') as file:
file.write(entry_toml)
if __name__ == '__main__':
main()
|