crunk
1 year ago
6 changed files with 69 additions and 62 deletions
@ -1,60 +1,61 @@ |
|||||
|
from json import dumps, loads |
||||
|
from logging import DEBUG, INFO, basicConfig, getLogger |
||||
from os import environ, mkdir |
from os import environ, mkdir |
||||
from os.path import exists |
from os.path import exists |
||||
from pathlib import Path |
from pathlib import Path |
||||
from logging import DEBUG, INFO, basicConfig, getLogger |
|
||||
from json import dumps, loads |
|
||||
|
|
||||
class SimpleJsonStorage(dict): |
class SimpleJsonStorage(dict): |
||||
"""A simple json file. |
"""A simple json file. |
||||
It is a dictionary which saves to disk on all writes. It is optimised for |
It is a dictionary which saves to disk on all writes. It is optimised for |
||||
ease of hacking and accessibility and not for performance or efficiency. |
ease of hacking and accessibility and not for performance or efficiency. |
||||
Written by decentral1se, as part of: |
Written by decentral1se, as part of: |
||||
https://git.vvvvvvaria.org/decentral1se/xbotlib/src/branch/main/xbotlib.py |
https://git.vvvvvvaria.org/decentral1se/xbotlib/src/branch/main/xbotlib.py |
||||
""" |
""" |
||||
|
|
||||
def __init__(self, filename, log, *args, **kwargs): |
def __init__(self, filename, log, *args, **kwargs): |
||||
"""Initialise the object.""" |
"""Initialise the object.""" |
||||
self.filename = Path(filename).absolute() |
self.filename = Path(filename).absolute() |
||||
self.log = getLogger(__name__) |
self.log = getLogger(__name__) |
||||
|
|
||||
self._loads() |
self._loads() |
||||
self.update(*args, **kwargs) |
self.update(*args, **kwargs) |
||||
|
|
||||
def _loads(self): |
def _loads(self): |
||||
"""Load the file.""" |
"""Load the file.""" |
||||
if not exists(self.filename): |
if not exists(self.filename): |
||||
return |
return |
||||
|
|
||||
try: |
try: |
||||
with open(self.filename, "r") as handle: |
with open(self.filename, "r") as handle: |
||||
self.update(loads(handle.read())) |
self.update(loads(handle.read())) |
||||
except Exception as exception: |
except Exception as exception: |
||||
message = f"Loading file storage failed: {exception}" |
message = f"Loading file storage failed: {exception}" |
||||
self.log.error(message, exc_info=exception) |
self.log.error(message, exc_info=exception) |
||||
exit(1) |
exit(1) |
||||
|
|
||||
def _dumps(self): |
def _dumps(self): |
||||
"""Save the file to disk.""" |
"""Save the file to disk.""" |
||||
try: |
try: |
||||
with open(self.filename, "w") as handle: |
with open(self.filename, "w") as handle: |
||||
handle.write(dumps(self, indent=4, sort_keys=True)) |
handle.write(dumps(self, indent=4, sort_keys=True)) |
||||
except Exception as exception: |
except Exception as exception: |
||||
message = f"Saving file storage failed: {exception}" |
message = f"Saving file storage failed: {exception}" |
||||
self.log.error(message, exc_info=exception) |
self.log.error(message, exc_info=exception) |
||||
exit(1) |
exit(1) |
||||
|
|
||||
def __setitem__(self, key, val): |
def __setitem__(self, key, val): |
||||
"""Write data to the file.""" |
"""Write data to the file.""" |
||||
super().__setitem__(key, val) |
super().__setitem__(key, val) |
||||
self._dumps() |
self._dumps() |
||||
|
|
||||
def __delitem__(self, key): |
def __delitem__(self, key): |
||||
"""Remove data from the file.""" |
"""Remove data from the file.""" |
||||
super().__delitem__(key) |
super().__delitem__(key) |
||||
self._dumps() |
self._dumps() |
||||
|
|
||||
def update(self, *args, **kwargs): |
def update(self, *args, **kwargs): |
||||
"""Update the file.""" |
"""Update the file.""" |
||||
for k, v in dict(*args, **kwargs).items(): |
for k, v in dict(*args, **kwargs).items(): |
||||
self[k] = v |
self[k] = v |
||||
self._dumps() |
self._dumps() |
||||
|
Loading…
Reference in new issue