-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.py
executable file
·288 lines (257 loc) · 9.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/python3
"""file contains code for console program"""
import re
import sys
import ast
import cmd
import shlex
from models import storage
from models.base_model import BaseModel
from models.user import User
from models.city import City
from models.amenity import Amenity
from models.state import State
from models.place import Place
from models.review import Review
from datetime import datetime
class HBNBCommand(cmd.Cmd):
'''contains the entry point of the command interpreter'''
prompt = '(hbnb) ' if sys.__stdin__.isatty() else ''
classes = {"BaseModel": BaseModel, "User": User, "Place": Place,
"City": City, "State": State, "Amenity": Amenity,
"Review": Review}
dot_cmds = ['all', 'count', 'show', 'destroy', 'update']
def preloop(self):
"""Prints is isatty false"""
if not sys.__stdin__.isatty():
print('(hbnb) ', end='')
def precmd(self, line):
"""Reformat command line for advanced command syntax.
Usage: <class name>.<command>([<id> [<*args> or <**kwargs>]])
(Brackets denote optional fields in usage example.)
"""
_cmd = _cls = _id = _args = '' # initialize line elements
# scan for general formating - i.e '.', '(', ')'
if not ('.' in line and '(' in line and ')' in line):
return line
try: # parse line left to right
pline = line[:] # parsed line
# isolate <class name>
_cls = pline[:pline.find('.')]
# isolate and validate <command>
_cmd = pline[pline.find('.') + 1:pline.find('(')]
if _cmd not in HBNBCommand.dot_cmds:
raise Exception
# if parantheses contain arguments, parse them
pline = pline[pline.find('(') + 1:pline.find(')')]
if pline:
# partition args: (<id>, [<delim>], [<*args>])
pline = pline.partition(', ') # pline convert to tuple
# isolate _id, stripping quotes
_id = pline[0].replace('\"', '')
# possible bug here:
# empty quotes register as empty _id when replaced
# if arguments exist beyond _id
pline = pline[2].strip() # pline is now str
if pline:
# check for *args or **kwargs
if pline[0] == '{' and pline[-1] == '}'\
and type(eval(pline)) is dict:
_args = pline
else:
_args = pline.replace(',', '')
# _args = _args.replace('\"', '')
line = ' '.join([_cmd, _cls, _id, _args])
except Exception as mess:
pass
finally:
return line
def postcmd(self, stop, line):
"""Prints if isatty is false"""
if not sys.__stdin__.isatty():
print('(hbnb) ', end='')
return stop
def do_quit(self, arg):
"""Quit command to exit the program\n"""
return True
def do_EOF(self, arg):
"""Command to also exit the program\n"""
print()
exit()
return True
def emptyline(self):
"""does nothing when no command is given\n"""
return False
def do_create(self, arg):
"""creates a new instance of BaseModel, saves it to the JSON file
and prints the id"""
integers = ["number_rooms", "number_bathrooms", "max_guest",
"price_by_night"]
floats = ["latitude", "longitude"]
try:
if not arg:
raise SynaxError()
arg_list = arg.split(" ")
params = {}
for args in arg_list[1:]:
arg_attr = args.split("=")
arg_attr[1] = eval(arg_attr[1])
if type(arg_attr[1]) is str:
arg_attr[1] = (arg_attr[1].
replace("_", " ").replace("\"", '\\"'))
elif arg_attr[0] in integers:
arg_attr[1] = int(arg_attr[1])
elif arg_attr[0] in floats:
arg_attr[1] = float(arg_attr[1])
params[arg_attr[0]] = arg_attr[1]
except SyntaxError:
print("** class name missing **")
except NameError:
print("** class does not exist **")
new_obj = HBNBCommand.classes[arg_list[0]](**params)
storage.new(new_obj)
new_obj.save()
print(new_obj.id)
def do_show(self, arg):
"""prints the string representation of an instance based on
the classname and id"""
args = arg.split() # splits the argument using whitespace
if len(args) < 1:
print("** class name missing **")
elif args[0] not in HBNBCommand.classes:
print("** class doesn't exist **")
elif len(args) < 2:
print("** instance id missing **")
elif len(args) > 1:
key = args[0] + "." + args[1]
for obj in storage.all().keys():
if obj == key:
print(storage.all()[key])
break
if obj != key:
print("** no instance found **")
def count(self, line):
"""
This counts the instances of a certain class
"""
cmd_list = line.split('.')
class_name = cmd_list[0]
if class_name not in HBNBCommand.classes:
raise NameError("** class doesn't exist **")
else:
counts = 0
for key in storage.all():
keyval = key.split(".")[0]
if keyval == class_name:
counts += 1
print(counts)
def do_destroy(self, arg):
"""Deletes an instance based on the class name and id
(save the change into the JSON file)"""
if not arg:
print("** class name missing **")
return
argv = arg.split() # splits arguments using whitespace as seperator
if argv[0] not in HBNBCommand.classes:
print("** class doesn't exist **")
return
elif len(arg) < 2:
print("** instance id missing **")
return
else:
try:
key = argv[0] + "." + argv[1]
storage.all().pop(key)
storage.save()
except Exception:
print("** no instance found **")
finally:
return
def default(self, line):
"""
This takes care of all self defined functions
"""
inst_ids = re.search(r'\d[a-z0-9-]+', line)
if inst_ids:
inst_id = inst_ids.group(0)
cmd_list = line.split(".")
if len(cmd_list) == 2:
class_name = cmd_list[0]
if class_name in HBNBCommand.classes:
if cmd_list[1] == "count()":
self.count(class_name)
elif cmd_list[1] == "all()":
self.do_all(class_name)
elif cmd_list[1][:4] == "show":
self.do_show("{} {}".format(class_name, inst_id))
elif cmd_list[1][:7] == "destroy":
self.do_destroy("{} {}".format(class_name, inst_id))
elif cmd_list[1][:6] == "update":
cmd_attrs = line.split(",")
if cmd_attrs[1][1] == "{":
dict_vals = ast.literal_eval(
re.search(r'({.+})', line).group(0))
for key, value in dict_vals.items():
self.do_update("{} {} {} {}".format(
class_name, inst_id, key, value))
else:
cmd_attr = cmd_attrs[1].strip()
cmd_attr1 = cmd_attrs[2].split(")")[0]
B
self.do_update("{} {} {} {}".format(
class_name, inst_id, cmd_attr, cmd_attr1))
def do_all(self, args):
""" Shows all objects, or all objects of a class"""
print_list = []
if args:
args = args.split(' ')[0] # remove possible trailing args
if args not in HBNBCommand.classes:
print("** class doesn't exist **")
return
for k, v in storage.all().items():
if k.split('.')[0] == args:
print_list.append(str(v))
else:
for k, v in storage.all().items():
print_list.append(str(v))
print(print_list)
def do_update(self, arg):
"""Updates an instance based on the class name and id by adding
or updating attribute (save the change into the JSON file)"""
if not arg:
print("** class name missing **")
return
argv = arg.split()
if argv[0] not in HBNBCommand.classes:
print("** class doesn't exist **")
return
if len(argv) == 1:
print("** instance id missing **")
else:
try:
key = argv[0] + "." + argv[1]
if key in storage.all():
storage.all()[key]
except Exception:
print("** no instance found **")
return
if len(argv) == 2:
print("** attribute name missing **")
return
elif len(argv) == 3:
print("** value missing **")
return
else:
key = argv[0] + "." + argv[1]
try:
try:
value = int(argv[3])
except ValueError:
value = float(argv[3])
except ValueError:
value = argv[3].strip(":\"\'")
attr = argv[2].strip(":\"\'")
setattr(storage.all()[key], attr, value)
storage.save()
if __name__ == '__main__':
HBNBCommand().cmdloop()