-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.py
executable file
·279 lines (235 loc) · 8.78 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
#!/usr/bin/python3
"""This module is a console that contains the entry
point of the command interpreter"""
import cmd
import string
import sys
import models
import re
from models.base_model import BaseModel
from models.user import User
from models.amenity import Amenity
from models.city import City
from models.place import Place
from models.state import State
from models.review import Review
list_classes = ["BaseModel", "User", "Place", "State",
"City", "Amenity", "Review"]
def parser_list(lists):
"""this function parses a list that is of type string into a
true list by decomposing each element and inserting
into a new list
Return: New List"""
lista = lists[1:-1].replace("\"", "")
lista = lista.split(", ")
return lista
def isfloat(num):
"""This function evaluates if a number
is a float or not a float and returns
True if it is a float and False if not."""
try:
float(num)
return True
except ValueError:
return False
def parser(lista):
"""This function parses the elements of a list if
it is a string it removes the quotes and if it does
not have quotes it keeps it the same, at the
end it returns a parsed list"""
final_list = []
for i in lista:
if (i[0] == '\"'):
final_list.append(i[1:-1])
else:
final_list.append(i)
return final_list
def validator(lists):
"""They are a valid function if what they are passing
me is a list or has quotes to be able to parse it and
return a set element with the correct type to be
inserted in the dictionary later"""
new_list = []
lista_principal = []
total = len(lists)
new_member = lists[3]
if (lists[3][0] == '\"' or lists[3][0] == '['):
for i in range(total):
if (lists[i][-1] == ']'):
new_list.append(lists[i])
break
if (i > 3 and lists[i][-1] == '\"'):
new_list.append(lists[i])
break
elif i >= 3:
new_list.append(lists[i])
else:
lista_principal.append(lists[i])
new_str = " ".join(new_list)
lista_principal.append(new_str)
lists = lista_principal
if (lists[3][0] == '\"'):
new_member = lists[3][1:-1]
else:
new_member = lists[3]
return new_member
class HBNBCommand(cmd.Cmd):
"""Command Interpreter of Airbnb"""
prompt = "(hbnb) "
def do_quit(self, arg):
"""Quit command to exit the program\n"""
exit()
def do_EOF(self, line):
"""EOF command to exit the program\n"""
print()
exit()
def emptyline(self):
"""Does not perform any action\n"""
pass
def default(self, line):
"""Function of the HBNB command that executes
functions by default before all functions"""
lists = line.split(sep=".", maxsplit=1)
lista = lists[1].split("(")
string = lista[-1].replace(")", "")
lista_f = list(string.split(", "))
final = [lista[0], lists[0]]
final = final + lista_f
if final[0] == "all":
line = final[1]
"""Retrieve all instances of a class by using:
<class name>.all()"""
HBNBCommand.do_all(self, line)
elif final[0] == "count":
line = final[1]
"""retrieve the number of instances of a class:
<class name>.count()"""
HBNBCommand.do_count(self, line)
elif final[0] == "show":
line = parser(final[1:])
line = " ".join(line)
"""retrieve an instance based on its ID:
<class name>.show(<id>)"""
HBNBCommand.do_show(self, line)
elif final[0] == "destroy":
line = parser(final[1:])
line = " ".join(line)
"""destroy an instance based on his ID:
<class name>.destroy(<id>)"""
HBNBCommand.do_destroy(self, line)
elif final[0] == "update":
line = parser(final[1:])
line = " ".join(line)
"""update an instance based on his ID:
<class name>.update(<id>, <attribute name>, <attribute value>)"""
HBNBCommand.do_update(self, line)
def do_create(self, line):
"""Creates a new instance, saves it and prints the id\n"""
if (line == "" or line is None):
print("** class name missing **")
elif (line not in list_classes):
print("** class doesn't exist **")
else:
line_to_input = f'{line}()'
instance = eval(line_to_input)
instance.save()
print(instance.id)
def do_show(self, line):
"""Show string representation of an instance and id\n"""
lists = line.split()
if (line == "" or line is None):
print("** class name missing **")
elif (lists[0] not in list_classes):
print("** class doesn't exist **")
elif len(lists) != 2:
print("** instance id missing **")
else:
key = f'{lists[0]}.{lists[1]}'
if key in models.storage.all().keys():
print(models.storage.all()[key])
else:
print("** no instance found **")
def do_destroy(self, line):
"""Deletes an instance based on the class name and id\n"""
lists = line.split()
if (line == "" or line is None):
print("** class name missing **")
elif (lists[0] not in list_classes):
print("** class doesn't exist **")
elif len(lists) != 2:
print("** instance id missing **")
else:
key = f'{lists[0]}.{lists[1]}'
if key in models.storage.all().keys():
del models.storage.all()[key]
models.storage.save()
else:
print("** no instance found **")
def do_all(self, line):
"""Prints all string representation of all instances\n"""
lists = line.split()
new_list = []
if len(lists) < 1:
for member in models.storage.all().values():
new_list.append(str(member))
print(new_list)
else:
if (lists[0] in list_classes):
for member in models.storage.all():
if re.search(lists[0], member):
print(models.storage.all()[member])
else:
print("** class doesn't exist **")
def do_update(self, line):
"""Update an instance based on the class name and id\n"""
lists = line.split()
total = len(lists)
ints = ["number_rooms", "number_bathrooms",
"max_guest", "price_by_night"]
floatings = ["latitude", "longitude"]
if (line == "" or line is None):
print("** class name missing **")
return
if (lists[0] not in list_classes):
print("** class doesn't exist **")
return
if len(lists) == 1:
print("** instance id missing **")
return
else:
key = f'{lists[0]}.{lists[1]}'
if key in models.storage.all().keys():
if len(lists) >= 3:
if len(lists) >= 4:
new_member = validator(lists)
lists[3] = new_member
if lists[0] == "Place":
if lists[2] in ints and lists[3].isdigit():
lists[3] = int(lists[3])
elif (lists[2] in floatings and isfloat(lists[3])):
lists[3] = float(lists[3])
elif lists[3][0] == "[":
lists[3] = parser_list(lists[3])
elif (lists[2] not in ints
) and (lists[2] not in floatings):
setattr(models.storage.all()[key],
lists[2], lists[3])
models.storage.all()[key].save()
setattr(models.storage.all()[key], lists[2], lists[3])
models.storage.all()[key].save()
else:
print("** value missing **")
else:
print("** attribute name missing **")
else:
print("** no instance found **")
def do_count(self, line):
"""Retrieve the number of instances of a class\n"""
lists = line.split()
count = 0
for classes in models.storage.all().values():
if lists[0] == classes.__class__.__name__:
count += 1
print(count)
if __name__ == '__main__':
HBNBCommand().cmdloop()