/
anga.py
81 lines (63 loc) · 1.79 KB
/
anga.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
doc="""
Anga Weather App.
Usage:
anga fetch_weather <city>
anga help
anga exit
Examples:
anga fetch_weather Nairobi
"""
import cmd
from docopt import docopt, DocoptExit
from weather import fetch_weather
def doc_opt(f):
"""
This decorator is used to simplify the try/except block and pass the result
of the docopt parsing to the called action.
"""
def fn(self, arg):
"""
The DocoptExit is thrown when the args do not match.
We print a message to the user and the usage block.
The SystemExit exception prints the usage for --help
"""
try:
opt = docopt(fn.__doc__, arg)
except DocoptExit as e:
print('Invalid Command!')
print(e)
return
except SystemExit:
return
return f(self, opt)
fn.__name__ = f.__name__
fn.__doc__ = f.__doc__
fn.__dict__.update(f.__dict__)
return fn
class InteractiveWeather(cmd.Cmd):
intro = '\n' + ' ' \
+ '\n' + ' ' \
+ (' ' * 9 + '* ' * 10) + 'ANGA. GET TO KNOW THE WEATHER WITHOUT LEAVING THE HOUSE!!' + (' *' * 10) \
+ '\n' + ' ' \
+ '\n' + ' ' \
+ '\n' + ' ' \
+ '\n' + (' ' * 12 + ' ' * 23) + 'Type help for a list of commands' \
+ '\n' + ' ' \
+ '\n' + '- ' * 53
prompt = '(anga>>)'
@doc_opt
def do_fetch_weather(self, arg):
"""Usage: fetch_weather <city>"""
if arg['<city>']:
fetch_weather(arg['<city>'])
@doc_opt
def do_exit(self, arg):
"""Usage: exit"""
print ('Bye!')
exit()
if __name__ == '__main__':
try:
InteractiveWeather().cmdloop()
except KeyboardInterrupt:
print('Please try again')
exit()