-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.py
executable file
·187 lines (164 loc) · 6.25 KB
/
console.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/python3
"""Defines the MyConsole console."""
import cmd
import re
from shlex import split
from models import storage
from models.base_model import BaseModel
from models.user import User
from models.state import State
from models.city import City
from models.place import Place
from models.amenity import Amenity
from models.review import Review
def parse(input_string):
"Parsing input"
curly_braces = re.search(r"\{(.*?)\}", input_string)
brackets = re.search(r"\[(.*?)\]", input_string)
if curly_braces is None:
if brackets is None:
return [item.strip(",") for item in split(input_string)]
else:
lexer = split(input_string[:brackets.span()[0]])
ret_list = [item.strip(",") for item in lexer]
ret_list.append(brackets.group())
return ret_list
else:
lexer = split(input_string[:curly_braces.span()[0]])
ret_list = [item.strip(",") for item in lexer]
ret_list.append(curly_braces.group())
return ret_list
class HBNBCommand(cmd.Cmd):
"""Defines the MyConsole command"""
prompt = "(hbnb) "
__myclasses = {
"BaseModel",
"User",
"State",
"City",
"Place",
"Amenity",
"Review"
}
def emptyline(self):
"""Do nothing upon receiving an empty line."""
pass
def default(self, input_string):
"""Default behavior for cmd module when input is invalid"""
command_dict = {
"all": self.do_all,
"show": self.do_show,
"destroy": self.do_destroy,
"count": self.do_count,
"update": self.do_update
}
match = re.search(r"\.", input_string)
if match is not None:
input_list = [input_string[:match.span()[0]], input_string[match.span()[1]:]]
match = re.search(r"\((.*?)\)", input_list[1])
if match is not None:
command = [input_list[1][:match.span()[0]], match.group()[1:-1]]
if command[0] in command_dict.keys():
call = "{} {}".format(input_list[0], command[1])
return command_dict[command[0]](call)
print("*** Unknown syntax: {}".format(input_string))
return False
def do_quit(self, input_string):
"""Quit command to exit the program."""
return True
def do_EOF(self, input_string):
"""EOF signal to exit the program."""
print("")
return True
def do_create(self, input_string):
"""Usage: create"""
input_list = parse(input_string)
if len(input_list) == 0:
print("** class name missing **")
elif input_list[0] not in HBNBCommand.__myclasses:
print("** class doesn't exist **")
else:
print(eval(input_list[0])().id)
storage.save()
def do_show(self, input_string):
"""Usage"""
input_list = parse(input_string)
obj_dict = storage.all()
if len(input_list) == 0:
print("** class name missing **")
elif input_list[0] not in HBNBCommand.__myclasses:
print("** class doesn't exist **")
elif len(input_list) == 1:
print("** instance id missing **")
elif "{}.{}".format(input_list[0], input_list[1]) not in obj_dict:
print("** no instance found **")
else:
print(obj_dict["{}.{}".format(input_list[0], input_list[1])])
def do_destroy(self, input_string):
"""Usage: destroy """
input_list = parse(input_string)
obj_dict = storage.all()
if len(input_list) == 0:
print("** class name missing **")
elif input_list[0] not in HBNBCommand.__myclasses:
print("** class doesn't exist **")
elif len(input_list) == 1:
print("** instance id missing **")
elif "{}.{}".format(input_list[0], input_list[1]) not in obj_dict.keys():
print("** no instance found **")
else:
del obj_dict["{}.{}".format(input_list[0], input_list[1])]
storage.save()
def do_all(self, input_string):
"""Usage: all or all <class> or <class>.all()"""
input_list = parse(input_string)
if len(input_list) > 0 and input_list[0] not in HBNBCommand.__myclasses:
print("** class doesn't exist **")
else:
obj_list = []
for obj in storage.all().values():
if len(input_list) > 0 and input_list[0] == obj.__class__.__name__:
obj_list.append(obj.__str__())
elif len(input_list) == 0:
obj_list.append(obj.__str__())
print(obj_list)
def do_count(self, input_string):
"""Usage: count <class> or <class>.count()"""
input_list = parse(input_string)
count = 0
for obj in storage.all().values():
if input_list[0] == obj.__class__.__name__:
count += 1
print(count)
def do_update(self, input_string):
"""Usage: update <class> <id> <attribute_name> <attribute>"""
input_list = parse(input_string)
obj_dict = storage.all()
if len(input_list) == 0:
print("** class name missing **")
return False
if input_list[0] not in HBNBCommand.__myclasses:
print("** class doesn't exist **")
return False
if len(input_list) == 1:
print("** instance id missing **")
return False
if "{}.{}".format(input_list[0], input_list[1]) not in obj_dict.keys():
print("** no instance found **")
return False
if len(input_list) == 2:
print("** attribute name missing **")
return False
if len(input_list) == 3:
print("** value missing **")
return False
obj_key = "{}.{}".format(input_list[0], input_list[1])
obj = obj_dict[obj_key]
if input_list[2] in obj.__class__.__dict__.keys():
val_type = type(obj.__class__.__dict__[input_list[2]])
obj.__dict__[input_list[2]] = val_type(input_list[3])
else:
obj.__dict__[input_list[2]] = input_list[3]
storage.save()
if __name__ == "__main__":
HBNBCommand().cmdloop()