-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.py
More file actions
executable file
·282 lines (253 loc) · 9.57 KB
/
console.py
File metadata and controls
executable file
·282 lines (253 loc) · 9.57 KB
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
#!/usr/bin/python3
"""The console of HBnB project,
to control the models and the storage engine"""
import cmd
from models.engine.file_storage import FileStorage
from models.base_model import BaseModel
from models.state import State
from models.city import City
from models.place import Place
from models.amenity import Amenity
from models.user import User
from models.review import Review
class HBNBCommand(cmd.Cmd):
"""HBNBConsole to control the storage engine
Arguments:
flag: Error flag
"""
flag = 'error'
def __init__(self, completeKey='tab', stdin=None, stdout=None):
"""HBNBCommand Constructor"""
super().__init__(completekey=completeKey, stdin=stdin, stdout=stdout)
self.prompt = '(hbnb) '
def emptyline(self):
"""Do nothing if the line is empty"""
return
def do_quit(self, line):
"""Quitting if line == quit"""
return True
def do_EOF(self, line):
"""Quitting with ctrl+d"""
print('')
return True
def help_quit(self):
"""The quit command help"""
print('Quit command to exit the program\n')
def help_EOF(self):
"""The EOF command help"""
print('EOF command to exit the program\n')
def default(self, line):
"""Handling User.count(), User.all(), User.create()
User.show(id), User.destroy(id),
User.update(id, attr, val)
Arguments:
quotes: Using for stripping
methods: methods map
obj: Getting the instance
method: Getting the method to execute
all_params: The params to pass to method
"""
if '.' in line:
quotes = ' \'"'
methods = {
'create': self.do_create,
'all': self.do_all,
'count': self.do_count,
'show': self.do_show,
'destroy': self.do_destroy,
'update': self.do_update
}
obj, rest = line.split('.', 1)
method, params = rest.split('(', 1)
if params == ')':
# Handling no args, ex: args = ''
all_params = obj
else:
if ',' not in params:
# Handling just id arg, ex: args = id
all_params = obj + ' ' + params.strip(quotes+')')
elif ',' in params:
# Handling args for update(), id - key - val
id, items = params.split(',', 1)
id = id.strip(quotes)
items = items.strip(' )(')
if items.startswith('{') and items.endswith('}'):
all_params = obj + ' ' + id + ' ' + items
else:
# Handling just one item (key, val) passing to update()
k_and_v = [x.strip(quotes) for x in items.split(',')]
all_params = [obj, id] + k_and_v
all_params = ' '.join(all_params).strip(' ')
# Invoking the proper method for each all_params
if method in methods.keys():
methods[method](all_params)
else:
cmd.Cmd.default(self, line)
def do_create(self, line):
"""Creating a new instance and save it"""
if self.check_line(line) == HBNBCommand.flag:
return
if self.check_name(line) == HBNBCommand.flag:
return
my_model = FileStorage.models_map[line]()
my_model.save()
print(my_model.id)
def do_show(self, line):
"""Printing the string representation"""
if self.check_line(line) == HBNBCommand.flag:
return
args = line.split(' ')
length = len(args)
if self.check_name(args[0]) == HBNBCommand.flag:
return
elif length == 1:
print('** instance id missing **')
return
elif length == 2:
obj_key = '.'.join([args[0], args[1]])
if self.check_instance(obj_key) == HBNBCommand.flag:
return
print(self.all_objects()[obj_key])
def do_destroy(self, line):
"""Deletes an instance based on the class name and id"""
if self.check_line(line) == HBNBCommand.flag:
return
args = line.split(' ')
length = len(args)
if self.check_name(args[0]) == HBNBCommand.flag:
return
elif length == 1:
print('** instance id missing **')
return
if length == 2:
obj_key = '.'.join([args[0], args[1]])
if self.check_instance(obj_key) == HBNBCommand.flag:
return
del self.all_objects()[obj_key]
self.store_save()
self.store_reload()
def do_destroyall(self, line):
"""Resetting everything"""
keys = list(self.all_objects().keys())
if line:
if self.check_name(line) == HBNBCommand.flag:
return
for key in keys:
obj_name, obj_id = key.split('.')
if obj_name == line:
del self.all_objects()[key]
else:
for key in keys:
del self.all_objects()[key]
self.store_save()
self.store_reload()
def do_all(self, line):
"""Printing all string representation of all instances"""
objects_list = []
if not line:
for val in self.all_objects().values():
objects_list.append(val.__str__())
else:
if self.check_name(line) == HBNBCommand.flag:
return
for key, val in self.all_objects().items():
if key.split('.')[0] == line:
objects_list.append(val.__str__())
print(objects_list)
def do_update(self, line):
"""Updating the instance by adding new attributes"""
if self.check_line(line) == HBNBCommand.flag:
return
args = line.split(' ')
quotes = ' "\''
length = len(args)
if length >= 2:
obj_key = '.'.join([args[0], args[1]])
if self.check_name(args[0]) == HBNBCommand.flag:
return
elif length == 1:
print('** instance id missing **')
return
elif self.check_instance(obj_key) == HBNBCommand.flag:
return
elif length == 2:
print('** attribute name missing **')
return
elif length == 3:
print('** value missing **')
return
elif length == 4:
# Handling adding one attribute
if self.attr_valid(args[2]) == HBNBCommand.flag:
return
setattr(self.all_objects()[obj_key],
args[2].strip(quotes), self.cast_attr(args[3]))
elif length > 4:
# Handling if there is a dictionary with valid items
if args[2].startswith('{') and args[-1].endswith('}'):
expected_dict = eval(' '.join(args[2:]))
print(args)
if type(expected_dict) == dict:
for k, v in expected_dict.items():
if self.attr_valid(args[2]) == HBNBCommand.flag:
return
setattr(self.all_objects()[obj_key],
k.strip(quotes), self.cast_attr(v))
else:
# Handling more than attr and no dictionary
setattr(self.all_objects()[obj_key],
args[2].strip(quotes), self.cast_attr(args[3]))
print(args)
self.store_save()
self.store_reload()
def attr_valid(self, attr):
if attr in ['id', 'created_at', 'updated_at']:
return HBNBCommand.flag
def do_count(self, line):
"""Counting How many instance are there"""
counter = 0
if line:
if self.check_name(line) == HBNBCommand.flag:
return
for key in self.all_objects().keys():
if key.split('.')[0] == line:
counter += 1
elif not line:
return
print(counter)
def cast_attr(self, var):
"""Editing the attr value before saving to the file.json"""
try:
return int(var)
except ValueError:
try:
return float(var)
except ValueError:
var = var.strip(' \'"')
return str(var)
def check_line(self, line):
"""Checking if the use didnot with the class name"""
if not line:
print('** class name missing **')
return HBNBCommand.flag
def check_name(self, name):
"""Checking if the use is writing the class name wrongly"""
if name not in FileStorage.models_map.keys():
print('** class doesn\'t exist **')
return HBNBCommand.flag
def check_instance(self, key):
"""Checking if instance not found, by checking the obj_key"""
if key not in FileStorage.all(self).keys():
print('** no instance found **')
return HBNBCommand.flag
def all_objects(self):
"""returns __objects dict variable from file_storage"""
return FileStorage.all(FileStorage)
def store_save(self):
"""invokes save() func from file_storage"""
FileStorage.save(FileStorage)
def store_reload(self):
"""invokes reload() func from file_storage"""
FileStorage.reload(FileStorage)
if __name__ == '__main__':
HBNBCommand().cmdloop()