-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathapp.py
More file actions
115 lines (91 loc) · 3.02 KB
/
app.py
File metadata and controls
115 lines (91 loc) · 3.02 KB
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
from __future__ import unicode_literals
import sys
from prompt_toolkit import PromptSession
from prompt_toolkit.history import InMemoryHistory
from prompt_toolkit.completion import WordCompleter
from peewee import *
import datetime
import click
command_completer = WordCompleter(['add', 'show'], ignore_case=True)
db = SqliteDatabase('to_do_list.db')
class ToDo(Model):
task = CharField(max_length=255)
description = CharField(max_length=255)
timestamp = DateTimeField(default=datetime.datetime.now)
mins = IntegerField()
done = BooleanField(default=True)
class Meta:
database = db
def initialize():
"""Connect to database, create tables if they don't exist"""
db.connect()
db.create_tables([ToDo], safe=True)
def parse(input):
"""
a b 10 first blog post
a c 10 finished cli
a p 120
"""
input = input.strip()
cmd, task, mins, description, done = ['']*5
try:
cmd, task, mins, *description, done = input.split()
description = ' '.join(description)
if done == 'False':
done = False
return cmd, task, mins, description, done
except Exception as e:
print('this is continuing to fail and we do not know why')
print(e)
return input, task, mins, description, done
def add(**kwargs):
try:
ToDo.create(task=kwargs['task'],
mins=kwargs['mins'],
description=kwargs['description'],
done=kwargs['done'])
except Exception:
print("Please check your ego")
def show(**kwargs):
total = {}
for t in ToDo.select():
print(t.task, t.description, t.mins, t.done)
if t.task in total:
total[t.task]+=t.mins
else:
total[t.task]=t.mins
for k,v in total.items():
print('{} minutes spent {}'.format(v,k))
def execute(**kwargs):
cmds = {
'add': add,
'show': show
}
cmds.get(kwargs['cmd'])(**kwargs)
@click.command()
@click.option('--interactive', '-i', help='needs some help text', is_flag=True, default=False)
@click.option('--show', '-s', help='needs some help text', is_flag=True, default=False)
@click.option('--add', '-a', nargs=4, type=(click.STRING, int, click.STRING, bool), default=(None, None, None, False))
def main(interactive, add, show):
initialize()
if interactive:
history = InMemoryHistory()
session = PromptSession()
while True:
try:
text = session.prompt('% ')
except KeyboardInterrupt:
continue
except EOFError:
break
else:
cmd, task, mins, description, done = parse(text)
execute(cmd=cmd, task=task, mins=mins, description=description, done=done)
elif show:
execute(cmd='show')
else:
task, mins, description = add
execute(cmd='add', task=task, mins=mins, description=description, done=done)
print('GoodBye!')
if __name__ == '__main__':
main()