-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.py
209 lines (175 loc) · 5.34 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
#!/usr/bin/python3
"""
Entry point of the command interpreter
"""
import cmd
import shlex
from models.base_model import BaseModel
from models import storage
class HBNBCommand(cmd.Cmd):
"""
Command interpreter class
"""
prompt = "(hbnb) "
def do_quit(self, arg):
"""
Quit command to exit the program
"""
return True
def do_EOF(self, arg):
"""
Quit command to exit the program
"""
print()
return True
def emptyline(self):
"""
Do nothing when the user inputs an empty line
"""
pass
def do_create(self, arg):
"""
Create a new instance of BaseModel or User, saves it (to the JSON file)
and prints the id
"""
args = shlex.split(arg)
if not args:
print("** class name missing **")
return
cls_name = args[0]
if cls_name not in storage.classes:
print("** class doesn't exist **")
return
obj_dict = {}
for pair in args[1:]:
key_val = pair.split('=')
if len(key_val) == 2:
key, val = key_val[0], key_val[1]
if val[0] == '"' and val[-1] == '"':
val = val[1:-1]
elif '.' in val:
try:
val = float(val)
except ValueError:
pass
else:
try:
val = int(val)
except ValueError:
pass
obj_dict.update({key: val})
obj = eval(cls_name)(**obj_dict)
obj.save()
print(obj.id)
def do_show(self, arg):
"""
Prints the string representation of an instance based on the class name and id
"""
args = shlex.split(arg)
if not args:
print("** class name missing **")
return
cls_name = args[0]
if cls_name not in storage.classes:
print("** class doesn't exist **")
return
if len(args) < 2:
print("** instance id missing **")
return
instance_id = args[1]
key = '{}.{}'.format(cls_name, instance_id)
if key not in storage.all():
print("** no instance found **")
return
instance = storage.all()[key]
print(instance)
def do_destroy(self, arg):
"""
Deletes an instance based on the class name and id
"""
args = shlex.split(arg)
if not args:
print("** class name missing **")
return
cls_name = args[0]
if cls_name not in storage.classes:
print("** class doesn't exist **")
return
if len(args) < 2:
print("** instance id missing **")
return
instance_id = args[1]
key = '{}.{}'.format(cls_name, instance_id)
if key not in storage.all():
print("** no instance found **")
return
storage.all().pop(key)
storage.save()
def do_all(self, arg):
"""
Prints all string representations of all instances based on the class name
"""
args = shlex.split(arg)
if not args:
print("** class name missing **")
return
cls_name = args[0]
if cls_name not in storage.classes:
print("** class doesn't exist **")
return
all_instances = []
for key in storage.all():
if key.split('.')[0] == cls_name:
all_instances.append(str(storage.all()[key]))
print(all_instances)
def do_update(self, arg):
"""
Updates an instance based on the class name and id using a dictionary representation of the attributes
"""
args = shlex.split(arg)
if not args:
print("** class name missing **")
return
cls_name = args[0]
if cls_name not in storage.classes:
print("** class doesn't exist **")
return
if len(args) < 2:
print("** instance id missing **")
return
instance_id = args[1]
key = '{}.{}'.format(cls_name, instance_id)
if key not in storage.all():
print("** no instance found **")
return
if len(args) < 3:
print("** dictionary missing **")
return
try:
attributes_dict = ast.literal_eval(args[2])
except ValueError:
print("** invalid dictionary **")
return
instance = storage.all().get(key)
for attr, value in attributes_dict.items():
setattr(instance, attr, value)
instance.save()
def do_count(self, arg):
"""
Retrieves the number of instances of a class
"""
args = shlex.split(arg)
if not args:
print("** class name missing **")
return
cls_name = args[0]
if cls_name not in storage.classes:
print("** class doesn't exist **")
return
count = 0
for key in storage.all():
if key.split('.')[0] == cls_name:
count += 1
print(count)
if __name__ == '__main__':
HBNBCommand().cmdloop()