-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.py
executable file
·169 lines (141 loc) · 5.49 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
#!/usr/bin/python3
"""Entry point of HBNB command interpreter"""
import cmd
from models.base_model import BaseModel
from models.user import User
from models.city import City
from models.state import State
from models.amenity import Amenity
from models.place import Place
from models.review import Review
from models import storage
from ast import literal_eval
import re
class HBNBCommand(cmd.Cmd):
"""Command interpreter class"""
prompt = '(hbnb) '
def do_create(self, class_name):
"""Creates a new instance of BaseModel"""
if not class_name:
print("** class name missing **")
elif globals().get(class_name) is None:
print("** class doesn't exist **")
else:
instance = storage.class_list()[class_name]()
instance.save()
print(instance.id)
def do_show(self, line):
"""Prints the string representation of an instance"""
line = line.split()
if len(line) < 1:
print("** class name missing **")
elif globals().get(line[0]) is None:
print("** class doesn't exist **")
elif len(line) < 2:
print("** instance id missing **")
elif f"{line[0]}.{line[1]}" not in storage.all():
print("** no instance found **")
else:
print(storage.all()[f"{line[0]}.{line[1]}"])
def do_destroy(self, line):
"""Deletes an instance based on the class name and id"""
line = line.split()
if len(line) < 1:
print("** class name missing **")
elif globals().get(line[0]) is None:
print("** class doesn't exist **")
elif len(line) < 2:
print("** instance id missing **")
elif f"{line[0]}.{line[1]}" not in storage.all():
print("** no instance found **")
else:
del (storage.all()[f"{line[0]}.{line[1]}"])
def do_all(self, class_name):
"""Prints all string representation of all instances"""
if not class_name:
print(list(str(instance) for instance in storage.all().values()))
else:
if globals().get(class_name) is None:
print("** class doesn't exist **")
else:
print(list(str(instance) for instance in storage.all(
).values() if type(instance).__name__ == class_name))
def do_update(self, line):
"""Updates an instance based on the class name and id"""
# Usage: update <class name> <id> <attribute name> "<attribute value>"
line = line.split()
if len(line) < 1:
print("** class name missing **")
elif globals().get(line[0]) is None:
print("** class doesn't exist **")
elif len(line) < 2:
print("** instance id missing **")
elif f"{line[0]}.{line[1]}" not in storage.all():
print("** no instance found **")
elif len(line) < 3:
print("** attribute name missing **")
elif len(line) < 4:
print("** value missing **")
else:
class_name = line[0]
instance_id = line[1]
attribute_name = line[2]
attribute_value = literal_eval(line[3])
instance = storage.all()[f"{class_name}.{instance_id}"]
setattr(instance, attribute_name, attribute_value)
instance.save()
def do_EOF(self, line):
"""Quits the command interpreter when it receives EOF signal"""
print()
return True
def do_quit(self, line):
"""Quits out of the command interpreter"""
return True
def emptyline(self):
pass
def default(self, line):
''' implements the default commands '''
objects = storage.all().values()
argv = line.split('.', 1)
if len(argv) != 2 or argv[0] not in storage.class_list():
print('*** unknown syntax:', line)
return
if argv[0] in storage.class_list() and argv[1].endswith('()'):
command = argv[1][:-2]
if command not in ['all', 'count']:
print('*** unknown syntax:', line)
return
if command == 'all':
attrs = \
[obj for obj in objects if type(obj).__name__ == argv[0]]
[print(att, end=', ' if att != attrs[-1] else '\n')
for att in attrs]
return
if command == 'count':
count = 0
for obj in objects:
if type(obj).__name__ == argv[0]:
count += 1
print(count)
return
cls_name = argv[0]
param = re.search(r"\((.*?)\)", argv[1])
attributes = re.search(r"\{(.*?)\}", param[1])
if param:
method = argv[1][:param.span()[0]]
param_list = param[1].split(', ', 2)
id = param_list[0][1:-1]
if len(param_list) == 1:
line = ' '.join([cls_name, id])
eval('self.do_' + method)(line)
elif not attributes:
param_list = ' '.join(param_list)
line = ' '.join([cls_name, param_list])
eval('self.do_' + method)(line)
else:
param_list = param[1].split(', ', 1)
attr_dict = param_list[1].replace("'", '"')
line = ' '.join([cls_name, id, attr_dict])
eval('self.do_' + method)(line)
if __name__ == '__main__':
HBNBCommand().cmdloop()