-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsole.py
executable file
·175 lines (157 loc) · 5.34 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
#!/usr/bin/python3
"""Module contains the entry point of the command interpreter"""
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 cmd
class HBNBCommand(cmd.Cmd):
"""a class to the command interpreter"""
prompt = '(hbnb) '
__cls = [
"BaseModel",
"User",
"State",
"City",
"Amenity",
"Place",
"Review"
]
def do_quit(self, line):
"""Quit command to exit the program"""
return True
def do_EOF(self, line):
"""EOF signal to exit the program"""
return True
def emptyline(self):
"""Handles emplyline"""
pass
def do_create(self, line):
"""
Creates a new instance of BaseModel,
saves it (to the JSON file) and prints the id
"""
line = line.split()
if len(line) == 0:
print("** class name missing **")
elif line[0] not in self.__cls:
print("** class doesn't exist **")
else:
new_instance = eval(f"{line[0]}()")
print(new_instance.id)
storage.save()
def do_show(self, line):
"""
Prints the string representation of an instance based
on the class name and id
"""
line = line.split()
if len(line) == 0:
print("** class name missing **")
elif line[0] not in self.__cls:
print("** class doesn't exist **")
elif len(line) < 2:
print("** instance id missing **")
elif f"{line[0]}.{line[1]}" not in storage.all():
print("** no instance found **")
else:
print(storage.all()[f"{line[0]}.{line[1]}"])
def do_destroy(self, line):
"""
Deletes an instance based on the class name
and id (save the change into the JSON file)
"""
line = line.split()
if len(line) == 0:
print("** class name missing **")
elif line[0] not in self.__cls:
print("** class doesn't exist **")
elif len(line) < 2:
print("** instance id missing **")
elif f"{line[0]}.{line[1]}" not in storage.all():
print("** !no instance found **")
else:
for key in storage.all():
if key == f"{line[0]}.{line[1]}":
del storage.all()[key]
break
storage.save()
def do_all(self, line):
"""
Prints all string representation of all instances
based or not on the class name
"""
line = line.split()
if len(line) == 0:
new_list = [str(value) for value in storage.all().values()]
print(new_list)
elif line[0] not in self.__cls:
print("** class doesn't exist **")
else:
new_list = []
for key, value in storage.all().items():
cls_name, cls_id = key.split(".")
if line[0] != cls_name:
continue
new_list.append(str(value))
print(new_list)
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)
"""
line = line.split()
if len(line) == 0:
print("** class name missing **")
elif line[0] not in self.__cls:
print("** class doesn't exist **")
elif len(line) < 2:
print("** instance id missing **")
elif f"{line[0]}.{line[1]}" not in storage.all():
print("** no instance found **")
elif len(line) < 3:
print("** attribute name missing **")
elif len(line) < 4:
print("** value missing **")
else:
obj_cls = line[0]
obj_id = line[1]
obj_key = f"{obj_cls}.{obj_id}"
obj = storage.all()[obj_key]
attr_name = line[2]
attr_val = line[3]
if attr_val[0] == '"':
attr_val = attr_val[1:-1]
if hasattr(obj, attr_name):
value_type = type(getattr(obj, attr_name))
if value_type in [str, int, float]:
attr_val = value_type(attr_val)
setattr(obj, attr_name, attr_val)
else:
setattr(obj, attr_name, attr_val)
storage.save()
def default(self, line):
"""
Retrieve all instances of a class
usage: <class name>.all()
Retrieve the number of instances of a class
Usage: <class name>.count()
"""
cls_name, command = line.split(".")
if cls_name in self.__cls:
if command in "all()":
return self.do_all(cls_name)
elif command == "count()":
new_list = []
for key, value in storage.all().items():
key = key.split(".")
if key[0] != cls_name:
continue
new_list.append(str(value))
print(len(new_list))
if __name__ == '__main__':
HBNBCommand().cmdloop()