-
Notifications
You must be signed in to change notification settings - Fork 1
/
console.py
executable file
·361 lines (334 loc) · 13.1 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
#!/usr/bin/python3
"""Importing some Standard modules and modules from our packages"""
import cmd
import datetime as dt
import re
from models.amenity import Amenity
from models.base_model import BaseModel
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
from models import storage
"""
This is a Python class that will act as an interface for the first phase
of the AirBnB Clone project.
"""
all_classes = {
"Amenity": Amenity,
"BaseModel": BaseModel,
"City": City,
"Place": Place,
"Review": Review,
"State": State,
"User": User
}
attributes = {
"BaseModel": {
"id": str,
"created_at": dt.datetime,
"updated_at": dt.datetime
}, "User": {
"email": str,
"password": str,
"first_name": str,
"last_name": str
}, "State": {
"name": str
}, "City": {
"state_id": str,
"name": str
}, "Amenity": {
"name": str
}, "Place": {
"city_id": str,
"user_id": str,
"name": str,
"description": str,
"number_rooms": int,
"number_bathrooms": int,
"max_guest": int,
"price_by_night": int,
"latitude": float,
"longitude": float,
"amenity_ids": list
}, "Review": {
"place_id": str,
"user_id": str,
"text": str
}
}
class HBNBCommand(cmd.Cmd):
"""
This is a class modelling the inteface (CLI) for AirBnB Clone project.
"""
"""Specify the prompt for the CLI"""
prompt = "(hbnb) "
def do_quit(self, arg: any) -> None:
"""Issues a quit command to the CLI by returning True"""
return True
def help_quit(self) -> None:
"""Updates the help for quit"""
print("")
print("The `quit` command issues a command to quit the CLI.\n")
print("Usage:\n(hbnb) quit\n")
def do_EOF(self, arg: any) -> True:
"""Returns True and breaks the cmdloop"""
print("")
return True
def help_EOF(self) -> None:
"""Updates the help for EOF"""
print("")
print("The `EOF` command returns True to break the cmdloop", end=" ")
print("and exits the CLI.\n")
print("Usage:\n(hbnb) EOF\nor\n(hbnb) <CTRL + C>")
print("or\n(hbnb) <CTRL + Z>\n")
def emptyline(self) -> None:
"""Method that does nothing when the ENTER key is pressed without a
command."""
pass
def do_create(self, args) -> None:
"""Public instance method that creates new instance of a class, save
it to a JSON file and print the `id` of the instance"""
if len(args) == 0:
print("** class name missing **")
return
arg_num = args.split(" ")
if arg_num[0] in all_classes.keys():
obj = eval(arg_num[0] + "()")
id = getattr(obj, 'id')
print(id)
storage.save()
return
else:
print("** class doesn't exist **")
return
def help_create(self) -> None:
"""Updates the help for create"""
print("")
print("The `create` command creates an instance of a class, ", end="")
print("saves it to the storage and prints out the ID of the", end=" ")
print("instance created.\n")
print("Models available includes:\n")
print("\tAmenity\n\tBaseModel\n\tCity\n\tPlace\n\tReview\n\t", end="")
print("State\n\tUser\n")
print("Usage:\n(hbnb) create User\n")
def do_show(self, args=None) -> None:
"""Public instance method that displays the string instance of a
class, based on the instance id and classname specified"""
if len(args) == 0:
print("** class name missing **")
return
arg_num = args.split(" ")
if arg_num[0] in all_classes.keys():
if len(arg_num) >= 2:
id = "{}.{}".format(arg_num[0], str(arg_num[1]))
str_obj = storage.all()
if id in str_obj.keys():
obj = str_obj[id]
print(obj)
return
else:
print("** no instance found **")
return
else:
print("** instance id missing **")
return
else:
print("** class doesn't exist **")
return
def help_show(self) -> None:
"""Updates the help for show"""
print("")
print("The `show` command displays the details and string", end=" ")
print("representation of an instance based on class name", end=" ")
print("and instance id provided.\n")
print("Usage:\n(hbnb) show User 51a155c1-214a-4923-8d53-523900fed722")
print("")
def do_destroy(self, args) -> None:
"""Public instance method that deletes the instance of a class,
based on the instance id and classname specified"""
if len(args) == 0:
print("** class name missing **")
return
arg_num = args.split(" ")
if arg_num[0] in all_classes.keys():
if len(arg_num) == 2:
id = "{}.{}".format(arg_num[0], str(arg_num[1]))
str_obj = storage.all()
if id in str_obj.keys():
del (str_obj[id])
storage.save()
return
else:
print("** no instance found **")
return
else:
print("** instance id missing **")
return
else:
print("** class doesn't exist **")
return
def help_destroy(self) -> None:
"""Updates the help for destroy"""
print("")
print("The `destroy` command deletes the details of an ", end="")
print("instance based on class name and instance id provided.\n")
print("Usage:\n(hbnb) destroy User 51a155c1-214a-4923-8d53-52fed22\n")
def do_all(self, args) -> None:
"""Public instance method that displays the string instance of all
the instances of a class based on the classname specified or no
classname specified"""
list_all = []
if args != "":
arg_num = args.split(" ")
if arg_num[0] in all_classes.keys():
for key, val in storage.all().items():
if type(val).__name__ == arg_num[0]:
list_all.append(str(val))
else:
print("** class doesn't exist **")
return
else:
for key, val in storage.all().items():
list_all.append(str(val))
print(list_all)
def help_all(self) -> None:
"""Updates the help for all"""
print("")
print("The `all` command displays the string representation", end="")
print(" of all class instances present in the storage.\n")
print("Usage:\n(hbnb) all User\nor\n(hbnb) User.all()\n")
def do_update(self, args) -> None:
"""Public instance method that updates a specified instance of a class
using the id and either adding more attributes or updating an
attribute"""
if len(args) == 0:
print("** class name missing **")
return
regx = r'^(\S+)(?:\s(\S+)(?:\s(\S+)(?:\s((?:"[^"]*")|(?:(\S)+)))?)?)?'
is_match = re.search(regx, args)
cls_name_match = is_match.group(1)
uid_match = is_match.group(2)
attr_match = is_match.group(3)
val_match = is_match.group(4)
if is_match:
if cls_name_match in all_classes.keys():
if uid_match:
id = "{}.{}".format(cls_name_match, uid_match)
if id in storage.all():
if attr_match:
if val_match:
datatype = None
if not re.search('^".*"$', val_match):
if '.' in val_match:
datatype = float
else:
datatype = int
else:
val_match = val_match.replace('"', '')
attrs = attributes[cls_name_match]
if attr_match in attrs:
val_match = attrs[attr_match](val_match)
elif datatype:
try:
val_match = datatype(val_match)
except ValueError:
pass
setattr(storage.all()[id], attr_match,
val_match)
storage.all()[id].save()
else:
print("** value missing **")
else:
print("** attribute name missing **")
else:
print("** no instance found **")
else:
print("** instance id missing **")
else:
print("** class doesn't exist **")
else:
print("** class name missing **")
def help_update(self) -> None:
"""Updates the help for update"""
print("")
print("The `update` command update a specified instance of a", end="")
print(" using the class name and the ID of the instance, and", end="")
print(" and the specifying the attribute to update or adding", end="")
print(" a new attribute plus the value.\n")
print("Usage:\n(hbnb) update User 1234-5678 email 'test@oop.com'\n")
def do_count(self, args) -> None:
"""Public instance method that counts the instances of a class"""
if len(args) == 0:
print("** class name missing **")
return
arg_num = args.split(" ")
instance_count = 0
if arg_num[0]:
if arg_num[0] in all_classes.keys():
for num in storage.all():
if num.startswith(arg_num[0] + "."):
instance_count += 1
else:
print("** class doesn't exist **")
return
else:
print("** class name missing **")
return
print(instance_count)
def help_count(self) -> None:
"""Updates the help for count"""
print("")
print("The `count` command displays the number of instances", end="")
print(" of a specified class found in the json file.", end="\n")
print("Usage:\n(hbnb) count User'\nor\n(hbnb) User.count()\n")
def default(self, args):
"""This public instance method is called when an invalid command is
given. If this is not overwritten, it displays an error, but we will
be handling invalid commands before returning False if command not
found."""
arg_num = args.split(".")
cls_name = arg_num[0]
if cls_name in all_classes.keys() and len(arg_num) > 1:
cmd = arg_num[1]
cmd = cmd.replace("()", "")
if cmd in ['all', 'count']:
if cmd == 'all':
self.do_all(cls_name)
elif cmd == 'count':
self.do_count(cls_name)
else:
if "show" in cmd:
id = cmd.split("(")[1].strip(")")
joiner = cls_name + " " + id
joiner = joiner.replace('"', "")
self.do_show(joiner)
elif "destroy" in cmd:
id = cmd.split("(")[1].strip(")")
joiner = cls_name + " " + id
joiner = joiner.replace('"', "")
self.do_destroy(joiner)
elif "update" in cmd:
clsname = cls_name
if "{" not in cmd.split("(")[1]:
cid = cmd.split("(")[1].split(", ")[0].strip(')"')
cr_at = cmd.split("(")[1].split(", ")[1].strip(')"')
up_at = cmd.split("(")[1].split(", ")[2].strip(')"')
joiner = "{} {} {} {}".format(clsname, cid, cr_at,
up_at)
print(joiner)
self.do_update(joiner)
elif len(cmd.split("(")[1].split(", {")) == 2:
cid = cmd.split("(")[1].split(", {")[0].strip(')"')
stn = cmd.split("(")[1].split(", {")[1].strip(")")
dic = eval("{" + stn)
for key, val in dic.items():
joiner = "{} {} {} {}".format(clsname, cid,
key, str(val))
print(joiner)
self.do_update(joiner)
if __name__ == "__main__":
commnd = HBNBCommand()
commnd.cmdloop()