-
Notifications
You must be signed in to change notification settings - Fork 1
/
console.py
executable file
·208 lines (164 loc) · 5.1 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
#!/usr/bin/python3
"""
Contains the Console class for the project
"""
import cmd
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
from models import storage
import json
class HBNBCommand(cmd.Cmd):
"""The command interpreter
Attributes:
prompt(str): The prompt to use for the interpreter
"""
all_class = {
"BaseModel": BaseModel,
"User": User,
"State": State,
"City": City,
"Amenity": Amenity,
"Place": Place,
"Review": Review
}
prompt = '(hbnb) '
def do_quit(self, arg):
"""Quit command to exit the program"""
return True
def help(self, arg):
"""Help command"""
print("")
def do_EOF(self, arg):
"""CTRL-D command to exit the interpreter"""
print("")
return True
def emptyline(self):
"""continue on encounter with an empty line + ENTER"""
pass
def do_create(self, arg):
"""Create a new instance of models classes"""
cls_name = arg.split(" ")[0]
if not cls_name:
print("** class name missing **")
return
if cls_name not in HBNBCommand.all_class:
print("** class doesn't exist **")
return
obj = HBNBCommand.all_class[cls_name]()
obj.save()
print(obj.id)
def do_show(self, arg):
"""Print the string representation of an istance"""
arg_list = arg.split(" ")
cls_name = arg_list[0]
if not cls_name:
print("** class name missing **")
return
if cls_name not in HBNBCommand.all_class:
print("** class doesn't exist **")
return
try:
cls_id = arg_list[1]
except IndexError:
print("** instance id missing **")
return
objs = storage.all()
for k, v in objs.items():
if k.split(".")[1] == cls_id:
print(v)
return
print("** no instance found **")
def do_destroy(self, arg):
""" Deletes an instance based on the class name and id """
my_dict = {}
arg_list = arg.split(" ")
cls_name = arg_list[0]
if not cls_name:
print("** class name missing **")
return
if cls_name not in HBNBCommand.all_class:
print("** class doesn't exist **")
return
try:
cls_id = arg_list[1]
except IndexError:
print("** instance id missing **")
return
objs = storage.all()
key = "{}.{}".format(cls_name, cls_id)
try:
del objs[key]
storage.save()
except KeyError:
print("** no instance found **")
def do_all(self, arg):
"""Prints all string representation of all instances
based or not on the class name"""
objs_list = []
my_dict = storage.all()
if len(arg) == 0:
for v in my_dict.values():
v = str(v)
objs_list.append(v)
else:
arg_list = arg.split(" ")
cls_name = arg_list[0]
if cls_name not in HBNBCommand.all_class:
print("** class doesn't exist **")
return
for k, v in my_dict.items():
if k.split(".")[0] == cls_name:
v = str(v)
objs_list.append(v)
print(json.dumps(objs_list))
def do_update(self, arg):
"""Update or add an attribute to an instance"""
arg_list = arg.split(" ")
cls_name = arg_list[0]
if not cls_name:
print("** class name missing **")
return
if cls_name not in HBNBCommand.all_class:
print("** class doesn't exist **")
return
try:
cls_id = arg_list[1]
except IndexError:
print("** instance id missing **")
return
try:
attr_name = arg_list[2]
except IndexError:
print("** attribute name missing **")
return
if len(arg_list) < 4:
print("** value missing **")
return
val = arg_list[3]
if val.startswith("\""):
i = 4
val = val.replace("\"", "")
while True:
try:
endwith = arg_list[i][-1]
val = val + " " + arg_list[i].replace("\"", "")
if endwith == "\"":
break
except IndexError:
break
i += 1
my_dict = storage.all()
key = cls_name + "." + cls_id
if key in my_dict:
obj = my_dict[key]
setattr(obj, attr_name, val)
obj.save()
else:
print("** no instance found **")
if __name__ == '__main__':
HBNBCommand().cmdloop()