forked from jemn21819/AirBnB_clone
/
console.py
executable file
·214 lines (190 loc) · 6.86 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
#!/usr/bin/python3
"""
This is the Air bnb clone Console. It works to navigate the
Air bnb Environmet.
Much like a shell.
"""
import re
import cmd
import models
from datetime import datetime
def pattern(arg):
""" Changing the default input function to manage callable functions """
pattern = '\.([^.]+)\(|([^(),]+)[,\s()]*[,\s()]*'
argum = re.findall(pattern, arg)
cmd = argum[0][0]
argum = argum[1:]
line = ' '.join(map(lambda x: x[1].strip('"'), argum))
return cmd, line
def loop_dict(line, obj_update):
""" looping function for the adv tasks"""
idx = 4
while idx <= len(line):
try:
attr = line[idx]
except IndexError:
print("** attribute name missing **")
else:
try:
val = line[idx + 1]
except IndexError:
print("** no value found **")
else:
setattr(obj_update, attr, val)
obj_update.save()
if idx + 1 == len(line) - 1:
break
idx += 1
class HBNBCommand(cmd.Cmd):
""" Base Command file class """
prompt = '(hbnb) '
def do_create(self, argv):
""" Creates a new object based on BaseModel """
if len(argv) == 0:
print("** class name missing **")
else:
try:
cls = models.class_dict[argv]
except KeyError:
print("** class doesn't exist **")
else:
new_obj = cls()
new_obj.save()
print(new_obj.id)
def do_show(self, argv):
""" Prints the string representation of an instance
given the class name and id """
if len(argv) == 0:
print("** class name missing **")
else:
line = argv.split()
if line[0] in models.class_dict:
try:
key = line[0] + '.' + line[1]
except IndexError:
print("** instance id missing **")
else:
try:
print(models.storage.all()[key])
except KeyError:
print("** no instance found **")
else:
print("** class doesn't exist **")
def do_destroy(self, argv):
""" Destroys an instances given the class name & id """
if len(argv) == 0:
return print("** class name missing **")
else:
line = argv.split()
if line[0] in models.class_dict:
try:
key = line[0] + '.' + line[1]
except IndexError:
print("** instance id missing **")
else:
try:
del models.storage.all()[key]
except KeyError:
print("** no instance found **")
else:
models.storage.save()
else:
print("** class doesn't exist **")
def do_all(self, line):
"""rints all string representation of all instances
based or not on the class name"""
if len(line) == 0:
print([str(v) for v in models.storage.all().values()])
elif line not in models.class_dict:
print("** class doesn't exist **")
else:
print([str(v) for k, v in models.storage.all().items()
if line in k])
def do_update(self, line):
"""pdates an instance based on the class name and id by
adding or updating attribute"""
if len(line) == 0:
print("** class name missing **")
else:
line = line.split(' ')
for i in range(len(line)):
line[i] = line[i].strip("\"'\"{\"}:\"'")
if line[0] in models.class_dict:
try:
obj_id = line[0] + '.' + line[1]
except IndexError:
print("** instance id missing **")
else:
try:
obj = models.storage.all()[obj_id]
except KeyError:
print("** no instance found **")
else:
try:
attr = line[2]
except IndexError:
print("** attribute name missing **")
else:
try:
val = line[3]
except IndexError:
print("** value missing **")
else:
setattr(obj, attr, val)
obj.save()
if len(line) >= 5:
loop_dict(line, obj)
else:
print("** class doesn't exist **")
def do_count(self, line):
""" Counts the number of instances of a class """
instance_cnt = 0
curr_dict = models.storage.all()
for key, val in curr_dict.items():
val = val.to_dict()
if val['__class__'] == line:
instance_cnt += 1
print(instance_cnt)
def do_Amenity(self, arg):
""" helper function for amenity class """
cmd, line = pattern(arg)
self.onecmd(' '.join([cmd, 'Amenity', line]))
def do_User(self, arg):
""" Helper function for User class """
cmd, line = pattern(arg)
self.onecmd(' '.join([cmd, 'User', line]))
def do_BaseModel(self, arg):
""" Helper function for BaseModel Class """
cmd, line = pattern(arg)
self.onecmd(' '.join([cmd, 'BaseModel', line]))
def do_City(self, arg):
""" Helper function for BaseModel Class """
cmd, line = pattern(arg)
self.onecmd(' '.join([cmd, 'City', line]))
def do_Review(self, arg):
""" Helper function for Review class """
cmd, line = pattern(arg)
self.onecmd(' '.join([cmd, 'Review', line]))
def do_State(self, arg):
""" Helper function for State class """
cmd, line = pattern(arg)
self.onecmd(' '.join([cmd, 'State', line]))
def do_Place(self, arg):
""" Helper function for Place class"""
cmd, line = pattern(arg)
self.onecmd(' '.join([cmd, 'Place', line]))
def emptyline(self):
""" Does nothing on (empty line + 'Enter') """
pass
def do_quit(self, line):
""" --- quit help documentation ---
The quit function closes the console gracefully """
return True
def do_EOF(self, line):
""" --- EOF help documentation ---
EOF force closes the console.
Use (Ctrl + D) to force close the console. """
print()
return True
if __name__ == '__main__':
HBNBCommand().cmdloop()