pythonify man script
This commit is contained in:
parent
ddb7a69f7e
commit
56d1705abd
4 changed files with 129 additions and 219 deletions
28
utils/manager/savefile.py
Normal file
28
utils/manager/savefile.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
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) as f:
|
||||
self.update(json.loads(f.read()))
|
||||
else:
|
||||
self.save()
|
||||
|
||||
def save(self):
|
||||
with open(self.path, 'a+') 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()
|
||||
|
Reference in a new issue