-
Notifications
You must be signed in to change notification settings - Fork 1
/
console.py
executable file
·211 lines (167 loc) · 5.79 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/python3
"""Console"""
from models.base_model import BaseModel
from models import storage
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
import cmd
import json
class HBNBCommand(cmd.Cmd):
"""Hbnb console"""
prompt = "(hbnb) "
class_names = ["BaseModel", "User", "State",
"City", "Amenity", "Place", "Review"]
def do_quit(self, line):
return True
def help_quit(self):
print("Quit command to exit the program")
def do_EOF(self, line):
return True
def emptyline(self):
pass
def do_create(self, line):
if line != "":
try:
new_cls = eval(line.split()[0])()
new_cls.save()
print(new_cls.id)
except Exception:
print("** class doesn't exist **")
else:
print("** class name missing **")
def do_show(self, line):
if not line:
print("** class name missing **")
return
parts = line.split()
if parts[0] not in self.class_names:
print("** class doesn't exist **")
return
if len(parts) < 2:
print("** instance id missing **")
return
class_name, instance_id = parts[0], parts[1]
try:
with open("file.json", "r") as f:
data = json.load(f)
except FileNotFoundError:
data = {}
key_to_show = "{}.{}".format(class_name, instance_id)
if key_to_show not in data:
print("** no instance found **")
return
value = eval(key_to_show.split(".")[0])(**data[key_to_show])
print(value)
def do_destroy(self, line):
if not line:
print("** class name missing **")
return
parts = line.split()
if parts[0] not in self.class_names:
print("** class doesn't exist **")
return
if len(parts) < 2:
print("** instance id missing **")
return
class_name, instance_id = parts[0], parts[1]
try:
with open("file.json", "r") as f:
data = json.load(f)
except FileNotFoundError:
data = {}
key_to_delete = "{}.{}".format(class_name, instance_id)
if key_to_delete not in data:
print("** no instance found **")
return
del data[key_to_delete]
with open("file.json", "w") as f:
json.dump(data, f)
def do_all(self, line):
if not line:
try:
with open("file.json", "r") as f:
data = json.load(f)
except FileNotFoundError:
data = {}
arr = []
for k in data.keys():
value = eval(k.split(".")[0])(**data[k])
arr.append(value.__str__())
print(arr)
else:
try:
with open("file.json", "r") as f:
data = json.load(f)
except FileNotFoundError:
data = {}
arr = []
if line not in self.class_names:
print("** class doesn't exist **")
return
for k in data.keys():
if (k.split(".")[0] == line):
value = eval(k.split(".")[0])(**data[k])
arr.append(value.__str__())
print(arr)
def do_update(self, line):
if not line:
print("** class name missing **")
return
parts = line.split()
if parts[0] not in self.class_names:
print("** class doesn't exist **")
return
if len(parts) < 2:
print("** instance id missing **")
return
if len(parts) < 3:
print("** attribute name missing **")
return
if len(parts) < 4:
print("** value missing **")
return
class_name, id_val, attribute, value = (parts[0], parts[1],
parts[2], parts[3])
try:
with open("file.json", "r") as f:
data = json.load(f)
except FileNotFoundError:
data = {}
key_for_find = "{}.{}".format(class_name, id_val)
if key_for_find not in data:
print("** no instance found **")
return
obj = eval(key_for_find.split(".")[0])(**data[key_for_find])
setattr(obj, attribute, value.strip('"'))
data[key_for_find] = obj.to_dict()
with open("file.json", "w") as f:
json.dump(data, f)
def default(self, line):
tokens = line.split('.')
if len(tokens) == 2:
class_name = tokens[0]
if class_name in self.class_names:
if tokens[1] == "all()":
self.do_all(class_name)
elif tokens[1] == "count()":
count = 0
for k in storage.all().keys():
if (class_name == k.split(".")[0]):
count += 1
print(count)
elif tokens[1].startswith("show"):
instance_id = tokens[1].split("(")[1].split(")")[0]
self.do_show(tokens[0] + " " + instance_id)
elif tokens[1].startswith("destroy"):
instance_id = tokens[1].split("(")[1].split(")")[0]
self.do_destroy(tokens[0] + " " + instance_id)
else:
print("** class doesn't exist **")
else:
print("*** Unknown syntax:", line)
if __name__ == '__main__':
HBNBCommand().cmdloop()