-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.py
executable file
·183 lines (152 loc) · 5.6 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
#!/usr/bin/python3
"""The console for AirBnB clone, hbnb"""
import cmd
from models import storage
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.state import State
from models.review import Review
import json
import shlex
class HBNBCommand(cmd.Cmd):
""" Class for the command interpreter. This is the entry point"""
prompt = "(hbnb) "
l_classes = ['BaseModel', 'User', 'Amenity',
'Place', 'City', 'State', 'Review']
l_c = ['create', 'show', 'update', 'all', 'destroy', 'count']
def precmd(self, arg):
""""""
if '.' in arg and '(' in arg and ')' in arg:
cls = arg.split('.')
cnd = cls[1].split('(')
args = cnd[1].split(')')
if cls[0] in HBNBCommand.l_classes and cnd[0] in HBNBCommand.l_c:
arg = cnd[0] + ' ' + cls[0] + ' ' + args[0]
return arg
def help_help(self):
""" Provides description of given command """
print("Provides description of a given command")
def emptyline(self):
""" Handles empty spaces when you press ENTER """
pass
def do_count(self, cls_name):
"""Counts all written commands"""
count = 0
all_objs = storage.all()
for k, v in all_objs.items():
clss = k.split('.')
if clss[0] == cls_name:
count = count + 1
print(count)
def do_create(self, type_model):
"""Creates and saves new instances of BaseModel and prints id"""
if not type_model:
print("** class name missing **")
elif type_model not in HBNBCommand.l_classes:
print("** class doesn't exist **")
else:
dct = {'BaseModel': BaseModel, 'User': User, 'Place': Place,
'City': City, 'Amenity': Amenity, 'State': State,
'Review': Review}
my_model = dct[type_model]()
print(my_model.id)
my_model.save()
def do_show(self, arg):
"""
Prints the string representation of an instance based on class name
"""
if not arg:
print("** class name missing **")
return
args = arg.split(' ')
if args[0] not in HBNBCommand.l_classes:
print("** class doesn't exist **")
elif len(args) == 1:
print("** instance id missing **")
else:
all_objs = storage.all()
for key, value in all_objs.items():
ob_name = value.__class__.__name__
ob_id = value.id
if ob_name == args[0] and ob_id == args[1].strip('"'):
print(value)
return
print("** no instance found **")
def do_destroy(self, arg):
""" Deletes an instance based on the class name and id"""
if not arg:
print("** class name missing **")
return
args = arg.split(' ')
if args[0] not in HBNBCommand.l_classes:
print("** class doesn't exist **")
elif len(args) == 1:
print("** instance id missing **")
else:
all_objs = storage.all()
for key, value in all_objs.items():
ob_name = value.__class__.__name__
ob_id = value.id
if ob_name == args[0] and ob_id == args[1].strip('"'):
del value
del storage._FileStorage__objects[key]
storage.save()
return
print("** no instance found **")
def do_all(self, arg):
"""
Print all string reps of all instances based or not on the class name
"""
if not arg:
print("** class name missing **")
return
args = arg.split(' ')
if args[0] not in HBNBCommand.l_classes:
print("** class doesn't exist **")
else:
all_objs = storage.all()
list_instances = []
for key, value in all_objs.items():
ob_name = value.__class__.__name__
if ob_name == args[0]:
list_instances += [value.__str__()]
print(list_instances)
def do_update(self, arg):
""" Updates an instance based on the class name and id"""
if not arg:
print("** class name missing **")
return
a = ""
for argv in arg.split(','):
a = a + argv
args = shlex.split(a)
if args[0] not in HBNBCommand.l_classes:
print("** class doesn't exist **")
elif len(args) == 1:
print("** instance id missing **")
else:
all_objs = storage.all()
for key, objc in all_objs.items():
ob_name = objc.__class__.__name__
ob_id = objc.id
if ob_name == args[0] and ob_id == args[1].strip('"'):
if len(args) == 2:
print("** attribute name missing **")
elif len(args) == 3:
print("** value missing **")
else:
setattr(objc, args[2], args[3])
storage.save()
return
print("** no instance found **")
def do_quit(self, line):
""" Quit command to exit the program"""
return True
def do_EOF(self, line):
""" EOF command to exit the program"""
return True
if __name__ == '__main__':
HBNBCommand().cmdloop()