forked from jonahar/google-reminders-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remind.py
133 lines (107 loc) · 4.16 KB
/
remind.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
import argparse
import readline # to enable navigating through entered text
from datetime import datetime
from typing import Union
import dateparser
from reminder import Reminder, gen_id
from reminders_client import RemindersClient
# format is "Fri, May 23 2019, 19:30"
DATE_FORMAT = '%a, %b %d %Y, %H:%M'
def read_yes_no(prompt) -> bool:
"""
read yes no answer from the user. default (empty answer) is yes
"""
ans = input(f'{prompt} [Y/n] ').lower()
return True if ans in ['', 'y', 'yes'] else False
def parse_time_str(time_str: str) -> Union[datetime, None]:
dt = dateparser.parse(time_str)
if dt is None:
print('Unrecognizable time text. See help menu for legal formats')
return None
return dt
def read_reminder_params() -> Union[Reminder, None]:
"""
read parameters from the user and build a Reminder object.
return a Reminder object, or None, meaning no action required (e.g. wrong
user parameters, or user aborted the action)
"""
title = input('What\'s the reminder: ')
dt = parse_time_str(input('When do you want to be reminded: '))
if dt is not None:
print(f'\n"{title}" on {dt.strftime(DATE_FORMAT)}\n')
save = read_yes_no('Do you want to save this?')
if save:
return Reminder(id=gen_id(), title=title, dt=dt)
def invoke_operation(args):
"""
inspect the program arguments and invoke the appropriate operation
"""
client = RemindersClient()
if args.interactive or args.create:
if args.interactive:
reminder = read_reminder_params()
else:
title, time_str = args.create
dt = parse_time_str(time_str)
if dt is None:
return
reminder = Reminder(id=gen_id(), title=title, dt=dt)
if reminder is not None:
if client.create_reminder(reminder):
print('Reminder set successfully:')
print(reminder)
elif args.get:
id = args.get
reminder = client.get_reminder(reminder_id=id)
if reminder is not None:
print(reminder)
elif args.delete:
id = args.delete
if client.delete_reminder(reminder_id=id):
print('Reminder deleted successfully')
elif args.list:
num_reminders = args.list
if num_reminders < 0:
print('argument to list command must be positive')
return
reminders = client.list_reminders(num_reminders=num_reminders)
if reminders is not None:
for r in sorted(reminders):
print(r)
else:
print('Wrong usage: no valid action was specified\n'
'please read help menu (-h) to see correct usage')
time_string_explain = '''
The TIME argument string can be in many formats such as
* "In 2 days at 14:56"
* "in 5 days at 9am"
* "Mar 6 at 7pm"
* "Sunday the 17th, 2:30pm"
* "tomorrow"
* "today at 19:00"
* "2019-05-25 10:42"
'''
def parse_args():
"""
parse and return the program arguments
"""
parser = argparse.ArgumentParser(description='Google reminders cli',
epilog=time_string_explain,
formatter_class=argparse.RawTextHelpFormatter)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-i', action='store_true', dest='interactive',
help='create a reminder by entering details interactively')
group.add_argument('-c', nargs=2, metavar=('TITLE', 'TIME'), dest='create',
help='create a reminder with the given title and time')
group.add_argument('-g', metavar='<id>', dest='get',
help='get reminder information by ID')
group.add_argument('-d', metavar='<id>', dest='delete',
help='delete reminder by ID')
group.add_argument('-l', type=int, metavar='N', dest='list',
help='list the last N created reminders, for a positive integer N')
return parser.parse_args()
def main():
args = parse_args()
invoke_operation(args)
if __name__ == '__main__':
main()