2020-03-21 22:43:14 +01:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
|
|
|
|
from storage import jsonencoder
|
|
|
|
|
|
|
|
|
|
|
|
class Objects:
|
|
|
|
def __init__(self, path: str):
|
|
|
|
self.path = os.path.abspath(path)
|
|
|
|
os.makedirs(os.path.join(self.path, "objects"), exist_ok=True)
|
2020-03-22 19:27:30 +01:00
|
|
|
self.encoder = jsonencoder.Encoder
|
2020-03-21 22:43:14 +01:00
|
|
|
|
|
|
|
def save_object(self, object_name, object_instance):
|
|
|
|
"""Save object into json file"""
|
|
|
|
with open(os.path.join(self.path, "objects", object_name + ".json"), "w") as file:
|
2020-03-22 19:27:30 +01:00
|
|
|
json.dump(object_instance, file, cls=self.encoder)
|
2020-03-21 22:43:14 +01:00
|
|
|
|
|
|
|
def load_object(self, object_name):
|
|
|
|
"""Load object from json file"""
|
|
|
|
if self.save_exists(object_name):
|
|
|
|
with open(os.path.join(self.path, "objects", object_name + ".json"), "r") as f:
|
2020-03-22 19:27:30 +01:00
|
|
|
return json.load(f, object_hook=self.encoder.hook)
|
2020-03-21 22:43:14 +01:00
|
|
|
|
|
|
|
def save_exists(self, object_name):
|
|
|
|
"""Check if json file exists"""
|
|
|
|
return os.access(os.path.join(self.path, "objects", object_name + ".json"), os.R_OK | os.W_OK)
|