-
Notifications
You must be signed in to change notification settings - Fork 1
/
console.py
executable file
·182 lines (139 loc) · 4.71 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
#!/usr/bin/python3
"""Program that contains the entry point of the command interpreter"""
import models
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
class HBNBCommand(cmd.Cmd):
prompt = "(hbnb) "
classes = {"BaseModel": BaseModel, "Amenity": Amenity, "User": User}
clss_1 = {"State": State, "City": City, "Review": Review, "Place": Place}
classes.update(clss_1)
def do_quit(self, arg):
"""Quit command to exit the program"""
return True
def do_EOF(self, arg):
"""EOF command to exit the program"""
return True
def emptyline(self):
"""Called when an empty line is entered in response to the prompt."""
pass
def do_create(self, arg):
"""Creates a new instance of BaseModel"""
arg = arg.split()
if len(arg) == 0:
print("** class name missing **")
return
elif arg[0] in HBNBCommand.classes:
newobject = HBNBCommand.classes[arg[0]]()
else:
print("** class doesn't exist **")
return
newobject.save()
print(newobject.id)
def do_show(self, arg):
"""Prints the string representation of an instance"""
arg = arg.split()
if len(arg) == 0:
print("** class name missing **")
return
elif arg[0] not in HBNBCommand.classes:
print("** class doesn't exist **")
return
elif len(arg) == 1:
print("** instance id missing **")
return
else:
bool = False
compare = f"{arg[0]}.{arg[1]}"
all_objs = storage.all()
for key in all_objs.keys():
if compare == key:
bool = True
print(all_objs[key])
break
if bool is False:
print("** no instance found **")
def do_destroy(self, arg):
"""Deletes an instance based on the class name and id"""
arg = arg.split()
if len(arg) == 0:
print("** class name missing **")
return
elif arg[0] not in HBNBCommand.classes:
print("** class doesn't exist **")
return
elif len(arg) == 1:
print("** instance id missing **")
return
else:
bool = False
compare = f"{arg[0]}.{arg[1]}"
for key in storage.all():
if compare == key:
bool = True
storage.all().pop(key)
storage.save()
break
if bool is False:
print("** no instance found **")
def do_all(self, arg):
"""Prints string representation of all instances
based or not on the class name."""
arg = arg.split()
list = []
all_objs = storage.all()
if len(arg) == 0:
for key, value in all_objs.items():
list.append(str(value))
print(list)
elif len(arg) == 1:
if arg[0] in HBNBCommand.classes:
for key, value in all_objs.items():
if arg[0] in key:
list.append(str(value))
print(list)
else:
print("** class doesn't exist **")
def do_update(self, arg):
"""Updates an instance based on the class name and id"""
arg = arg.split()
if len(arg) == 0:
print("** class name missing **")
return
elif arg[0] not in HBNBCommand.classes:
print("** class doesn't exist **")
return
elif len(arg) == 1:
print("** instance id missing **")
return
compare = f"{arg[0]}.{arg[1]}"
if compare in storage.all().keys():
if len(arg) == 2:
print("** attribute name missing **")
return
if len(arg) == 3:
print("** value missing **")
return
new_instance = storage.all()[compare]
setattr(new_instance, arg[2], arg[3].strip('"'))
storage.save()
else:
print("** no instance found **")
return
def precmd(self, arg):
if "." in arg:
args = arg.split(".")
clase = args[0]
args[1] = args[1].strip("()")
command = args[1] + " " + clase
return command
return arg
if __name__ == '__main__':
HBNBCommand().cmdloop()