-
Notifications
You must be signed in to change notification settings - Fork 1
/
console.py
executable file
·182 lines (167 loc) · 5.76 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
"""testing module"""
import cmd
import sys
import re
from models import storage
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
class HBNBCommand(cmd.Cmd):
"""class handling command interpreter"""
classes = ["BaseModel", "User", "State",
"City", "Amenity", "Place", "Review"]
if (sys.stdin.isatty()):
prompt = '(hbnb)'
else:
prompt = '(hbnb)\n'
def default(self, arg):
custom_cmd = {
"all": self.do_all,
"show": self.do_show,
"destroy": self.do_destroy,
"update": self.do_update
}
if '.' not in arg:
print("*** Unknown syntax: ", arg)
return
args = arg.split('.')
args[1] = str.replace(args[1], '()', '')
if args[0] == "":
print("** class name missing **")
return
if args[1] in custom_cmd.keys():
return custom_cmd[args[1]]("{}".format(args[0]))
else:
print("*** Unknown syntax: ", arg)
def do_quit(self, line):
"""Quit - command to exit the program
"""
return True
def do_EOF(self, line):
"""EOF - command to exit the program
"""
return True
def emptyline(self):
"""Do nothing when the line is empty"""
pass
def do_create(self, line):
"""Create - Creates a new instance of BaseModel,
saves it (to the JSON file) and prints the id.
Ex: $ create BaseModel
"""
if (len(line) == 0):
print("** class name missing **")
elif line not in HBNBCommand.classes:
print("** class doesn't exist **")
else:
my_model = eval(line)()
my_model.save()
print(my_model.id)
def do_show(self, line):
"""Show - Prints the string representation of an instance
based on the class name and id.
Ex: $ show BaseModel 1234-1234-1234
"""
if (len(line) == 0):
print("** class name missing **")
else:
args = line.split()
if (args[0] not in HBNBCommand.classes):
print("** class doesn't exist **")
else:
if (len(args) == 1):
print("** instance id missing **")
else:
instance = args[0]+"."+args[1]
models = storage.all()
if (instance in models):
print(models[instance])
else:
print("** no instance found **")
def do_destroy(self, line):
"""Destroy - Deletes an instance based on the class name and
id (save the change into the JSON file).
Ex: $ destroy BaseModel 1234-1234-1234"""
if (len(line) == 0):
print("** class name missing **")
else:
args = line.split()
if (args[0] not in HBNBCommand.classes):
print("** class doesn't exist **")
else:
if (len(args) == 1):
print("** instance id missing **")
else:
instance = args[0]+"."+args[1]
models = storage.all()
if (instance in models):
del models[instance]
storage.save()
else:
print("** no instance found **")
def do_all(self, line):
"""All - Prints all string representation of all instances
based or not on the class name.
Ex: $ all BaseModel or $ all"""
print(line)
if (len(line) == 0):
models = storage.all()
list_models = []
for i in models.values():
list_models.append(str(i))
print(list_models)
elif line in HBNBCommand.classes:
models = storage.all()
list_models = []
for i in models.values():
if i.__class__.__name__ == line:
list_models.append(str(i))
print(list_models)
else:
print("** class doesn't exist **")
def do_update(self, line):
"""Update - Updates an instance based on the class name and id
by adding or updating attribute (save the change into the JSON file).
Ex: $ update BaseModel 1234-1234-1234 email "[email protected]" """
print("update")
if (len(line) == 0):
print("** class name missing **")
return
args = line.split()
print(args)
if (args[0] not in HBNBCommand.classes):
print("** class doesn't exist **")
return
if (len(args) == 1):
print("** instance id missing **")
return
instance = args[0]+"."+args[1]
objects = storage.all()
if (instance not in objects):
print("** no instance found **")
return
if (len(args) == 2):
print("** attribute name missing **")
return
if (len(args) == 3):
try:
type(eval(args[2])) != dict
except NameError:
print("** value missing **")
return
attr = args[2]
attr_array = line.split('"')
try:
value = getattr(objects[instance], attr)
mtype = type(value)
setattr(objects[instance], attr, mtype(attr_array[1]))
except Exception:
setattr(objects[instance], attr, attr_array[1])
storage.save()
if __name__ == '__main__':
HBNBCommand().cmdloop()