forked from nelsonwamalwa/AirBnB_clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.py
executable file
·265 lines (221 loc) · 7.95 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
#!/usr/bin/python3
"""This module defines the entry point of the command interpreter.
It defines one class, `HBNBCommand()`, which sub-classes the `cmd.Cmd` class.
This module defines abstractions that allows us to manipulate a powerful
storage system (FileStorage / DB). This abstraction will also allow us to
change the type of storage easily without updating all of our codebase.
It allows us to interactively and non-interactively:
- create a data model
- manage (create, update, destroy, etc) objects via a console / interpreter
- store and persist objects to a file (JSON file)
usage example:
$ ./console
(hbnb)
(hbnb) help
Documented commands (type help <topic>):
========================================
EOF create help quit
(hbnb)
(hbnb) quit
$
"""
import re
import cmd
import json
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.review import Review
from models.amenity import Amenity
from models.place import Place
CurrentClasses = {'BaseModel': BaseModel, 'User': User,
'Amenity': Amenity, 'City': City, 'State': State,
'Place': Place, 'Review': Review}
class HBNBCommand(cmd.Cmd):
"""The command interpreter.
This class represents the command interpreter, and the control center
of this project. It defines function handlers for all commands inputted
in the console and calls the appropriate storage engine APIs to manipulate
application data / models.
The sub-classes Python's `cmd.Cmd` class which provides a simple framework
for writing line-oriented command interpreters.
"""
prompt = "(hbnb) "
def precmd(self, line):
""" Defines instructions to execute before <line> is interpreted. """
if not line:
return '\n'
pattern = re.compile(r"(\w+)\.(\w+)\((.*)\)")
MatchList = pattern.findall(line)
if not MatchList:
return super().precmd(line)
MatchTuple = MatchList[0]
if not MatchTuple[2]:
if MatchTuple[1] == "count":
ObjectInstances = storage.all()
print(len([
j for _, j in ObjectInstances.items()
if type(j).__name__ == MatchTuple[0]]))
return "\n"
return "{} {}".format(MatchTuple[1], MatchTuple[0])
else:
args = MatchTuple[2].split(", ")
if len(args) == 1:
return "{} {} {}".format(
MatchTuple[1], MatchTuple[0],
re.sub("[\"\']", "", MatchTuple[2]))
else:
MatchJson = re.findall(r"{.*}", MatchTuple[2])
if (MatchJson):
return "{} {} {} {}".format(
MatchTuple[1], MatchTuple[0],
re.sub("[\"\']", "", args[0]),
re.sub("\'", "\"", MatchJson[0]))
return "{} {} {} {} {}".format(
MatchTuple[1], MatchTuple[0],
re.sub("[\"\']", "", args[0]),
re.sub("[\"\']", "", args[1]), args[2])
def do_help(self, arg):
""" To get help on a command, type help <topic>. """
return super().do_help(arg)
def do_EOF(self, line):
""" Built-In EOF command to catch errors when necessary. """
print("")
return True
def do_quit(self, arg):
""" Quit command to exit the program. """
return True
def emptyline(self):
""" Override default `empty line + return` behaviour. """
pass
def do_create(self, arg):
"""Creates a new instance.
"""
args = arg.split()
if not validate_classname(args):
return
NewObject = CurrentClasses[args[0]]()
NewObject.save()
print(NewObject.id)
def do_show(self, arg):
""" Show the string representation of an instance. """
args = arg.split()
if not validate_classname(args, CheckId=True):
return
ObjectInstances = storage.all()
key = "{}.{}".format(args[0], args[1])
RequestInstance = ObjectInstances.get(key, None)
if RequestInstance is None:
print("** no instance found **")
return
print(RequestInstance)
def do_destroy(self, arg):
""" Destory instance based on the class name and id. """
args = arg.split()
if not validate_classname(args, CheckId=True):
return
ObjectInstances = storage.all()
key = "{}.{}".format(args[0], args[1])
RequestInstance = ObjectInstances.get(key, None)
if RequestInstance is None:
print("** no instance found **")
return
del ObjectInstances[key]
storage.save()
def do_all(self, arg):
"""Do print string representation of all instances. """
args = arg.split()
AllObject = storage.all()
if len(args) < 1:
print(["{}".format(str(j)) for _, j in AllObject.items()])
return
if args[0] not in CurrentClasses.keys():
print("** class doesn't exist **")
return
else:
print(["{}".format(str(j))
for _, j in AllObject.items() if type(j).__name__ == args[0]])
return
def do_update(self, arg: str):
""" Do update of instance based on the class name and id."""
args = arg.split(maxsplit=3)
if not validate_classname(args, CheckId=True):
return
ObjectInstances = storage.all()
key = "{}.{}".format(args[0], args[1])
RequestInstance = ObjectInstances.get(key, None)
if RequestInstance is None:
print("** no instance found **")
return
MatchJson = re.findall(r"{.*}", arg)
if MatchJson:
payload = None
try:
payload: dict = json.loads(MatchJson[0])
except Exception:
print("** invalid syntax")
return
for i, j in payload.items():
setattr(RequestInstance, i, j)
storage.save()
return
if not validate_attrs(args):
return
FistAttribute = re.findall(r"^[\"\'](.*?)[\"\']", args[3])
if FistAttribute:
setattr(RequestInstance, args[2], FistAttribute[0])
else:
ValueList = args[3].split()
setattr(RequestInstance, args[2], parse_str(ValueList[0]))
storage.save()
def validate_classname(args, CheckId=False):
"""Runs Validation on args classname entry. """
if len(args) < 1:
print("** class name missing **")
return False
if args[0] not in CurrentClasses.keys():
print("** class doesn't exist **")
return False
if len(args) < 2 and CheckId:
print("** instance id missing **")
return False
return True
def validate_attrs(args):
"""Runs validations on args classname attributes and values."""
if len(args) < 3:
print("** attribute name missing **")
return False
if len(args) < 4:
print("** value missing **")
return False
return True
def is_float(x):
"""Checks for float value passed in the function `is_float`"""
try:
a = float(x)
except (TypeError, ValueError):
return False
else:
return True
def is_int(x):
"""Checks for integer value passed in the function `is_int`"""
try:
a = float(x)
b = int(a)
except (TypeError, ValueError):
return False
else:
return a == b
def parse_str(arg):
""" Parse through `args` to integer, float or string. """
parsed = re.sub("\"", "", arg)
if is_int(parsed):
return int(parsed)
elif is_float(parsed):
return float(parsed)
else:
return arg
if __name__ == "__main__":
HBNBCommand().cmdloop()