From c2d805ebf879c16533735578475c484cf8f1322c Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Wed, 17 Dec 2014 01:17:18 +0100 Subject: todo_new: Initial import --- todo_new | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100755 todo_new (limited to 'todo_new') diff --git a/todo_new b/todo_new new file mode 100755 index 0000000..5d16654 --- /dev/null +++ b/todo_new @@ -0,0 +1,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() -- cgit v1.2.3-54-g00ecf