-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
112 lines (90 loc) · 3.12 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
"""
Usage:
create_room <type_room> <name>...
add_person <firstname> <surname> <person_type> [<wants_accomodation>]
print_room <name>
print_allocations [<filename>]
print_unallocations [<filename>]
dojo (-i | --interactive)
dojo (-h | --help | --version)
Options:
-i, --interactive Interactive Mode
-h, --help Show this screen and exit.
"""
from docopt import docopt, DocoptExit
import cmd
import os
import sys
from models.dojo import Dojo
from termcolor import colored
from insta import instance
from modules.ui import error,success
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
def intro():
os.system('cls' if os.name == 'nt' else 'clear')
print(__doc__)
class DOJO(cmd.Cmd):
prompt = colored('DOJO$$$', 'magenta', attrs=['blink','bold'])
@docopt_cmd
def do_create_room(self, arg):
"""Usage: create_room <type_room> <name>..."""
for room in arg['<name>']:
print(instance.create_room(room,arg['<type_room>']))
@docopt_cmd
def do_add_person(self, arg):
"""Usage: add_person <firstname> <surname> <person_type> [<wants_accomodation>]"""
print(instance.add_person(arg['<firstname>'],arg['<surname>'],arg['<person_type>'],arg['<wants_accomodation>']))
@docopt_cmd
def do_print_room(self, arg):
"""Usage: print_room <name>"""
room = instance.print_room(arg['<name>'])
if room == error('Room %s does not exist!'%(arg['<name>'])):
print(room)
else:
print(room)
print('``````````````````````````````````````````')
for person in room:
print(room[person])
@docopt_cmd
def do_print_allocations(self, arg):
"""Usage: print_allocations [<filename>]"""
instance.print_allocations(arg['<filename>'])
@docopt_cmd
def do_print_unallocations(self, arg):
"""Usage: print_allocations [<filename>]"""
unallocations = instance.print_unallocations(arg['<filename>'])
@docopt_cmd
def do_quit(self, arg):
"""Usage: quit"""
os.system('cls' if os.name == 'nt' else 'clear')
print ('Dojo Exiting')
exit()
if __name__ == "__main__":
try:
intro()
DOJO().cmdloop()
except KeyboardInterrupt:
os.system('cls' if os.name == 'nt' else 'clear')
print(error('DOJO EXITING'))