-
Notifications
You must be signed in to change notification settings - Fork 1
/
console.py
executable file
·169 lines (139 loc) · 4.39 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
import cmd
import models
from models import storage
from models.base_model import BaseModel
from models.place import Place
from models.state import State
from models.city import City
from models.amenity import Amenity
from models.user import User
from models.review import Review
class HBNBCommand(cmd.Cmd):
"""
making the console
"""
all_classes = {'BaseModel' : BaseModel, 'User' : User, 'State' : State,
'City': City, 'Amenity' : Amenity, 'Place' : Place,
'Review' : Review}
prompt = '(hbnb)'
def do_quit(self, arg):
""" exiting the program"""
return True
def do_EOF(self, arg):
""" quit """
return True
def help_quit(self):
print('Quit command to exit the program\n')
def emptyline(self):
pass
def do_create(self, arg):
""" creates a new instance of BaseModel saves it to the json file
and prints the id
"""
if arg == '':
print('** class name missing **')
return
elif arg not in HBNBCommand.all_classes.keys():
print('** class doesn\'t exist **')
else:
B1 = HBNBCommand.all_classes[arg]()
B1.save()
print(B1.id)
def do_show(self, arg):
""" prints the str rep of an instance based on the
class name and id
"""
lst = arg.split(' ')
if len(arg) == 0:
print('** class name missing **')
return
elif lst[0] not in HBNBCommand.all_classes.keys():
print('** class doesn\'t exist **')
return
elif len(lst) == 1:
print('** instance id missing **')
return
else:
key = lst[0] + '.' + lst[1]
allInstances = storage.all()
if key not in allInstances.keys():
print('** no instance found **')
else:
obj = allInstances[key]
print(obj)
def do_destroy(self, arg):
"""
deletes an instance based on the class name and id
"""
lst = arg.split(' ')
if len(arg) == 0:
print('** class name missing **')
return
elif lst[0] not in HBNBCommand.all_classes.keys():
print('** class doesn\'t exist **')
return
elif len(lst) == 1:
print('** instance id missing **')
return
else:
key = lst[0] + '.' + lst[1]
allInstances = storage.all()
if key not in allInstances.keys():
print('** no instance found **')
else:
del(allInstances[key])
storage.save()
def do_all(self, arg):
"""
prints all str rep of all instances based or not on the class name
"""
objList = []
obj = storage.all()
try:
if len(arg) != 0:
eval(arg)
else:
pass
except NameError:
print('** class doesn\'t exist **')
return
arg.strip()
for key, val in obj.items():
if len(arg) != 0:
if type(val) is eval(arg):
val = str(obj[key])
objList.append(val)
else:
val = str(obj[key])
objList.append(val)
print(objList)
def do_update(self, arg):
"""
updates an instance based on the class name and id by adding or updating attr
"""
lst = arg.split(' ')
if len(arg) == 0:
print('** class name missing **')
return
elif lst[0] not in HBNBCommand.all_classes.keys():
print('** class doesn\'t exist **')
return
elif len(lst) == 1:
print('** instance id missing **')
return
elif len(lst) == 2:
print('** attribute name missing **')
elif len(lst) == 3:
print('** value missing **')
else:
key = lst[0] + '.' + lst[1]
allInstances = storage.all()
if key not in allInstances.keys():
print('** no instance found **')
else:
obj = allInstances[key]
setattr(obj, lst[2], lst[3])
storage.save()
if __name__ == '__main__':
HBNBCommand().cmdloop()