-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
164 lines (136 loc) · 3.47 KB
/
app.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""
Amity Allocator
This system makes it easy to manage rooms and people at Amity.
Usage:
create_room <room_type> <room_name>
add_person <person_name> <person_description> [--wants_accommodation=N]
reallocate_person <person_identifier><new_room>
load_people <filename>
print_room <room_name>
print_allocations [--o=filename]
print_unallocated [--o=filename]
load_state [--dbname]
save_state [--o=db_name]
quit
(-i | --interactive)
Options:
-h --help Show this screen.
-i --interactive Interactive mode.
-v --version
"""
import cmd
from docopt import docopt, DocoptExit
from pyfiglet import figlet_format
from termcolor import cprint
from app.amity import Amity
def app_exec(func):
"""
Decorator definition for the app.
"""
def fn(self, arg):
try:
opt = docopt(fn.__doc__, arg)
except DocoptExit as e:
msg = "Invalid command! See help."
print(msg)
print(e)
return
except SystemExit:
return
return func(self, opt)
fn.__name__ = func.__name__
fn.__doc__ = func.__doc__
fn.__dict__.update(func.__dict__)
return fn
class AmityApp(cmd.Cmd):
intro = cprint(figlet_format("Amity Room!!!", font="cosmike"),\
"yellow")
prompt = "Amity --> "
amity = Amity()
@app_exec
def do_create_room(self, arg):
"""Creates a new room
Usage: create_room <room_name> <room_type>
"""
room_name = arg["<room_name>"]
room_type = arg["<room_type>"]
self.amity.create_room(room_name, room_type)
@app_exec
def do_add_person(self, arg):
"""
Adds a person and allocates rooms if available
Usage: add_person <person_name> <person_description> [--wants_accommodation=N]
"""
person_description= arg["<person_description>"]
wants_accommodation = arg["--wants_accommodation"]
if wants_accommodation is None:
wants_accommodation = "N"
person_name= arg["<person_name>"]
self.amity.add_person(person_name, person_description, wants_accommodation=wants_accommodation)
@app_exec
def do_print_room(self, arg):
"""
Prints all the people in a given rooms
Usage: print_room <room_name>
"""
self.amity.print_room(arg["<room_name>"])
@app_exec
def do_print_allocations(self, arg):
"""
Prints all offices and the people in them.
Usage: print_allocations [--o=filename]
"""
filename = arg["--o"] or ""
self.amity.print_allocations(filename)
@app_exec
def do_print_unallocated(self, arg):
"""
Prints all the people that don't have rooms
Usage: print_unallocated [--o=filename]
"""
filename = arg["--o"] or ""
self.amity.print_unallocated(filename)
@app_exec
def do_load_people(self, arg):
"""
Loads people from a text file to the app.
Usage: load_people <filename>
"""
self.amity.load_people(arg["<filename>"])
print("File loaded.")
@app_exec
def do_reallocate_person(self, arg):
"""
Reallocates person
Usage: reallocate_person <person_name> <new_room>
"""
person_name = arg["<person_name>"]
new_room = arg["<new_room>"]
self.amity.reallocate_person(person_name, new_room)
@app_exec
def do_load_state(self, arg):
"""
Loads data from the specified db into the app.
Usage: load_state <filename>
"""
self.amity.load_state(arg["<filename>"])
@app_exec
def do_save_state(self, arg):
"""
Persists app data into the given db
Usage: save_state [--db_name=sqlite_db]
"""
db = arg['--db_name']
if db:
self.amity.save_state(db)
else:
self.amity.save_state()
@app_exec
def do_quit(self, arg):
"""
Exits the app.
Usage: quit
"""
exit()
if __name__ == '__main__':
AmityApp().cmdloop()