-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.py
executable file
·439 lines (407 loc) · 12.2 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#!/usr/bin/python3
'''
The HBNB console
'''
import cmd
from models.base_model import BaseModel
# from models import classes
from models import storage
from models.user import User
from models.place import Place
from models.state import State
from models.city import City
from models.amenity import Amenity
from models.review import Review
from shlex import split
import re
def parse(line):
'''
Ussage:
Search for text enclosed in curly braces
using a regular expression
'''
curly_braces = re.search(r'\{(.*?)\}', line)
'''
Usage:
Search for text enclosed in square brackets
using a regular expression
'''
brackets = re.search(r'\[(.*?)\]', line)
'''
Usage:
Check if there sre no curly braces found in
the input string
'''
if curly_braces is None:
'''
Usage:
Check if there are no square brackets
found in the input string
'''
if brackets is None:
'''
If neither curly braces nor square brackets
are present, split the input string by commas
'''
return [arg.strip(',') for arg in split(line)]
# end if
else:
'''
If square brackets are found,
split the input string up to the end of the brackets
'''
lexer = split(line[:brackets.span()[0]])
'''
Remove trailing commas and create a list of
split items
'''
my_list = [arg.strip(',') for arg in lexer]
'''
Append the contents of
the square brackets to the list
'''
my_list.append(brackets.group())
'''
Return the final list
'''
return my_list
# end else
# end if
else:
'''
If there are curly brces in the input string
Split the input string up to the end of the curly braces
'''
lexer = split(line[:curly_braces.span()[0]])
'''
Remove trailing commas and
create a list of split items
'''
my_list = [arg.strip(',') for arg in lexer]
'''
Append the contents of the curly braces to the list
'''
my_list.append(curly_braces.group())
'''
Return the final list
'''
return my_list
# end else
class HBNBCommand(cmd.Cmd):
'''
cmdln - interpreter class
'''
prompt = '(hbnb) '
__classes = {
'BaseModel',
'User',
'State',
'City',
'Place',
'Amenity',
'Review'
}
def emptyline(self):
'''
Usage:
`Empty line + ENTER` do nothing
'''
pass
# end method
def do_quit(self, line):
'''
Usage:
Quit command to exit the program
'''
return True
# end method
def do_EOF(self, line):
'''
EOF command to exit program
'''
print('')
return True
# end method
def do_create(self, line):
'''
creates a new instance of class BaseModel,
saves it to JSON file and print the id
'''
args = parse(line)
if not args or len(args) == 0:
'''If class is missing '''
print('** class name missing **')
# end if
elif args[0] not in self.__classes:
''' if it doe not exist '''
print("** class doesn't exist **")
# end elif
else:
print(eval(args[0])().id)
storage.save()
# end else
# end method
def do_show(self, line):
'''shows an instance'''
args = parse(line)
objects = storage.all()
if not args or len(args) == 0 or args[0] == "":
'''
if class is missing
'''
print('** class name missing **')
# end if
elif args[0] not in self.__classes:
'''
if class does not exist
'''
print("** class doesn't exist **")
# end elif
elif len(args) < 2 or args[1] == '':
'''
command without an argument
'''
print('** instance id missing **')
# end elif
else:
key = '{}.{}'.format(args[0], args[1])
if key in objects:
print(objects[key])
# end if
else:
print('** no instance found **')
# end else
# end else
# end method
def do_destroy(self, line):
'''
Usage: `destroy <class><id>`
`<class>.destroy(<id>)`
Deletes the class instance of a given id.
'''
args = parse(line)
cl_nm = '{}.{}.format(args[0], args[1])'
objects = storage.all()
if len(args) == 0:
''' If class is missing '''
print('** class name missing **')
return False
# end if
# elif cl_nm not in classes:
elif args[0] not in HBNBCommand.__classes:
'''
if class does not exist
'''
print("** class doesn't exist **")
return False
# end elif
elif len(args) == 1:
'''
If the id is missing
'''
print('** instance id missing **')
return False
# end elif
else:
key = cl_nm + '.' + args[1]
objects = storage.all()
if key in objects:
'''
if exists
'''
del objects[key]
storage.save()
# end if
else:
'''
If the instance of the class name
doesn’t exist for the id
'''
print('** no instance found **')
# end else
# end method
def do_all(self, line):
'''
Usage: all | all <class>
prints all instance
'''
args = parse(line)
objects = storage.all()
if len(args) > 0 and args[0] not in self.__classes:
print("** class doesn't exist **")
return False
else:
'''
class_name = args[0]
print([str(obj) for key, obj in objects.items()
if key.startswith(class_name)])
'''
objects = []
for obj in storage.all().values():
if len(args) > 0 and args[0] == obj.__class__.__name__:
objects.append(obj.__str__())
# end if
elif len(args) == 0:
objects.append(obj.__str__())
# end elif
# end for
print(objects)
# end else
# end method
def do_update(self, line):
'''
Usage:
updates instance
'''
args = parse(line)
objects = storage.all()
if len(args) < 2:
'''
checking the lenght of args:
At least 2 elements are needed:
- The class name (args[0])
- The instance id (args[1])
'''
print('** class name missing **')
return False
# end if
class_name = args[0]
# key = '{}.{}'.format(class_name, args[1])
key = args[0] + '.' + args[1]
if class_name not in HBNBCommand.__classes:
'''
the class does not exist
'''
print("** class doesn't exist **")
return False
# end if
if len(args) == 1:
'''
command without argument
'''
print("** instance id missing **")
return False
# end if
if key not in objects.keys():
'''
If the instance of the class name
doesn’t exist for the id
'''
print('** no instance found **')
return False
# end if
if len(args) == 2:
print('** attribute name missing **')
return False
# end if
if len(args) == 3:
args_type = type(eval(args[2]))
if args_type != dict:
raise NameError(print('** value missing **'))
# end if
return False
# end if
if len(args) == 4:
objs = objects[key]
my_dict = objs.__class__.__dict__
if args[2] in my_dict.keys():
value_type = type(my_dict[args[2]])
objs.__dict__[args[2]] = value_type(args[3])
# end if
else:
objs.__dict__[args[2]] = args[3]
# end else
# end if
elif type(eval(args[2])) == dict:
objs = objects[key]
data_type = {str, float, int}
for i, val in eval(args[2]).items():
if i in my_dict.key() and type(my_dict[i]) in data_type:
value_type = type(my_dict[i])
objs.__dict__[i] = value_type(val)
# end if
else:
objs.__dict__[k] = v
# end else
# end for
# end elif
storage.save()
def default(self, line):
'''
Usage:
Behaviour when module input is invalid
'''
'''
Create a dictionary that maps commands
to their corresponding methods.
'''
my_default_dict = {
'all': self.do_all,
'show': self.do_show,
'destroy': self.do_destroy,
'update': self.do_update,
'count': self.do_count
}
'''
Check to see if the input argument contains a period
(this indicates a method call).
'''
match = re.search(r'\.', line)
if match is not None:
'''
Usage:
Split the argument into two parts
before and after the period
'''
args = [line[:match.span()[0]], line[match.span()[1]:]]
'''
search for text enclosed in parentheseees
'''
match = re.search(r'\((.*?)\)', args[1])
if match is not None:
'''
Split the method call into the command and parameters
'''
my_command = [args[1][:match.span()[0]], match.group()[1:-1]]
if my_command[0] in my_default_dict.keys():
'''
if the command is valid,
construct the full command and call the method
'''
key = '{}.{}'.format(args[0],my_command[1])
key_const = my_default_dict[my_command[0]](key)
'''
If the input does not match any of the syntax(knownn),
print the error message
'''
# print('*** Unknown syntax: {}'.format(line))
return False
def do_count(self, line):
'''
Usage:
This used to retrieve
the number of instances of a class:
<class name>.count()
'''
# Parse the input argument to identify the class name
args = parse(line)
if not args:
return False
# the counter
counter = 0
'''
We Iterate through all instances stored in the 'storage'
'''
for inst in storage.all().values():
'''
we checck if class name of instance
matches the provided class name
'''
if args[0] == inst.__class__.__name__:
''' increment count '''
counter += 1
# end if
# end for
print(counter)
if __name__ == '__main__':
HBNBCommand().cmdloop()