generated from manningstinson/codespaces-flask
-
Notifications
You must be signed in to change notification settings - Fork 1
/
console.py
executable file
·154 lines (140 loc) · 4.16 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
#!/usr/bin/python3
"""
Console module implementing a command interpreter for the AirBnB clone project.
"""
import cmd
import re
from shlex import split
from models import storage
from models.base_model import BaseModel
from models.user import User
from models.state import State
from models.city import City
from models.place import Place
from models.amenity import Amenity
from models.review import Review
class HBNBCommand(cmd.Cmd):
"""
HBNBCommand class implementing a command interpreter.
"""
prompt = '(hbnb) '
def do_quit(self, arg):
"""
Quit command to exit the program
"""
return True
def do_EOF(self, line):
"""
Exit the program when EOF is reached
"""
return True
def emptyline(self):
"""
Do nothing on empty line
"""
pass
def do_create(self, arg):
"""
Create a new instance of a specified class
Usage: create <class_name>
"""
class_names = {"State", "City", "Amenity", "Place", "Review", "User"} # Add more if needed
args = arg.split()
if len(args) == 0:
print("** class name missing **")
return
class_name = args[0]
if class_name not in class_names:
print("** class doesn't exist **")
return
new_instance = eval(class_name)()
new_instance.save()
print(new_instance.id)
def do_show(self, arg):
"""
Show the string representation of an instance
Usage: show <class_name> <id>
"""
args = arg.split()
if len(args) == 0:
print("** class name missing **")
return
if len(args) == 1:
print("** instance id missing **")
return
class_name = args[0]
obj_id = args[1]
key = "{}.{}".format(class_name, obj_id)
objects = storage.all()
if key not in objects:
print("** no instance found **")
else:
print(objects[key])
def do_destroy(self, arg):
"""
Delete an instance based on the class name and id
Usage: destroy <class_name> <id>
"""
args = arg.split()
if len(args) == 0:
print("** class name missing **")
return
if len(args) == 1:
print("** instance id missing **")
return
class_name = args[0]
obj_id = args[1]
key = "{}.{}".format(class_name, obj_id)
objects = storage.all()
if key not in objects:
print("** no instance found **")
else:
del objects[key]
storage.save()
def do_all(self, arg):
"""
Print all string representation of all instances
Usage: all [optional_class_name]
"""
objects = storage.all()
if not arg:
print([str(obj) for obj in objects.values()])
return
try:
class_name = eval(arg).__name__
except NameError:
print("** class doesn't exist **")
return
print([str(obj) for key, obj in objects.items() if key.startswith(class_name)])
def do_update(self, arg):
"""
Update an instance based on the class name and id
Usage: update <class_name> <id> <attribute_name> "<attribute_value>"
"""
args = arg.split()
if len(args) == 0:
print("** class name missing **")
return
if len(args) == 1:
print("** instance id missing **")
return
if len(args) == 2:
print("** attribute name missing **")
return
if len(args) == 3:
print("** value missing **")
return
class_name = args[0]
obj_id = args[1]
attr_name = args[2]
attr_value = args[3]
key = "{}.{}".format(class_name, obj_id)
objects = storage.all()
if key not in objects:
print("** no instance found **")
return
obj = objects[key]
setattr(obj, attr_name, attr_value)
storage.save()
if __name__ == '__main__':
HBNBCommand().cmdloop()