-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.py
executable file
·384 lines (362 loc) · 14.5 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/usr/bin/python3
''' Module class console.py '''
import cmd
import ast
import shlex
import models
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_ls = {'BaseModel': BaseModel, 'User': User,
'State': State, 'City': City, 'Amenity': Amenity,
'Place': Place, 'Review': Review}
store_all = models.storage.all()
class HBNBCommand(cmd.Cmd):
''' Class command interpreter HBNB '''
def do_EOF(self, enter):
''' Close the command interpreter when you use the \'EOF\' command '''
return True
def do_quit(self, enter):
''' Close the command interpreter when you use the \'quit\' command '''
return True
def emptyline(self):
''' Clean the last nonempty command entered '''
return False
def do_create(self, args):
''' Create a new class including his id number '''
if not args:
print('** class name missing **')
else:
arg = args.split()
if arg[0] in class_ls:
exc = class_ls[arg[0]]()
print(exc.id)
exc.save()
else:
print('** class doesn\'t exist **')
def do_show(self, args):
''' Print the object with id specified and his dictionary '''
if not args:
print('** class name missing **')
else:
arg = args.split()
if not arg[0] in class_ls:
print('** class doesn\'t exist **')
else:
if len(arg) > 1:
class_key = ''
class_key = arg[0] + '.' + arg[1]
if class_key in store_all:
print(store_all[class_key])
else:
print('** no instance found **')
else:
print('** instance id missing **')
def do_destroy(self, args):
''' Removes an object with id specified and his dictionary '''
if not args:
print('** class name missing **')
else:
arg = args.split()
if not arg[0] in class_ls:
print('** class doesn\'t exist **')
else:
if len(arg) > 1:
class_key = ''
class_key = arg[0] + '.' + arg[1]
if class_key in store_all:
store_all.pop(class_key)
models.storage.save()
else:
print('** no instance found **')
else:
print('** instance id missing **')
def do_all(self, args):
'''
Prints all string representation of all instances
based or not on the class name
ex: \'(hbnb ) all\' or \'(hbnb )\' all BaseModel
'''
ls_val = []
arg = args.split()
if len(arg) == 0:
for val in store_all.values():
ls_val.append(str(val))
print(ls_val)
elif arg[0] in class_ls:
for class_key in store_all:
if arg[0] in class_key:
ls_val.append(str(store_all[class_key]))
print(ls_val)
else:
print('** class doesn\'t exist **')
def do_update(self, args):
'''
Updates an instance based on the class name and
id by adding or updating attribute.
ex: \'(hbnb )\' update BaseModel 123456789 attr_name attr_value
'''
arg = shlex.split(args)
length = len(arg)
if length == 0:
print('** class name missing **')
return
if not arg[0] in class_ls:
print('** class doesn\'t exist **')
return
else:
if length == 1:
print('** instance id missing **')
return
else:
class_key = ''
class_key = arg[0] + '.' + arg[1]
if length == 2:
print('** attribute name missing **')
return
if length == 3:
print('** value missing **')
return
if length >= 4:
if class_key in store_all:
setattr(store_all[class_key], arg[2], arg[3])
models.storage.save()
else:
print('** no instance found **')
return
def count(self, args):
'''
count all string representation of all instances
based or not on the class name
ex: \'(hbnb ) all\' or \'(hbnb )\' all BaseModel
'''
count = 0
ls_val = []
arg = args.split()
if arg[0] in class_ls:
for class_key in store_all:
if arg[0] in class_key:
ls_val.append(str(store_all[class_key]))
count += 1
print(count)
else:
print('** class doesn\'t exist **')
@staticmethod
def do_BaseModel(args):
'''
do_BaseModel, use the class.command of the console as input
while execute the command inserted
'''
if args:
fnd = args[args.find('("') + 2:args.find('")')]
command = args[0:args.find('(')]
arg = command.split(".")
if arg[1] == 'all':
HBNBCommand.do_all(HBNBCommand, 'BaseModel')
if arg[1] == 'count':
HBNBCommand.count(HBNBCommand, 'BaseModel')
if arg[1] == 'show':
HBNBCommand.do_show(HBNBCommand, 'BaseModel {}'.format(fnd))
if arg[1] == 'destroy':
HBNBCommand.do_destroy(HBNBCommand, 'BaseModel {}'.format(fnd))
if arg[1] == 'update':
ls_fd = fnd.split("\", \"")
if len(ls_fd) >= 3:
st = 'BaseModel {} {} {}'.format(ls_fd[0],
ls_fd[1], ls_fd[2])
HBNBCommand.do_update(HBNBCommand, st)
else:
fnd3 = fnd[0:fnd.find('"')]
fnd4 = fnd[fnd.find('", ') + 3:len(args) - 1]
dc = ast.literal_eval(fnd4)
for key, val in dc.items():
st = 'BaseModel {} {} {}'.format(fnd3, key, val)
HBNBCommand.do_update(HBNBCommand, st)
@staticmethod
def do_User(args):
'''
do_User, use the class.command of the console as input
while execute the command inserted
'''
if args:
fnd = args[args.find('("') + 2:args.find('")')]
command = args[0:args.find('(')]
arg = command.split(".")
if arg[1] == 'all':
HBNBCommand.do_all(HBNBCommand, 'User')
if arg[1] == 'count':
HBNBCommand.count(HBNBCommand, 'User')
if arg[1] == 'show':
HBNBCommand.do_show(HBNBCommand, 'User {}'.format(fnd))
if arg[1] == 'destroy':
HBNBCommand.do_destroy(HBNBCommand, 'User {}'.format(fnd))
if arg[1] == 'update':
ls_fd = fnd.split("\", \"")
if len(ls_fd) >= 3:
st = 'User {} {} {}'.format(ls_fd[0], ls_fd[1], ls_fd[2])
HBNBCommand.do_update(HBNBCommand, st)
else:
fnd3 = fnd[0:fnd.find('"')]
fnd4 = fnd[fnd.find('", ') + 3:len(args) - 1]
dc = ast.literal_eval(fnd4)
for key, val in dc.items():
st = 'User {} {} {}'.format(fnd3, key, val)
HBNBCommand.do_update(HBNBCommand, st)
@staticmethod
def do_State(args):
'''
do_State, use the class.command of the console as input
while execute the command inserted
'''
if args:
fnd = args[args.find('("') + 2:args.find('")')]
command = args[0:args.find('(')]
arg = command.split(".")
if arg[1] == 'all':
HBNBCommand.do_all(HBNBCommand, 'State')
if arg[1] == 'count':
HBNBCommand.count(HBNBCommand, 'State')
if arg[1] == 'show':
HBNBCommand.do_show(HBNBCommand, 'State {}'.format(fnd))
if arg[1] == 'destroy':
HBNBCommand.do_destroy(HBNBCommand, 'State {}'.format(fnd))
if arg[1] == 'update':
ls_fd = fnd.split("\", \"")
if len(ls_fd) >= 3:
st = 'State {} {} {}'.format(ls_fd[0], ls_fd[1], ls_fd[2])
HBNBCommand.do_update(HBNBCommand, st)
else:
fnd3 = fnd[0:fnd.find('"')]
fnd4 = fnd[fnd.find('", ') + 3:len(args) - 1]
dc = ast.literal_eval(fnd4)
for key, val in dc.items():
st = 'State {} {} {}'.format(fnd3, key, val)
HBNBCommand.do_update(HBNBCommand, st)
@staticmethod
def do_City(args):
'''
do_City, use the class.command of the console as input
while execute the command inserted
'''
if args:
fnd = args[args.find('("') + 2:args.find('")')]
command = args[0:args.find('(')]
arg = command.split(".")
if arg[1] == 'all':
HBNBCommand.do_all(HBNBCommand, 'City')
if arg[1] == 'count':
HBNBCommand.count(HBNBCommand, 'City')
if arg[1] == 'show':
HBNBCommand.do_show(HBNBCommand, 'City {}'.format(fnd))
if arg[1] == 'destroy':
HBNBCommand.do_destroy(HBNBCommand, 'City {}'.format(fnd))
if arg[1] == 'update':
ls_fd = fnd.split("\", \"")
if len(ls_fd) >= 3:
st = 'City {} {} {}'.format(ls_fd[0], ls_fd[1], ls_fd[2])
HBNBCommand.do_update(HBNBCommand, st)
else:
fnd3 = fnd[0:fnd.find('"')]
fnd4 = fnd[fnd.find('", ') + 3:len(args) - 1]
dc = ast.literal_eval(fnd4)
for key, val in dc.items():
st = 'City {} {} {}'.format(fnd3, key, val)
HBNBCommand.do_update(HBNBCommand, st)
@staticmethod
def do_Amenity(args):
'''
do_Amenity, use the class.command of the console as input
while execute the command inserted
'''
if args:
fnd = args[args.find('("') + 2:args.find('")')]
command = args[0:args.find('(')]
arg = command.split(".")
if arg[1] == 'all':
HBNBCommand.do_all(HBNBCommand, 'Amenity')
if arg[1] == 'count':
HBNBCommand.count(HBNBCommand, 'Amenity')
if arg[1] == 'show':
HBNBCommand.do_show(HBNBCommand, 'Amenity {}'.format(fnd))
if arg[1] == 'destroy':
HBNBCommand.do_destroy(HBNBCommand, 'Amenity {}'.format(fnd))
if arg[1] == 'update':
ls_fd = fnd.split("\", \"")
if len(ls_fd) >= 3:
st = 'Amenity {} {} {}'.format(ls_fd[0],
ls_fd[1], ls_fd[2])
HBNBCommand.do_update(HBNBCommand, st)
else:
fnd3 = fnd[0:fnd.find('"')]
fnd4 = fnd[fnd.find('", ') + 3:len(args) - 1]
dc = ast.literal_eval(fnd4)
for key, val in dc.items():
st = 'Amenity {} {} {}'.format(fnd3, key, val)
HBNBCommand.do_update(HBNBCommand, st)
@staticmethod
def do_Place(args):
'''
do_Place, use the class.command of the console as input
while execute the command inserted
'''
if args:
fnd = args[args.find('("') + 2:args.find('")')]
command = args[0:args.find('(')]
arg = command.split(".")
if arg[1] == 'all':
HBNBCommand.do_all(HBNBCommand, 'Place')
if arg[1] == 'count':
HBNBCommand.count(HBNBCommand, 'Place')
if arg[1] == 'show':
HBNBCommand.do_show(HBNBCommand, 'Place {}'.format(fnd))
if arg[1] == 'destroy':
HBNBCommand.do_destroy(HBNBCommand, 'Place {}'.format(fnd))
if arg[1] == 'update':
ls_fd = fnd.split("\", \"")
if len(ls_fd) >= 3:
st = 'Place {} {} {}'.format(ls_fd[0], ls_fd[1], ls_fd[2])
HBNBCommand.do_update(HBNBCommand, st)
else:
fnd3 = fnd[0:fnd.find('"')]
fnd4 = fnd[fnd.find('", ') + 3:len(args) - 1]
dc = ast.literal_eval(fnd4)
for key, val in dc.items():
st = 'Place {} {} {}'.format(fnd3, key, val)
HBNBCommand.do_update(HBNBCommand, st)
@staticmethod
def do_Review(args):
'''
do_Review, use the class.command of the console as input
while execute the command inserted
'''
if args:
fnd = args[args.find('("') + 2:args.find('")')]
command = args[0:args.find('(')]
arg = command.split(".")
if arg[1] == 'all':
HBNBCommand.do_all(HBNBCommand, 'Review')
if arg[1] == 'count':
HBNBCommand.count(HBNBCommand, 'Review')
if arg[1] == 'show':
HBNBCommand.do_show(HBNBCommand, 'Review {}'.format(fnd))
if arg[1] == 'destroy':
HBNBCommand.do_destroy(HBNBCommand, 'Review {}'.format(fnd))
if arg[1] == 'update':
ls_fd = fnd.split("\", \"")
if len(ls_fd) >= 3:
st = 'Review {} {} {}'.format(ls_fd[0], ls_fd[1], ls_fd[2])
HBNBCommand.do_update(HBNBCommand, st)
else:
fnd3 = fnd[0:fnd.find('"')]
fnd4 = fnd[fnd.find('", ') + 3:len(args) - 1]
dc = ast.literal_eval(fnd4)
for key, val in dc.items():
st = 'Review {} {} {}'.format(fnd3, key, val)
HBNBCommand.do_update(HBNBCommand, st)
if __name__ == '__main__':
HBNBCommand.prompt = '(hbnb) '
HBNBCommand().cmdloop()