-
Notifications
You must be signed in to change notification settings - Fork 1
/
console.py
executable file
·163 lines (148 loc) · 4.88 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
#!/usr/bin/python3
"""
The console, to manage everything
"""
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):
"""Contains functionality of the console"""
intro = 'Welcome to the interpreter! Type help or ? to list commands.\n'
prompt = '(hbnb) '
classes = {
'BaseModel': BaseModel,
'User': User,
'State': State,
'City': City,
'Amenity': Amenity,
'Place': Place,
'Review': Review
}
def do_create(self, line):
"""Creates a new object"""
if len(line) == 0:
print("** class name missing **")
elif line not in self.__class__.classes.keys():
print("** class doesn't exist **")
else:
obj = self.__class__.classes[line]()
obj.save()
print(obj.id)
def do_show(self, line):
""" Prints the string representation of an instance
Args:
line -> Class id ( In that order)
"""
class_object = line.split(" ")
if len(line) == 0:
print("** class name missing **")
return
elif class_object[0] not in self.__class__.classes.keys():
print("** class doesn't exist **")
return
elif len(class_object) == 1:
print("** instance id missing **")
return
else:
key = class_object[0] + "." + class_object[1]
all_instances = storage.all()
if key not in all_instances.keys():
print("** no instance found **")
else:
obj = all_instances[key]
print(str(obj))
def do_update(self, line):
"""Updates attributes of an object"""
updates = line.split(" ")
if len(line) == 0:
print("** class name missing **")
return
elif updates[0] not in __class__.classes.keys():
print("** class doesn't exist **")
return
elif len(updates) == 1:
print("** instance id missing **")
return
elif len(updates) == 2:
print("** attribute name missing **")
elif len(updates) == 3:
print("** value missing **")
else:
key = updates[0] + "." + updates[1]
all_instances = storage.all()
if key not in all_instances.keys():
print("** no instance found **")
else:
obj = all_instances[key]
setattr(obj, updates[2], updates[3])
storage.save()
def do_destroy(self, args):
"""Destroys an object based on the Class Name and ID"""
target_list = args.split(" ")
if len(args) == 0:
print("** class name missing **")
return
elif target_list[0] not in self.__class__.classes.keys():
print("** class doesn't exist **")
return
elif len(target_list) == 1:
print("** instance id missing **")
return
else:
key = target_list[0] + "." + target_list[1]
all_instances = storage.all()
if key not in all_instances.keys():
print("** no instance found **")
else:
del all_instances[key]
storage.save()
def do_all(self, line):
"""Print string representation of all instances"""
obj_list = []
objs = storage.all()
try:
if len(line) != 0:
eval(line)
else:
pass
except NameError:
print("** class doesn't exist **")
return
line.strip()
for key, val in objs.items():
if len(line) != 0:
if type(val) is eval(line):
val = str(objs[key])
obj_list.append(val)
else:
val = str(objs[key])
obj_list.append(val)
print(obj_list)
def emptyline(self):
"""Overwrite default behavior to repeat last cmd"""
pass
def do_operations(self, args):
"""Do operations on objects"""
def do_EOF(self, arg):
""" Handles EOF to exit program """
print()
return True
def do_quit(self, args):
"""Quits the interpreter"""
raise SystemExit
def do_count(self, arg):
"""Count all instances of a class"""
to_count = args.split(" ")
instances = 0
for obj_ in models.storage.all().values():
if to_count[0] == type(obj_).__name__:
instances += 1
print(instances)
if __name__ == '__main__':
HBNBCommand().cmdloop()