-
Notifications
You must be signed in to change notification settings - Fork 1
/
console.py
executable file
·122 lines (104 loc) · 3.82 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
#!/usr/bin/python3
"""This module provides a console-like interpreter to handle input commands."""
import cmd
from models.base_model import BaseModel
from models import storage
from models.user import User
from models.place import Place
from models.state import State
from models.city import City
from models.amenity import Amenity
from models.review import Review
class HBNBCommand(cmd.Cmd):
"""Cmd console method."""
prompt = "(hbnb) "
__classes = ["BaseModel", "User"]
def do_quit(self, arg):
"""Quit command to exit the program.
"""
return True # True for (1) means program run successful then quits.
def do_EOF(self, arg):
"""Exit command to exit the program.
"""
return True
def emptyline(self):
"""Empty line + ENTER shouldn’t execute anything."""
return
def do_create(self, arg):
"""Command interpreter: create command.
Create new class instance, save it (to the JSON file),
and print the id.
"""
args = arg.split()
if len(args) == 0:
print("** class name missing **")
elif args[0] not in self.__classes:
print("** class doesn't exist **")
else:
new_object = eval(f"{args[0]}")()
print(new_object.id)
def do_show(self, arg):
"""Command interpreter: show command.
Print string representation of an instance based
on the class name and id.
"""
args = arg.split()
if len(args) == 0:
print("** class name missing **")
elif args[0] not in self.__classes:
print("** class doesn't exist **")
elif len(args) == 1:
print("** instance id missing **")
elif f"{args[0]}.{args[1]}" not in storage.all():
print("** no instance found **")
else:
print(storage.all()[f"{args[0]}.{args[1]}"])
def do_destroy(self, arg):
"""Command interpreter: destroy command.
Deletes an instance based on the class name and id.
"""
args = arg.split()
if len(args) == 0:
print("** class name missing **")
elif args[0] not in self.__classes:
print("** class doesn't exist **")
elif len(args) == 1:
print("** instance id missing **")
elif f"{args[0]}.{args[1]}" not in storage.all():
print("** no instance found **")
else:
del storage.all()[f"{args[0]}.{args[1]}"]
storage.save()
def do_all(self, arg):
"""Command interpreter: all command.
Prints all string representation of all instances based
or not on the class name.
"""
args = arg.split()
if len(args) == 0: # if args doesn't exist
print([str(value) for value in storage.all().values()])
elif args[0] not in self.__classes:
print("** class doesn't exist **")
else:
all_items = storage.all().items()
print([str(v) for k, v in all_items if k.startswith(arg[0])])
def do_update():
"""Command interpreter: update command.
Updates an instance based on the class name and id by adding
or updating attribute (save the change into the JSON file).
"""
args = arg.split()
if len(args) == 0:
print("** class name missing **")
elif args[0] not in self.__classes:
print("** class doesn't exist **")
elif len(args) == 1:
print("** instance id missing **")
elif f"{args[0]}.{args[1]}" not in storage.all():
print("** no instance found **")
elif len(args) == 2:
print("** attribute name missing **")
elif len(args) == 3:
print("** value missing **")
if __name__ == "__main__":
HBNBCommand().cmdloop()