1
0

move methods from wallet_db to json_db

the goal of this commit is to call JsonDB.__init__ with data,
not an empty dict
This commit is contained in:
ThomasV
2023-06-24 12:12:32 +02:00
parent c85139009a
commit 411098f293
2 changed files with 44 additions and 33 deletions

View File

@@ -27,6 +27,7 @@ import copy
import json
from . import util
from .util import WalletFileException
from .logging import Logger
JsonDBJsonEncoder = util.MyEncoder
@@ -166,8 +167,20 @@ class JsonDB(Logger):
def __init__(self, data):
Logger.__init__(self)
self.lock = threading.RLock()
self.data = data
self._modified = False
# load data
if data:
self.load_data(data)
else:
self.data = {}
def load_data(self, s):
try:
self.data = json.loads(s)
except Exception:
raise WalletFileException("Cannot read wallet file. (parsing failed)")
if not isinstance(self.data, dict):
raise WalletFileException("Malformed wallet file (not dict)")
def set_modified(self, b):
with self.lock:
@@ -250,3 +263,18 @@ class JsonDB(Logger):
else:
v = constructor(v)
return v
def write(self, storage: 'WalletStorage'):
with self.lock:
self._write(storage)
def _write(self, storage: 'WalletStorage'):
if threading.current_thread().daemon:
self.logger.warning('daemon thread cannot write db')
return
if not self.modified():
return
json_str = self.dump(human_readable=not storage.is_encrypted())
storage.write(json_str)
self.set_modified(False)