forked from Polaris-algedi/AirBnB_clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.py
executable file
·226 lines (185 loc) · 6.1 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
#!/usr/bin/python3
"""Module for the entry point of the command interpreter."""
import cmd
import shlex
from models import storage
from models.base_model import BaseModel
from models.user import User
from models.place import Place
from models.state import State
from models.city import City
from models.amenity import Amenity
from models.review import Review
class_dict = {
"BaseModel": BaseModel,
"User": User,
"State": State,
"City": City,
"Amenity": Amenity,
"Place": Place,
"Review": Review
}
class HBNBCommand(cmd.Cmd):
"""
Class for the command interpreter.
Attributes:
prompt (str): Prompt text for the command line.
"""
prompt = "(hbnb) "
def do_create(self, class_name):
"""
Create a new instance of the specified class.
Args:
class_name (str): The name of the class.
Usage: create <class_name>
"""
if class_name:
if class_name in class_dict:
new_instance = class_dict[class_name]()
new_instance.save()
print(new_instance.id)
else:
print("** class doesn't exist **")
else:
print("** class name missing **")
def _validate_instance_command(self, line):
"""
Helper method to validate and extract information from a command line.
Args:
line (str): The input command line.
Returns:
str: The instance key if valid and found, otherwise False.
"""
if line:
commands = shlex.split(line)
if commands[0] in class_dict:
if len(commands) >= 2:
objs = storage.all()
key = f"{commands[0]}.{commands[1]}"
if key in objs:
return key
else:
print("** no instance found **")
else:
print("** instance id missing **")
else:
print("** class doesn't exist **")
else:
print("** class name missing **")
return False
def do_show(self, line):
"""
Display information about a specified instance.
Args:
line (str): The input command line.
Usage: show <class_name> <instance_id>
"""
key = self._validate_instance_command(line)
if key:
objs = storage.all()
print(objs[key])
def do_destroy(self, line):
"""
Delete a specified instance.
Args:
line (str): The input command line.
Usage: destroy <class_name> <instance_id>
"""
key = self._validate_instance_command(line)
if key:
objs = storage.all()
del objs[key]
storage.save()
def do_all(self, line):
"""
Display information about all instances or instances
of a specific class.
Args:
line (str): The input command line.
Usage: all [class_name]
"""
objs = storage.all()
if objs:
objs_list = list(objs.values())
if not line:
objs_list = [str(o) for o in objs_list]
print(objs_list)
else:
objs_list = [str(o) for o in objs_list
if type(o).__name__ == line]
if objs_list:
print(objs_list)
else:
print("** class doesn't exist **")
def do_update(self, line):
"""
Update attributes of a specified instance.
Args:
line (str): The input command line.
Usage: update <class_name> <instance_id> <attribute_name>
<attribute_value>
"""
key = self._validate_instance_command(line)
commands = shlex.split(line)
if key:
if len(commands) < 3:
print("** attribute name missing **")
return
if len(commands) < 4:
print("** value missing **")
return
objs = storage.all()
setattr(objs[key], commands[2], commands[3])
objs[key].save()
def count(self, cls_name):
"""
Count the number of instances of a specified class.
Args:
cls_name (str): The name of the class.
This method counts the instances of the specified class
and prints the count.
"""
objs = list(storage.all().values())
count = sum(1 for o in objs if type(o).__name__ == cls_name)
print(count)
def default(self, line):
"""
Default method called for unrecognized commands.
Args:
line (str): The input command line.
This method checks for specific patterns of commands
and delegates to corresponding methods.
Available Patterns:
- "<class name>.all()":
Calls the "do_all()" method with the class name.
- "<class name>.count()":
Calls the "count()" method with the class name.
"""
cmd = {
"all()": self.do_all,
"count()": self.count
}
commands = line.split(".")
if len(commands) > 1 and commands[1] in cmd:
cmd[commands[1]](commands[0])
def emptyline(self):
"""Do nothing when an empty line is entered."""
pass
def do_EOF(self, line):
"""Handles the end-of-file condition,
by exiting gracefully
"""
print()
return True
def help_EOF(self):
"""Display help information for the EOF command."""
print("This command allows you to exit the command", end=" ")
print("interpreter gracefully by pressing Ctrl+D (EOF).\n")
def do_quit(self, line):
"""This command allows you to exit the command"""
return True
def help_quit(self):
"""Display help information for the quit command."""
print("Quit command to exit the program\n")
if __name__ == '__main__':
HBNBCommand().cmdloop()