-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsole.py
executable file
·147 lines (125 loc) · 4.3 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
#!/usr/bin/python3
"""
HBNB Command Module
"""
import cmd
import json
from models.base_model import BaseModel
from models import storage
from models.user import User
from models.amenity import Amenity
from models.place import Place
from models.review import Review
from models.state import State
from models.city import City
class HBNBCommand(cmd.Cmd):
"""
HBNB command console class
"""
prompt = "(hbnb) "
__classes = ["BaseModel", "User", "Amenity",
"Place", "Review", "State", "City"]
def do_EOF(self, line):
"""Quit command to exit the program"""
return True
def do_quit(self, line):
"""Quit command to exit the program\n"""
return True
def emptyline(self):
"""function checks for empty line"""
pass
def do_create(self, line):
"""Creates a new instance of BaseModel, saves it and prints the id"""
arguments = line.split()
if len(arguments) == 0:
print("** class name missing **")
elif arguments[0] not in self.__classes:
print("** class doesn't exist **")
else:
#cls = globals()[arguments[0]]
#new_cls = cls()
#storage.save()
new_instance = eval(arguments[0])()
new_instance.save()
print(new_instance.id)
def do_show(self, line):
"""Prints the string representation of an instance
based on the class name and id"""
arguments = line.split()
if len(arguments) == 0:
print("** class name missing **")
elif arguments[0] not in self.__classes:
print("** class doesn't exist **")
elif len(arguments) < 2:
print("** instance id missing **")
else:
objs = storage.all()
key = "{}.{}".format(arguments[0], arguments[1])
if key in objs:
print(objs[key])
else:
print("** no instance found **")
def do_destroy(self, line):
"""Delete an instance based on the class name and id"""
arguments = line.split()
if len(arguments) == 0:
print("** class name missing **")
elif arguments[0] not in self.__classes:
print("** class doesn't exist **")
elif len(arguments) < 2:
print("** instance id missing **")
else:
objs = storage.all()
key = "{}.{}".format(arguments[0], arguments[1])
if key in objs:
del objs[key]
storage.save()
else:
print("** no instance found **")
def do_all(self, line):
"""
Print the string representation of all
instances or a specific class
"""
objj = []
arg = line.split()
if len(arg) > 0 and arg[0] not in self.__classes:
print("** class doesn't exist **")
else:
objj = []
for obj in storage.all().values():
if len(arg) > 0 and arg[0] == obj.__class__.__name__:
objj.append(obj.__str__())
elif len(arg) == 0:
objj.append(obj.__str__())
print(objj)
def do_update(self, line):
"""Update an instance by adding or updating an attribute"""
arguments = line.split()
if len(arguments) == 0:
print("** class name missing **")
elif arguments[0] not in self.__classes:
print("** class doesn't exist **")
elif len(arguments) < 2:
print("** instance id missing **")
else:
objs = storage.all()
key = "{}.{}".format(arguments[0], arguments[1])
if key not in objs:
print("** no instance found **")
elif len(arguments) < 3:
print("** attribute name missing **")
elif len(arguments) < 4:
print("** value missing **")
else:
obj = objs[key]
attr_name = arguments[2]
attr_value = arguments[3]
try:
attr_value = eval(attr_value)
except Exception:
pass
setattr(obj, attr_name, attr_value)
obj.save()
if __name__ == '__main__':
HBNBCommand().cmdloop()