-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconsole.py
executable file
·221 lines (201 loc) · 6.6 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
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/python3
'''
the entry point of the command interpreter
'''
import cmd
import models
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
import sys
import inspect
class HBNBCommand(cmd.Cmd):
'''
command line interpreter class
'''
prompt = '(hbnb)'
def get_classes(self):
all_classes = []
for name, obj in inspect.getmembers(sys.modules[__name__]):
if inspect.isclass(obj):
all_classes.append("{}".format(obj.__name__))
return all_classes
def emptyline(self):
pass
def do_EOF(self, arg):
'''
quits the command interpreter
'''
return True
def do_quit(self, arg):
'''
quits the command interpreter
'''
return True
def do_create(self, args):
'''
Creates a new instance of BaseModel,
saves it (to the JSON file) and prints the id
'''
if not args:
print('** class name missing **')
return False
if args not in globals():
print("** class doesn't exist **")
return False
Class = globals()[args]
new_obj = Class()
new_obj.save()
print(new_obj.id)
def do_show(self, arg):
'''
Prints the string representation of an instance based on
the class name and id
Usage:
show <BaseModel> <9bffd505-c163-40ce-a210-c9dd1cda9409>
'''
if arg:
args = arg.split(' ')
if len(args) != 2:
if not args[0] in globals():
print("** class doesn't exist **")
return False
print('** instance id missing **')
return False
Class = globals()[args[0]]
key = ".".join(args)
objects = models.storage.all()
if key not in objects:
print('** no instance found **')
return False
new_obj = Class(**objects[key])
print(new_obj)
else:
print('** class name missing **')
return False
def do_destroy(self, arg):
'''
Deletes an instance based on the class name and id
(save the change into the JSON file).
Usage:
$ destroy BaseModel 1234-1234-1234
'''
if not arg:
print('** class name missing **')
return False
args = arg.split()
if args[0] not in globals():
print('** class doesn\'t exist **')
return False
if len(args) != 2:
print('** instance id missing **')
return False
Class = globals()[args[0]]
key = ".".join(args)
objs = models.storage.all()
if key not in objs:
print("** no instance found **")
return False
del objs[key]
models.storage.save()
def do_all(self, args):
'''
Prints all string representation of all instances
based or not on the class name.
Usage:
(hbnb)all BaseModel or (hbnb)all
'''
str_list = []
if len(args) == 0:
all_dict = models.storage.all()
for id in all_dict.keys():
obj = all_dict[id]
idx = id.split('.')
Class = globals()[idx[0]]
str_list.append("{}".format(Class(**obj)))
print(str_list)
else:
models.storage.reload()
args = args.split()
if args[0] in globals():
all_dict = models.storage.all()
Class = globals()[args[0]]
for id in all_dict.keys():
if args[0] in id:
obj = Class(**all_dict[id])
str_list.append("{}".format(obj))
print(str_list)
else:
print("** class doesn't exist **")
return False
def do_update(self, args):
'''
Updates an instance based on the class name and id
by adding or updating attribute
Usage:
update <class name> <id> <attribute name> "<attribute value>"
'''
if len(args) == 0:
print("** class name missing **")
return False
args = args.split()
if args[0] not in globals():
print("** class doesn't exist **")
return False
Class = globals()[args[0]]
if len(args) < 2:
print("** instance id missing **")
return False
all_objects = models.storage.all()
key = ".".join([args[i] for i in range(2)])
if key not in all_objects:
print("** no instance found **")
return False
if len(args) < 3:
print("** attribute name missing **")
return False
if len(args) < 4:
print("** value missing **")
return False
all_objects[key][args[2]] = args[3]
new_obj = Class(**all_objects[key])
new_obj.save()
def onecmd(self, line):
"""Interpret the argument as though it had been typed in response
to the prompt.
Checks whether this line is typed at the normal prompt or in
a breakpoint command list definition.
"""
try:
classname, command = line.split('.')
if classname not in globals():
return cmd.Cmd.onecmd(self, line)
else:
if command == 'all()':
self.do_all(classname)
return
elif command == 'count()':
counter = 0
all_objs = models.storage.all()
for k in all_objs.keys():
key = k.split('.')
if key[0] == classname:
counter += 1
print(counter)
return
else:
raw = command[command.find('(')+1:command.find(')')]
raw = raw.split(', ')
id = raw[0][1:-1]
c = command[0: command.find('(')]
cm = "{} {} {} {}".format(c, classname, id.replace('"', ''),
" ".join(raw[1:]))
return cmd.Cmd.onecmd(self, cm)
except:
return cmd.Cmd.onecmd(self, line)
if __name__ == '__main__':
HBNBCommand().cmdloop()