-
Notifications
You must be signed in to change notification settings - Fork 1
/
console.py
executable file
·242 lines (208 loc) · 6.8 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/python3
"""contains the entry point of the command interpreter
"""
import cmd
import re
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.amenity import Amenity
from models.place import Place
from models.review import Review
class HBNBCommand(cmd.Cmd):
"""entry point of the command interpreter
"""
prompt = "(hbnb) "
airbnb_engine_classes = {
"User": User,
"BaseModel": BaseModel,
"Place": Place,
"State": State,
"City": City,
"Amenity": Amenity,
"Review": Review
}
def emptyline(self):
"""Handles empty line
"""
pass
def do_EOF(self, arg):
"""exit the program
"""
return True
def do_quit(self, arg):
"""exit the program
"""
return True
def do_create(self, arg):
"""Creates a new instance of BaseModel,
saves it (to the JSON file)
and prints the id
Ex: $ create BaseModel
"""
if not arg:
print("** class name missing **")
return
try:
args = arg.split()
base_model = self.airbnb_engine_classes[args[0]]()
base_model.save()
print(base_model.id)
except KeyError as ex:
print("** class doesn't exist **")
return
def do_show(self, arg):
"""
Prints the string representation
of an instance based on the class name and id
$ show BaseModel 1234-1234-1234
"""
if not arg:
print("** class name missing **")
return
try:
class_name, id = arg.split()
if class_name in self.airbnb_engine_classes:
key = class_name + "." + id
objects = storage.all()
if key not in objects:
print("** no instance found **")
return
else:
print(objects[key])
return
else:
print("** class doesn't exist ***")
return
except ValueError as ex:
print("** instance id missing **")
return
def do_destroy(self, arg):
"""
Deletes an instance based on the class name
and id (save the change into the JSON file)
Ex: $ destroy BaseModel 1234-1234-1234
"""
if not arg:
print("** class name missing **")
return
try:
class_name, id = arg.split()
if class_name in self.airbnb_engine_classes:
key = class_name + "." + id
objects = storage.all()
if key not in objects:
print("** no instance found **")
return
else:
del objects[key]
storage.save()
return
else:
print("** class doesn't exist **")
return
except ValueError as ex:
print("** instance id missing **")
return
def do_all(self, arg):
"""
Prints all string representation of all
instances based or not on the class name
$ all BaseModel or $ all.
"""
value_list = []
objects = storage.all()
if not arg:
for value in objects.values():
value_list.append(value.__str__())
print(value_list)
return
if arg in self.airbnb_engine_classes:
for value in objects.values():
if arg == type(value).__name__:
value_list.append(value.__str__())
print(value_list)
return
else:
print("** class doesn't exist **")
return
def do_update(self, arg):
"""
Updates an instance based on the class
name and id by adding or updating attribute
(save the change into the JSON file)
Ex: $ update BaseModel 1234-1234-1234
email "[email protected]".
"""
arg_list = arg.split()
length = len(arg_list)
if length == 0:
print("** class name missing **")
return
base_model = arg_list[0]
if base_model not in self.airbnb_engine_classes:
print("** class doesn't exist **")
return
if length == 1:
print("** instance id missing **")
return
objects = storage.all()
if length > 1:
obj_key = arg_list[0] + '.' + arg_list[1]
if obj_key not in objects:
print("** no instance found **")
return
if length == 2:
print("** attribute name missing **")
return
if length == 3:
print("** value missing **")
return
class_instance = objects[obj_key]
attribute, value = arg_list[2], arg_list[3]
setattr(class_instance, attribute, value)
class_instance.save()
def default(self, arg):
"""Handles defaults arguments not created
"""
count = 0
args = arg.split(".")
if args[0] in self.airbnb_engine_classes and args[1] == "all()":
self.do_all(args[0])
elif args[0] in self.airbnb_engine_classes and args[1] == "count()":
if (args[0] not in self.airbnb_engine_classes):
print("** class doesn't exist **")
return (False)
else:
for key in storage.all():
if key.startswith(args[0]):
count += 1
print(count)
elif args[0] in self.airbnb_engine_classes and \
args[1].startswith('show'):
arg = args[1].split('"')
if len(arg) == 3:
arg1 = args[0] + " " + arg[1]
self.do_show(arg1)
elif args[0] in self.airbnb_engine_classes and \
args[1].startswith('destroy'):
arg = args[1].split('"')
if len(arg) == 3:
arg1 = args[0] + " " + arg[1]
self.do_destroy(arg1)
elif args[0] in self.airbnb_engine_classes and \
args[1].startswith('update'):
start = 'update('
end = ')'
arg = re.findall(re.escape(start)+"(.*)" +
re.escape(end), args[1])[0]
arg = arg.replace('(', '').replace(')', '').replace(',', '')
arg = arg.replace('"', '')
arg1 = args[0] + " " + arg
self.do_update(arg1)
else:
print("*** Unknown syntax: {}".format(arg))
if __name__ == '__main__':
HBNBCommand().cmdloop()