forked from alexaorrico/AirBnB_clone_v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_storage.py
executable file
·110 lines (96 loc) · 3.37 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
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
#!/usr/bin/python3
"""
Handles I/O, writing and reading, of JSON for storage of all class instances
"""
import json
from models import base_model, amenity, city, place, review, state, user
from datetime import datetime
strptime = datetime.strptime
to_json = base_model.BaseModel.to_json
class FileStorage:
"""handles long term storage of all class instances"""
CNC = {
'BaseModel': base_model.BaseModel,
'Amenity': amenity.Amenity,
'City': city.City,
'Place': place.Place,
'Review': review.Review,
'State': state.State,
'User': user.User
}
"""CNC - this variable is a dictionary with:
keys: Class Names
values: Class type (used for instantiation)
"""
__file_path = './dev/file.json'
__objects = {}
def all(self, cls=None):
"""returns private attribute: __objects"""
if cls:
objects_dict = {}
for class_id, obj in FileStorage.__objects.items():
if type(obj).__name__ == cls:
objects_dict[class_id] = obj
return objects_dict
return FileStorage.__objects
def new(self, obj):
"""sets / updates in __objects the obj with key <obj class name>.id"""
bm_id = "{}.{}".format(type(obj).__name__, obj.id)
FileStorage.__objects[bm_id] = obj
def get(self, cls, id):
"""
gets specific object
:param cls: class
:param id: id of instance
:return: object or None
"""
all_class = self.all(cls)
for obj in all_class.values():
if id == str(obj.id):
return obj
return None
def count(self, cls=None):
"""
count of instances
:param cls: class
:return: number of instances
"""
return len(self.all(cls))
def save(self):
"""serializes __objects to the JSON file (path: __file_path)"""
fname = FileStorage.__file_path
d = {}
for bm_id, bm_obj in FileStorage.__objects.items():
d[bm_id] = bm_obj.to_json()
with open(fname, mode='w+', encoding='utf-8') as f_io:
json.dump(d, f_io)
def reload(self):
"""if file exists, deserializes JSON file to __objects, else nothing"""
fname = FileStorage.__file_path
FileStorage.__objects = {}
try:
with open(fname, mode='r', encoding='utf-8') as f_io:
new_objs = json.load(f_io)
except:
return
for o_id, d in new_objs.items():
k_cls = d['__class__']
d.pop("__class__", None)
d["created_at"] = datetime.strptime(d["created_at"],
"%Y-%m-%d %H:%M:%S.%f")
d["updated_at"] = datetime.strptime(d["updated_at"],
"%Y-%m-%d %H:%M:%S.%f")
FileStorage.__objects[o_id] = FileStorage.CNC[k_cls](**d)
def delete(self, obj=None):
"""deletes obj"""
if obj is None:
return
for k in list(FileStorage.__objects.keys()):
if obj.id == k.split(".")[1] and k.split(".")[0] in str(obj):
FileStorage.__objects.pop(k, None)
self.save()
def close(self):
"""
calls the reload() method for deserialization from JSON to objects
"""
self.reload()