-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
142 lines (122 loc) · 4.9 KB
/
main.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
#!/usr/bin/env python
"""The Dojo Rooms
Usage:
main.py create_room <room_type> <room_name>...
main.py add_person <first_name> <last_name> <person_type> [<wants_accommodation>]
main.py print_room <room_name>
main.py print_allocations [<filename>]
main.py print_unallocated [<filename>]
main.py reallocate_person <first_name> <last_name> <room_name>
main.py load_people <filename>
main.py save_state [--db=sqlite_database]
main.py load_state <sqlite_database>
main.py
main.py (-h | --help | --version)
Options:
-i, --interactive Interactive Mode
-h, --help Show this screen and exit.
--o=<filename> Filename to save output
--db=sqlite_database
"""
import sys
import cmd
from docopt import docopt, DocoptExit
from app.models.the_dojo_rooms import Dojo
dojo = Dojo()
# Citation: Source https://github.com/docopt/docopt/blob/master/examples/interactive_example.py
def docopt_cmd(func):
"""
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):
try:
opt = docopt(fn.__doc__, arg)
except DocoptExit as e:
# The DocoptExit is thrown when the args do not match.
# We print a message to the user and the usage block.
print('Invalid Command!')
print(e)
return
except SystemExit:
# The SystemExit exception prints the usage for --help
# We do not need to do the print here.
return
return func(self, opt)
fn.__name__ = func.__name__
fn.__doc__ = func.__doc__
fn.__dict__.update(func.__dict__)
return fn
# End of Citation
class TheDojoRooms(cmd.Cmd):
intro = '\n\tWelcome to The Dojo Rooms!\n' \
+ '\tProduct by Rwothoromo Elijah (www.github.com/Rwothoromo)\n' \
+ '\t(type help for a list of commands.)\n'
prompt = 'The Dojo Rooms: '
file = None
# create_room is used to create rooms in The Dojo Rooms
@docopt_cmd
def do_create_room(self, arg):
"""Usage: create_room <room_type> <room_name>..."""
room_type = arg["<room_type>"]
room_name = arg["<room_name>"]
dojo.create_room(room_type, room_name)
# add_person is used to add persons in The Dojo Rooms
@docopt_cmd
def do_add_person(self, arg):
"""Usage: add_person <first_name> <last_name> <person_type> [<wants_accommodation>]"""
person_name = str(arg["<first_name>"]) + ' ' + str(arg["<last_name>"])
person_type = arg["<person_type>"]
wants_accommodation = arg["<wants_accommodation>"]
dojo.add_person(person_name, person_type, wants_accommodation)
# print_room is used to display the people in a given room
@docopt_cmd
def do_print_room(self, arg):
"""Usage: print_room <room_name>"""
room_name = arg["<room_name>"]
dojo.print_room(room_name)
# print_allocations is used to print a list of allocations onto the screen.
@docopt_cmd
def do_print_allocations(self, arg):
"""Usage: print_allocations [<filename>]"""
filename = arg["<filename>"]
dojo.print_allocations(filename)
# print_unallocated is used to print a list of people without room allocations, onto the screen.
@docopt_cmd
def do_print_unallocated(self, arg):
"""Usage: print_unallocated [<filename>]"""
filename = arg["<filename>"]
dojo.print_unallocated(filename)
# reallocate_person is used to relocate a person from one room to another in The Dojo Rooms
@docopt_cmd
def do_reallocate_person(self, arg):
"""Usage: reallocate_person <first_name> <last_name> <room_name>"""
person_name = str(arg["<first_name>"]) + ' ' + str(arg["<last_name>"])
room_name = arg["<room_name>"]
dojo.reallocate_person(person_name, room_name)
# load_people is used to retrieve people from a text file and allocate them to rooms.
@docopt_cmd
def do_load_people(self, arg):
"""Usage: load_people <filename>"""
filename = arg["<filename>"]
dojo.load_people(filename)
# save_state persists all the data stored in the app to a SQLite database.
@docopt_cmd
def do_save_state(self, arg):
"""Usage: save_state [--db=sqlite_database]"""
db_name = arg['--db']
dojo.save_state(db_name)
# You can view database content from http://sqliteviewer.flowsoft7.com/
# load_state loads data from a database into the application.
@docopt_cmd
def do_load_state(self, arg):
"""Usage: load_state <sqlite_database>"""
db_name = arg["<sqlite_database>"]
dojo.load_state(db_name)
# You can view database content from http://sqliteviewer.flowsoft7.com/
def do_quit(self, arg):
"""Quits out of Interactive Mode."""
print('Thank you for stopping by!')
exit()
opt = docopt(__doc__, sys.argv[1:])
TheDojoRooms().cmdloop()