-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsole.py
executable file
·222 lines (175 loc) · 5.48 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
#!/usr/bin/python3
"""A program that uses the cmd module"""
import cmd
from models.amenity import Amenity
from models.base_model import BaseModel
from models.city import City
from models.place import Place
from models.review import Review
from models.state import State
from models.user import User
from models import storage
import shlex
CLASSES = {
"Amenity": Amenity,
"City": City,
"Place": Place,
"Review": Review,
"State": State,
"User": User,
"BaseModel": BaseModel
}
class HBNBCommand(cmd.Cmd):
"""
A program that uses the cmd module
Attributes:
prompt: custom prompt
Methods:
do_quit(self, arg):
Quit command to exit the program
do_EOF(self, arg):
EOF command to exit the program
help_help(self):
Shows help information about commands
emptyline(self):
empty line
"""
prompt = "(hbnb) "
def do_quit(self, arg):
"""Quit command to exit the program"""
return True
def do_EOF(self, arg):
"""EOF command to exit the program"""
print()
return True
def help_help(self):
"""Help section info about all commands"""
print("Shows help information about commands")
def emptyline(self):
"""An empty line + ENTER shouldn’t execute anything"""
pass
def do_create(self, arg):
"""Create a new instance of BaseModel,
saves it (to the JSON file) and prints the id"""
args = shlex.split(arg)
if len(args) < 1:
print("** class name missing **")
return
class_name = args[0]
if class_name not in CLASSES:
print("** class doesn't exist **")
return
else:
model = CLASSES[class_name]()
model.save()
print(model.id)
def do_show(self, arg):
"""
Prints the string representation of an instance based
on the class name and id.
:param arg:
:return:
"""
args = arg.split()
if len(args) < 1:
print("** class name missing **")
return
class_name = args[0]
if class_name not in CLASSES:
print("** class doesn't exist **")
return
if len(args) < 2:
print("** instance id missing **")
return
instance_id = args[1]
instance_key = "{}.{}".format(class_name, instance_id)
instance = storage.all().get(instance_key)
if not instance:
print("** no instance found **")
return
print(instance)
def do_all(self, arg):
"""
Prints string representation of all instances based on the class name.
If no class name is provided, it prints all instances.
:param arg:
:return:
"""
args = arg.split()
all_objs = storage.all()
if len(args) > 0 and args[0] not in CLASSES.keys():
print("** class doesn't exist **")
return
instances = list(all_objs.values()) if \
len(args) < 1 else [v for v in all_objs.values() if
type(v).__name__ == args[0]]
print([str(obj) for obj in instances])
return
def do_update(self, arg):
"""
Updates an instance based on the class name and id
by adding or updating attribute.
:param arg:
:return:
"""
args = shlex.split(arg)
if len(args) < 1:
print("** class name missing **")
return
class_name = args[0]
if class_name not in CLASSES:
print("** class doesn't exist **")
return
if len(args) < 2:
print("** instance id missing **")
return
instance_id = args[1]
instance_key = "{}.{}".format(class_name, instance_id)
instance = storage.all().get(instance_key)
if not instance:
print("** no instance found **")
return
if len(args) < 3:
print("** attribute name missing **")
return
attribute_name = args[2]
if len(args) < 4:
print("** value missing **")
return
attribute_value = args[3].strip('"')
# Check that the attribute value is a simple type
try:
if type(eval(attribute_value)) not in [str, int, float]:
return False
except (NameError, TypeError, ValueError):
return False
setattr(instance, attribute_name, attribute_value)
instance.save()
def do_destroy(self, arg):
"""
Deletes an instance based on the class name and id.
The change is saved into the JSON file.
:param arg:
:return:
"""
args = shlex.split(arg)
if len(args) < 1:
print("** class name missing **")
return
class_name = args[0]
if class_name not in CLASSES:
print("** class doesn't exist **")
return
if len(args) < 2:
print("** instance id missing **")
return
instance_id = args[1]
instance_key = "{}.{}".format(class_name, instance_id)
instance = storage.all().get(instance_key)
if not instance:
print("** no instance found **")
return
del storage.all()[instance_key]
storage.save()
if __name__ == '__main__':
HBNBCommand().cmdloop()