-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_storage.py
executable file
·83 lines (72 loc) · 2.71 KB
/
file_storage.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
#!/usr/bin/python3
import json
import os
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 FileStorage:
"""
Defines FileStorage class
Attributes:
__file_path = string, private, file name
__objects = Dictionary, empty but will grow with value
__classes
"""
__file_path = "file.json"
__objects = {}
__classes = {
"BaseModel": BaseModel,
"User": User,
"State": State,
"City": City,
"Place": Place,
"Amenity": Amenity,
"Review": Review
}
def __init__(self):
"""Initialization"""
pass
def all(self):
"""
returns the dictionary __objects
"""
# return (FileStorage.__objects)
return (self.__objects)
def new(self, obj):
"""
sets in __objects the obj with key <obj class name>.id
"""
my_key = ("{}.{}".format(obj.__class__.__name__, obj.id))
# FileStorage.__objects[my_key] = obj
self.__objects[my_key] = obj
def save(self):
"""serializes __objects to the JSON file (path: __file_path)"""
my_temp_dict = {}
for key, value in FileStorage.__objects.items():
my_temp_dict[key] = value.to_dict()
with open(FileStorage.__file_path, "w", encoding="utf-8") as myFile:
json.dump(my_temp_dict, myFile)
# with open(FileStorage.__file_path, "w", encoding="utf-8") as file:
# dict_value = {key: value.to_dict() for key,
# value in FileStorage.__objects.items()}
# json.dump(dict_value, file)
def reload(self):
"""deserializes the JSON file to __objects
(only if the JSON file (__file_path) exists; otherwise, do nothing.
If the file doesn’t exist, no exception should be raised)"""
# Error check when file.json does not exist.
if os.path.exists(FileStorage.__file_path):
# with open(self.__file_path, "r", encoding="utf-8") as myFile:
with open(FileStorage.__file_path, "r", encoding="utf-8") as myFil:
my_reload_dict = json.load(myFil)
for key, value in my_reload_dict.items():
split_result = key.split(".")
class_name = split_result[0]
class_ID = split_result[1]
obj_class = FileStorage.__classes.get(class_name)
if obj_class is not None:
FileStorage.__objects[key] = obj_class(**value)