-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconsole.py
More file actions
executable file
·207 lines (179 loc) · 5.98 KB
/
Copy pathconsole.py
File metadata and controls
executable file
·207 lines (179 loc) · 5.98 KB
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
#!/usr/bin/python3
"""
This program contains the entry point of the command interpreter
"""
import cmd
from models.base_model import BaseModel
from models.user import User
from models.place import Place
from models.city import City
from models.amenity import Amenity
from models.review import Review
from models.state import State
from models import storage
class HBNBCommand(cmd.Cmd):
"""This is a Cmd subclass representing our command interpreter"""
prompt = "(hbnb) "
def do_EOF(self, line):
"""Exit the interpreter"""
print()
return True
def do_quit(self, line):
"""Quit command to exit the program"""
return True
def emptyline(self):
"""Should do nothing when Enter key is pressed"""
pass
def do_create(self, arg):
'''
A function Creates a new instance of BaseModel,
saves it (to the JSON file) and prints the id
'''
if not arg:
print("** class name missing **")
return
args = arg.split()
class_repr = globals().get(args[0])
if len(args) != 1 or class_repr is None:
print("** class doesn't exist **")
else:
class_obj = class_repr()
print(class_obj.id)
def do_show(self, arg):
'''
A function that shows the string representation of a class name
and the id given
Usage: $ show <Class name> <id>
'''
args = arg.split()
if len(args) == 0:
print("** class name missing **")
return
if not globals().get(args[0]):
print("** class doesn't exist **")
return
if len(args) == 1:
print("** instance id missing **")
return
key = args[0] + '.' + args[1]
objects = storage.all()
obj = objects.get(key)
if not obj:
print("** no instance found **")
return
else:
print(obj)
def do_destroy(self, arg):
'''
A function that deletes an instance based on the class name and id
(save the change into the JSON file).
Usage: $ destroy <Class name> <id>
'''
args = arg.split()
if len(args) == 0:
print("** class name missing **")
return
if not globals().get(args[0]):
print("** class doesn't exist **")
return
if len(args) == 1:
print("** instance id missing **")
return
key = args[0] + '.' + args[1]
objects = storage.all()
obj = objects.get(key)
if not obj:
print("** no instance found **")
return
else:
del objects[key]
storage.save()
def do_all(self, arg):
'''
A function that prints all string representation of all instances
based or not on the class name.
Usage: $ all <Class name> or $ all
'''
args = arg.split()
objects = storage.all()
if len(args) > 1 or (len(args) == 1 and
globals().get(args[0]) is None):
print("** class doesn't exist **")
return
else:
all_objects = []
for key, value in objects.items():
if len(args) == 1 and key.split('.')[0] != args[0]:
pass
else:
all_objects.append(value.__str__())
print(all_objects)
def do_update(self, arg):
'''
A function that updates an instance based on the class name and id
by adding or updating an attribute (save the change into the
JSON file).
Usage: $ update <Class name> <id> <attribute name> "<attribute value>"
'''
args = arg.split()
if len(args) == 0:
print("** class name missing **")
return
if not globals().get(args[0]):
print("** class doesn't exist **")
return
if len(args) == 1:
print("** instance id missing **")
return
key = args[0] + '.' + args[1]
objects = storage.all()
obj = objects.get(key)
if not obj:
print("** no instance found **")
return
if len(args) == 2:
print("** attribute name missing **")
return
if len(args) == 3:
print("** value missing **")
return
value = args[3]
if value.replace('.', '', 1).isnumeric():
if value.replace('.', '', 1) == value:
value = int(value)
else:
value = float(value)
else:
if value[0] == '"' and value[-1] == '"':
value = value.strip('"')
if value[0] == "'" and value[-1] == "'":
value = value.strip("'")
setattr(objects[key], args[2], value)
objects[key].save()
def default(self, line):
args = line.split('.')
if globals().get(args[0]) and len(args) > 1:
if args[1] == 'all()':
self.do_all(args[0])
elif args[1] == 'count()':
count = 0
for key, value in storage.all().items():
if key.split('.')[0] == args[0]:
count += 1
print(count)
elif args[1][:4] == 'show':
id = args[1].strip('"()\'show')
self.do_show(args[0] + " " + id)
elif args[1][:7] == 'destroy':
id = args[1].strip('"()\'destroy')
self.do_destroy(args[0] + " " + id)
elif args[1][:6] == 'update':
pars = args[1].strip('update"\'()').split(', ')
ar = ""
for argument in pars:
ar += " " + argument.strip('"\'')
self.do_update(args[0] + ar)
else:
print("** Unknown syntax:", line)
if __name__ == '__main__':
HBNBCommand().cmdloop()