This repository has been archived on 2023-05-02. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
neo/utils/manager/savefile.py
2022-12-09 11:05:13 +01:00

28 lines
710 B
Python

import json
import os
class SaveFile(dict):
def __init__(self, file: str) -> None:
super().__init__()
self.path: str = file
_dir = os.path.dirname(self.path)
if not os.path.exists(_dir):
os.makedirs(_dir)
if os.path.exists(self.path):
with open(self.path, 'w+') as f:
self.update(json.loads(f.read()))
else:
self.save()
def save(self):
with open(self.path, 'w+') as f:
f.write(json.dumps(self, indent=1))
def __getitem__(self, key):
return super().__getitem__(key)
def __setitem__(self, key, value):
super().__setitem__(key, value)
self.save()