-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.py
More file actions
executable file
·145 lines (130 loc) · 4.2 KB
/
Copy pathconsole.py
File metadata and controls
executable file
·145 lines (130 loc) · 4.2 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
#!/usr/bin/python3
import cmd
import models
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):
"""Command interpreter class."""
prompt = "(hbnb) "
classes = {
'BaseModel': BaseModel,
'User': User,
'State': State,
'City': City,
'Amenity': Amenity,
'Place': Place,
'Review': Review,
}
def do_create(self, arg):
"""Creates a new instance of BaseModel."""
if not arg:
print("** class name missing **")
return
if arg not in self.classes:
print("** class doesn't exist **")
return
new_instance = self.classes[arg]()
new_instance.save()
print(new_instance.id)
def do_show(self, arg):
"""Prints the string representation of an instance."""
args = arg.split()
if not args:
print("** class name missing **")
return
if args[0] not in self.classes:
print("** class doesn't exist **")
return
if len(args) < 2:
print("** instance id missing **")
return
key = f"{args[0]}.{args[1]}"
instance = models.storage.all().get(key)
if instance:
print(instance)
else:
print("** no instance found **")
def do_destroy(self, arg):
"""Deletes an instance based on the class name and id."""
args = arg.split()
if not args:
print("** class name missing **")
return
if args[0] not in self.classes:
print("** class doesn't exist **")
return
if len(args) < 2:
print("** instance id missing **")
return
key = f"{args[0]}.{args[1]}"
if key in models.storage.all():
del models.storage.all()[key]
models.storage.save()
else:
print("** no instance found **")
def do_all(self, arg):
"""Prints all string representation."""
if arg and arg not in self.classes:
print("** class doesn't exist **")
return
objects = models.storage.all()
if arg:
objects = {key: obj for key,
obj in objects.items() if key.startswith(arg)}
print([str(obj) for obj in objects.values()])
def do_update(self, arg):
"""Updates an instance based on the class name and id by adding or
updating attribute."""
objects = {
key: obj for key, obj in objects.items()
if key.startswith(arg)
}
print([str(obj) for obj in objects.values()])
def do_update(self, arg):
"""Updates an instance based on the class name and id"""
args = arg.split()
if not args:
print("** class name missing **")
return
if args[0] not in self.classes:
print("** class doesn't exist **")
return
if len(args) < 2:
print("** instance id missing **")
return
key = f"{args[0]}.{args[1]}"
instance = models.storage.all().get(key)
if not instance:
print("** no instance found **")
return
if len(args) < 3:
print("** attribute name missing **")
return
if len(args) < 4:
print("** value missing **")
return
attribute = args[2]
value = args[3].strip('"')
if hasattr(instance, attribute):
value_type = type(getattr(instance, attribute))
setattr(instance, attribute, value_type(value))
else:
setattr(instance, attribute, value)
instance.save()
def do_quit(self, arg):
"""Exits the program."""
return True
def do_EOF(self, arg):
"""Exits the program."""
print()
return True
def emptyline(self):
"""Overrides the default behavior to repeat the last command."""
pass
if __name__ == "__main__":
HBNBCommand().cmdloop()