forked from Fayth7/AirBnB_clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.py
executable file
·363 lines (337 loc) · 12.9 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/python3
"""Defines the HBnB console."""
import cmd
from models.engine.file_storage import FileStorage
from models.base_model import BaseModel
from models.amenity import Amenity
from models.city import City
from models.place import Place
from models.review import Review
from models.state import State
from models.user import User
import json
from shlex import split
class HBNBCommand(cmd.Cmd):
"""Command interpreter class for HBNB"""
prompt = "(hbnb) "
classes = {
"BaseModel", "Amenity", "City", "Place", "Review", "State", "User"
}
def parseline(self, line):
"""Override the default parseline() method to handle <class name>.all() command"""
line = line.strip()
if line.startswith("."):
line = line[1:]
if line.startswith("<") and line.endswith(">"):
cmd, arg = line[1:-1], ""
else:
cmd, _, arg = line.partition(" ")
return cmd, arg, line
def emptyline(self):
"""Do nothing when an empty line is entered"""
pass
# def do_emptyline(self):
# """Do nothing when an empty line is entered"""
# return True
def do_quit(self, arg):
"""Exit the program"""
return True
def do_EOF(self, arg):
"""Exit the program"""
return True
def do_create(self, arg):
"""Creates a new instance, saves it to the JSON file, and prints the id"""
if len(arg) == 0:
print("** class name missing **")
elif arg not in self.classes:
print("** class doesn't exist **")
else:
new_instance = eval(arg)()
new_instance.save()
print(new_instance.id)
def do_show(self, arg):
"""Prints the string representation of an instance based on the class name and id"""
from models import storage
args = arg.split()
if len(args) == 0:
print("** class name missing **")
elif args[0] not in self.classes:
print("** class doesn't exist **")
elif len(args) == 1:
print("** instance id missing **")
else:
key = "{}.{}".format(args[0], args[1])
objects = storage.all()
if key in objects:
print(objects[key])
else:
print("** no instance found **")
def do_destroy(self, arg):
"""Deletes an instance based on the class name and id"""
from models import storage
args = arg.split()
if len(args) == 0:
print("** class name missing **")
elif args[0] not in self.classes:
print("** class doesn't exist **")
elif len(args) == 1:
print("** instance id missing **")
else:
key = "{}.{}".format(args[0], args[1])
objects = storage.all()
if key in objects:
objects.pop(key)
storage.save()
else:
print("** no instance found **")
def do_all(self, arg):
"""Prints all string representations of instances"""
from models import storage
arg_list = split(arg)
objects = storage.all().values()
if not arg_list:
# In case no arguments are passed
print([str(obj) for obj in objects])
else:
if arg_list[0] not in self.classes:
# In Case a wrong class is passed
print("** class doesn't exist **")
else:
# In case a right class is passed
print([str(obj) for obj in objects
if arg_list[0] in str(obj)])
# from models import storage
# args = arg.split()
# if len(args) == 0:
# objects = storage.all()
# print([str(obj) for obj in objects.values()])
# elif args[0] not in self.classes:
# print("** class doesn't exist **")
# else:
# # print([str(obj)
# # for obj in objects.values() if isinstance(obj, eval(arg))])
# class_name = args[0]
# objects = storage.all(class_name)
# print([str(obj) for obj in objects])
def do_update(self, arg):
"""Updates an instance based on the class name and id"""
from models import storage
args = arg.split()
if len(args) == 0:
print("** class name missing **")
elif args[0] not in self.classes:
print("** class doesn't exist **")
elif len(args) == 1:
print("** instance id missing **")
else:
key = "{}.{}".format(args[0], args[1])
objects = storage.all()
if key not in objects:
print("** no instance found **")
elif len(args) == 2:
print("** attribute name missing **")
elif len(args) == 3:
print("** value missing **")
else:
instance = objects[key]
attribute = args[2]
value = args[3].strip("\"'")
if hasattr(instance, attribute):
attr_type = type(getattr(instance, attribute))
setattr(instance, attribute, attr_type(value))
instance.save()
else:
print("** attribute doesn't exist **")
def default(self, line):
"""Handle the <class name>.all() command"""
class_name, _, command = line.partition(".")
if class_name in self.classes:
if command == "all()":
self.do_all(class_name)
elif command == "count()":
self.do_count(class_name)
elif command.endswith("\")"):
func, _, clas_id = command.partition("(\"")
id = clas_id[:-2]
new_command = f"{class_name} {id}"
if func == "show":
self.do_show(new_command)
elif func == "destroy":
self.do_destroy(new_command)
elif command.startswith("update"):
# User.update("4726cdcc-50e2-48b0-aebc-9fd403a36d8e", "first_name", "John")
# update User 4726cdcc-50e2-48b0-aebc-9fd403a36d8e first_name "Emma"
func, _, others = command.partition("(")
args = others.split(", ")
id = args[0].strip("\"")
name = args[1]
value = args[2].strip(")")
stripped_command = f"{class_name} {id} {name} {value}"
self.do_update_id(stripped_command)
else:
print("** no instance found **")
else:
print("*** Unknown syntax: {}".format(line))
# def do_help(self, arg):
# """Display help information"""
# if arg == "":
# print("Documented commands (type help <topic>):")
# print("========================================")
# print("EOF all create destroy help quit show update")
# else:
# super().do_help(arg)
def do_count(self, arg):
"""Counts the number of instances of a class"""
from models import storage
args = arg.split()
if len(args) == 0:
print("** class name missing **")
elif args[0] not in self.classes:
print("** class doesn't exist **")
else:
class_name = args[0]
objects = storage.all()
count = sum(1 for obj in objects.values()
if type(obj).__name__ == class_name)
print(count)
def do_show_id(self, arg):
"""Show an instance based on its ID"""
from models import storage
args = arg.split()
if len(args) == 0:
print("** class name missing **")
elif args[0] not in self.classes:
print("** class doesn't exist **")
elif len(args) == 1:
print("** instance ID missing **")
else:
class_name = args[0]
instance_id = args[1].strip("\"'")
objects = storage.all()
key = "{}.{}".format(class_name, instance_id)
if key in objects:
print(objects[key])
else:
print("** no instance found **")
def do_destroy_id(self, arg):
"""Destroy an instance based on its ID"""
from models import storage
args = arg.split()
if len(args) == 0:
print("** class name missing **")
elif args[0] not in self.classes:
print("** class doesn't exist **")
elif len(args) == 1:
print("** instance ID missing **")
else:
class_name = args[0]
instance_id = args[1].strip("\"'")
objects = storage.all()
key = "{}.{}".format(class_name, instance_id)
if key in objects:
objects.pop(key)
storage.save()
else:
print("** no instance found **")
def do_update_id(self, arg):
"""Update an instance based on its ID"""
from models import storage
args = arg.split()
if len(args) == 0:
print("** class name missing **")
elif args[0] not in self.classes:
print("** class doesn't exist **")
elif len(args) == 1:
print("** instance ID missing **")
elif len(args) == 2:
print("** attribute name missing **")
elif len(args) == 3:
print("** value missing **")
else:
class_name = args[0]
instance_id = args[1].strip("\"'")
attribute_name = args[2]
attribute_value = args[3].strip("\"'")
objects = storage.all()
key = "{}.{}".format(class_name, instance_id)
if key in objects:
instance = objects[key]
if hasattr(instance, attribute_name):
attr_type = type(getattr(instance, attribute_name))
setattr(instance, attribute_name,
attr_type(attribute_value))
instance.save()
else:
print("** attribute doesn't exist **")
else:
print("** no instance found **")
def do_update_dict(self, arg):
"""Update an instance based on its ID with a dictionary representation"""
from models import storage
args = arg.split()
if len(args) == 0:
print("** class name missing **")
elif args[0] not in self.classes:
print("** class doesn't exist **")
elif len(args) == 1:
print("** instance ID missing **")
elif len(args) == 2:
print("** dictionary representation missing **")
else:
class_name = args[0]
instance_id = args[1].strip("\"'")
dictionary = ' '.join(args[2:]).replace("'", "\"")
try:
attrs = json.loads(dictionary)
objects = storage.all()
key = "{}.{}".format(class_name, instance_id)
if key in objects:
instance = objects[key]
for attr, value in attrs.items():
if hasattr(instance, attr):
attr_type = type(getattr(instance, attr))
setattr(instance, attr, attr_type(value))
instance.save()
else:
print("** attribute doesn't exist **")
else:
print("** no instance found **")
except ValueError:
print("** invalid dictionary representation **")
def do_count(self, arg):
"""Counts the number of instances of a class"""
from models import storage
args = arg.split()
if len(args) == 0:
print("** class name missing **")
elif args[0] not in self.classes:
print("** class doesn't exist **")
else:
class_name = args[0]
objects = storage.all()
count = sum(1 for obj in objects.values()
if type(obj).__name__ == class_name)
print(count)
def do_show_id(self, arg):
"""Show an instance based on its ID"""
from models import storage
args = arg.split()
if len(args) == 0:
print("** class name missing **")
elif args[0] not in self.classes:
print("** class doesn't exist **")
elif len(args) == 1:
print("** instance ID missing **")
else:
class_name = args[0]
instance_id = args[1].strip("\"'")
objects = storage.all()
key = "{}.{}".format(class_name, instance_id)
if key in objects:
print(objects[key])
else:
print("** no instance found **")
if __name__ == '__main__':
from models import storage
storage.reload()
HBNBCommand().cmdloop()