-
Notifications
You must be signed in to change notification settings - Fork 1
/
console.py
executable file
·115 lines (103 loc) · 3.32 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
#!/usr/bin/python3
""" console.py """
import cmd
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.amenity import Amenity
from models.place import Place
from models.review import Review
class HBNBCommand(cmd.Cmd):
""" hbnb """
prompt = '(hbnb) '
__my_class = ["BaseModel", "User", "Place",
"State", "City", "Amenity", "Review"]
def do_quit(self, arg):
""" Quit console """
return True
def do_EOF(self, arg):
""" exit file with eof """
print("")
return True
def emptyline(self):
""" empty line """
pass
def do_create(self, arg):
""" create class instance """
args = arg.split()
if len(args) == 0:
print("** class name missing **")
elif args[0] not in self.__my_class:
print("** class doesn't exist **")
else:
instance = eval(args[0])()
instance.save()
print(instance.id)
def do_show(self, arg):
""" show str repr """
args = arg.split()
if not args:
print("** class name missing **")
elif args[0] not in self.__my_class:
print("** class doesn't exist **")
elif len(args) < 2:
print("** instance id missing **")
else:
objects = storage.all()
key = f"{args[0]}.{args[1]}"
if key in objects:
print(objects[key])
else:
print("** no instance found **")
def do_destroy(self, arg):
""" Remove instance based id """
args = arg.split()
if not args:
print("** class name missing **")
elif args[0] not in self.__my_class:
print("** class doesn't exist **")
elif len(args) < 2:
print("** instance id missing **")
else:
objects = storage.all()
key = f"{args[0]}.{args[1]}"
if key in objects.keys():
del objects[key]
storage.save()
else:
print("** no instance found **")
def do_all(self, arg):
""" Print all string reprs """
args = arg.split()
if len(args) == 0 or args[0] in self.__my_class:
objects = storage.all()
for value in objects.values():
print(str(value))
else:
print("** class doesn't exist **")
def do_update(self, arg):
""" update instance based id """
args = arg.split()
if not args:
print("** class name missing **")
elif args[0] not in self.__my_class:
print("** class doesn't exist **")
elif len(args) < 2:
print("** instance id missing **")
elif len(args) < 3:
print("** attribute name missing **")
elif len(args) < 4:
print("** value missing **")
else:
objects = storage.all()
key = f"{args[0]}.{args[1]}"
if key in objects:
obj = objects[key]
setattr(obj, args[2], args[3])
obj.save()
else:
print("** no instance found **")
if __name__ == '__main__':
HBNBCommand().cmdloop()