-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.py
executable file
·152 lines (137 loc) · 4.93 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
#!/usr/bin/python3
""" Console module"""
import cmd
from models import storage
from models.base_model import BaseModel
from models.user import User
from models.amenity import Amenity
from models.city import City
from models.place import Place
from models.review import Review
from models.state import State
class HBNBCommand(cmd.Cmd):
"""Command interpreter class.
Attributes:
prompt : The command prompt.
"""
prompt = "(hbnb) "
__classes = {"BaseModel", "User", "City", "Place", "Review", "State", "Amenity"}
def emptyline(self):
"""Do nothing when the line is empty"""
pass
def do_quit(self, arg):
"""Quit command to exit the program"""
return True
def do_EOF(self, arg):
"""Exit the program with EOF (Ctrl + D)"""
print("")
return True
def do_create(self, arg):
"""Creates a new instance of BaseModel
and save it to json file.
"""
args = arg.split()
if len(args) == 0:
print("** class name missing **")
elif args[0] not in HBNBCommand.__classes:
print("** class doesn't exist **")
else:
new_instance = eval(arg)()
new_instance.save()
print(new_instance.id)
def do_show(self, arg):
"""
Prints the string representation
of an instance based on the class name and id.
"""
args = arg.split()
if len(args) == 0:
print("** class name missing **")
elif args[0] not in HBNBCommand.__classes:
print("** class doesn't exist **")
elif len(args) == 1:
print("** instance id missing **")
else:
key = "{}.{}".format(args[0], args[1])
instaces = storage.all()
if key not in instaces:
print("** no instance found **")
else:
print(instaces[key])
def do_destroy(self, arg):
"""
Deletes an instance based on the class name and id.
"""
args = arg.split()
if len(args) == 0:
print("** class name missing **")
elif args[0] not in HBNBCommand.__classes:
print("** class doesn't exist **")
elif len(args) == 1:
print("** instance id missing **")
else:
key = "{}.{}".format(args[0], args[1])
instaces = storage.all()
if key not in instaces.keys():
print("** no instance found **")
else:
del instaces[key]
storage.save()
def do_all(self, arg):
"""
Prints all string representation of allall
instances based or not on the class name.
"""
args = arg.split()
if len(args) > 0 and args[0] not in HBNBCommand.__classes:
print("** class doesn't exist **")
else:
listOfModels = []
for model in storage.all().values():
if len(args) > 0 and args[0] == model.__class__.__name__:
listOfModels.append(str(model))
elif len(args) == 0:
listOfModels.append(str(model))
print(listOfModels)
def do_update(self, arg):
"""
Updates an instance based on the class name and id
by adding or updating attribute.
"""
args = arg.split()
if len(args) == 0:
print("** class name missing **")
elif args[0] not in HBNBCommand.__classes:
print("** class does'nt exist **")
elif len(args) == 1:
print("** instace id missing **")
elif "{}.{}".format(args[0], args[1]) not in storage.all().keys():
print("** no instance found **")
elif len(args) == 2:
print("** attribute name missing **")
elif len(args) == 3:
try:
value = eval(args[2])
except NameError:
print("** value missing **")
else:
instance = storage.all()["{}.{}".format(args[0], args[1])]
attributeName = args[2]
value = args[3]
if attributeName not in ("id", "created_at", "updated_at"):
if attributeName in instance.__class__.__dict__:
attrType = type(instance.__class__.__dict__[attributeName])
try:
castedValue = attrType(value)
setattr(instance, attributeName, castedValue)
instance.save()
except ValueError:
print("** invalid value **")
else:
# Add the attribute to the class with the specified value
attrType = type(value)
setattr(instance.__class__, attributeName, attrType)
setattr(instance, attributeName, value)
instance.save()
if __name__ == "__main__":
HBNBCommand().cmdloop()