-
Notifications
You must be signed in to change notification settings - Fork 1
/
console.py
executable file
·191 lines (165 loc) · 5.68 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
#!/usr/bin/python3
"""Contains the entry point of a command interpreter"""
import cmd
from datetime import datetime
import uuid
from models.base_model import BaseModel
from models import storage
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
class HBNBCommand(cmd.Cmd):
"""This is the class definition for the command interpreter"""
prompt = "(hbnb) "
classes = [
"BaseModel",
"User",
"State",
"City",
"Amenity",
"Place",
"Review"]
def do_EOF(self, line):
""" This is a method to exit the program"""
return True
def help_EOF(self):
""" This helps to quit the program """
print("This also quits program\n")
def do_quit(self, line):
"""This is a method to quit the program"""
return True
def help_quit(self):
"""Gives more information on the method quit"""
print("Quit command to exit the program\n")
def emptyline(self):
"""when line is empty print nothing"""
pass
print("Quit command to exit the program")
def do_create(self, line):
"""
Creates a new instance of BaseModel, saves it
(to the JSON file) and prints the id
"""
if line:
if line in type(self).classes:
new_instance = eval(line)()
new_instance.save()
print(new_instance.id)
else:
print("** class doesn't exist **")
else:
print("** class name missing **")
def do_show(self, line):
"""
Prints the string representation of an instance
based on the class name and id
"""
all_objs = storage.all()
if line:
line = line.split()
if len(line) == 1:
if line[0] in type(self).classes:
print("** instance id missing **")
else:
print("** class doesn't exist **")
elif len(line) >= 2:
name = line[0]
name_id = line[1]
obj_key = line[0] + "." + line[1]
if obj_key in all_objs.keys():
str_instance = all_objs[obj_key]
print(str_instance)
else:
print("** no instance found **")
else:
print("** class name missing **")
def do_destroy(self, line):
"""
Deletes an instance based on the class nameand
id (save the change into the JSON file)
"""
all_objs = storage.all()
if line:
line = line.split()
if len(line) == 1:
if line[0] in type(self).classes:
print("** instance id missing **")
else:
print("** class doesn't exist **")
elif len(line) >= 2:
name = line[0]
name_id = line[1]
obj_key = f"{line[0]}.{line[1]}"
if obj_key in all_objs.keys():
del all_objs[obj_key]
storage.save()
else:
print("** no instance found **")
else:
print("** class name missing **")
def do_all(self, line):
"""
Prints all string representation of all instances
based or not on the class name
"""
if line:
all_objs = storage.all()
line = line.split()
if len(line) == 1:
if line[0] in type(self).classes:
if all_objs:
for value in all_objs.values():
print(value)
else:
pass
else:
print("** class doesn't exist **")
else:
pass
else:
pass
def do_update(self, line):
"""
Updates an instance based on the class name and id by adding or
updating attribute (save the change into the JSON file)
Usage: update <class name> <id> <attribute name> "<attribute value>"
"""
all_objs = storage.all()
line = line.split()
if line:
if len(line) == 1:
if line[0] == type(self).classes:
print("** instance id missing **")
else:
print("** class doesn't exist **")
elif len(line) == 2:
key = line[0] + "." + line[1]
if key in all_objs.keys():
print("** attribute name missing **")
else:
print("** no instance found **")
elif len(line) == 3:
key = line[0] + "." + line[1]
if key in all_objs.keys():
print("** value missing **")
else:
print("** no instance found **")
elif len(line) >= 4:
key = line[0] + "." + line[1]
if key in all_objs.keys():
new_instance = all_objs[key].to_dict()
attribute_key = line[2]
attribute_value = line[3]
new_instance[attribute_key] = attribute_value
new_inst_obj = BaseModel(**new_instance)
new_inst_obj.save()
storage.new(new_inst_obj)
else:
print("** no instance found **")
else:
print("** class doesn't exist **")
if __name__ == '__main__':
HBNBCommand().cmdloop()