43 lines
903 B
Python
43 lines
903 B
Python
import json
|
|
import os
|
|
|
|
|
|
class DataFile(dict):
|
|
def __init__(self, filename: str, create: bool):
|
|
super().__init__()
|
|
self.filename = filename
|
|
|
|
try:
|
|
os.stat(self.filename)
|
|
except OSError as e:
|
|
if not create:
|
|
raise e
|
|
else:
|
|
open(self.filename, "w").close()
|
|
|
|
self.load()
|
|
|
|
def load(self):
|
|
pass
|
|
|
|
def save(self):
|
|
pass
|
|
|
|
|
|
class JSONFile(DataFile):
|
|
def __init__(self, filename: str, create: bool):
|
|
super().__init__(filename, create)
|
|
|
|
def load(self):
|
|
with open(self.filename, "rt") as f:
|
|
c = f.read()
|
|
|
|
if len(c) > 0:
|
|
json_dict = json.loads(c)
|
|
for k in json_dict:
|
|
self[k] = json_dict[k]
|
|
|
|
def save(self):
|
|
with open(self.filename, "wt") as f:
|
|
f.write(json.dumps(self.copy()))
|