jellyfin-accounts/jellyfin_accounts/data_store.py

70 lines
2.1 KiB
Python
Raw Normal View History

import json
import datetime
2020-06-21 19:29:53 +00:00
class JSONFile(dict):
2020-06-21 19:21:33 +00:00
"""
Behaves like a dictionary, but automatically
reads and writes to a JSON file (most of the time).
"""
2020-06-21 19:29:53 +00:00
@staticmethod
def readJSON(path):
try:
2020-06-21 19:29:53 +00:00
with open(path, "r") as f:
return json.load(f)
except FileNotFoundError:
return {}
2020-06-21 19:29:53 +00:00
@staticmethod
def writeJSON(path, data):
2020-06-21 19:29:53 +00:00
with open(path, "w") as f:
return f.write(json.dumps(data, indent=4, default=str))
2020-06-21 19:29:53 +00:00
def __init__(self, path, data=None):
self.path = path
if data is None:
super(JSONFile, self).__init__(self.readJSON(self.path))
else:
super(JSONFile, self).__init__(data)
self.writeJSON(self.path, data)
def __getitem__(self, key):
super(JSONFile, self).__init__(self.readJSON(self.path))
return super(JSONFile, self).__getitem__(key)
def __setitem__(self, key, value):
data = self.readJSON(self.path)
data[key] = value
2020-06-21 19:29:53 +00:00
self.writeJSON(self.path, data)
super(JSONFile, self).__init__(data)
def __delitem__(self, key):
data = self.readJSON(self.path)
super(JSONFile, self).__init__(data)
del data[key]
2020-06-21 19:29:53 +00:00
self.writeJSON(self.path, data)
super(JSONFile, self).__delitem__(key)
def __str__(self):
super(JSONFile, self).__init__(self.readJSON(self.path))
return json.dumps(super(JSONFile, self))
class JSONStorage:
2020-06-21 19:29:53 +00:00
def __init__(
self, emails, invites, user_template, user_displayprefs, user_configuration
):
self.emails = JSONFile(path=emails)
self.invites = JSONFile(path=invites)
self.user_template = JSONFile(path=user_template)
self.user_displayprefs = JSONFile(path=user_displayprefs)
self.user_configuration = JSONFile(path=user_configuration)
2020-06-21 19:29:53 +00:00
def __setattr__(self, name, value):
if hasattr(self, name):
path = self.__dict__[name].path
self.__dict__[name] = JSONFile(path=path, data=value)
else:
self.__dict__[name] = value