Merge pull request #3951 from SomberNight/file_import_export_unification
File import-export unification
This commit is contained in:
@@ -28,7 +28,7 @@ import sys
|
||||
|
||||
from . import bitcoin
|
||||
from . import dnssec
|
||||
from .util import FileImportFailed, FileImportFailedEncrypted
|
||||
from .util import export_meta, import_meta
|
||||
|
||||
|
||||
class Contacts(dict):
|
||||
@@ -51,18 +51,15 @@ class Contacts(dict):
|
||||
self.storage.put('contacts', dict(self))
|
||||
|
||||
def import_file(self, path):
|
||||
try:
|
||||
with open(path, 'r') as f:
|
||||
d = self._validate(json.loads(f.read()))
|
||||
except json.decoder.JSONDecodeError:
|
||||
traceback.print_exc(file=sys.stderr)
|
||||
raise FileImportFailedEncrypted()
|
||||
except BaseException:
|
||||
traceback.print_exc(file=sys.stdout)
|
||||
raise FileImportFailed()
|
||||
self.update(d)
|
||||
import_meta(path, self._validate, self.load_meta)
|
||||
|
||||
def load_meta(self, data):
|
||||
self.update(data)
|
||||
self.save()
|
||||
|
||||
def export_file(self, filename):
|
||||
export_meta(self, filename)
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
dict.__setitem__(self, key, value)
|
||||
self.save()
|
||||
@@ -120,13 +117,13 @@ class Contacts(dict):
|
||||
return None
|
||||
|
||||
def _validate(self, data):
|
||||
for k,v in list(data.items()):
|
||||
for k, v in list(data.items()):
|
||||
if k == 'contacts':
|
||||
return self._validate(v)
|
||||
if not bitcoin.is_address(k):
|
||||
data.pop(k)
|
||||
else:
|
||||
_type,_ = v
|
||||
_type, _ = v
|
||||
if _type != 'address':
|
||||
data.pop(k)
|
||||
return data
|
||||
|
||||
@@ -40,7 +40,7 @@ except ImportError:
|
||||
from . import bitcoin
|
||||
from . import util
|
||||
from .util import print_error, bh2u, bfh
|
||||
from .util import FileImportFailed, FileImportFailedEncrypted
|
||||
from .util import export_meta, import_meta
|
||||
from . import transaction
|
||||
from . import x509
|
||||
from . import rsakey
|
||||
@@ -468,27 +468,29 @@ class InvoiceStore(object):
|
||||
continue
|
||||
|
||||
def import_file(self, path):
|
||||
try:
|
||||
with open(path, 'r') as f:
|
||||
d = json.loads(f.read())
|
||||
self.load(d)
|
||||
except json.decoder.JSONDecodeError:
|
||||
traceback.print_exc(file=sys.stderr)
|
||||
raise FileImportFailedEncrypted()
|
||||
except BaseException:
|
||||
traceback.print_exc(file=sys.stdout)
|
||||
raise FileImportFailed()
|
||||
def validate(data):
|
||||
return data # TODO
|
||||
import_meta(path, validate, self.on_import)
|
||||
|
||||
def on_import(self, data):
|
||||
self.load(data)
|
||||
self.save()
|
||||
|
||||
def save(self):
|
||||
l = {}
|
||||
def export_file(self, filename):
|
||||
export_meta(self.dump(), filename)
|
||||
|
||||
def dump(self):
|
||||
d = {}
|
||||
for k, pr in self.invoices.items():
|
||||
l[k] = {
|
||||
d[k] = {
|
||||
'hex': bh2u(pr.raw),
|
||||
'requestor': pr.requestor,
|
||||
'txid': pr.tx
|
||||
}
|
||||
self.storage.put('invoices', l)
|
||||
return d
|
||||
|
||||
def save(self):
|
||||
self.storage.put('invoices', self.dump())
|
||||
|
||||
def get_status(self, key):
|
||||
pr = self.get(key)
|
||||
|
||||
37
lib/util.py
37
lib/util.py
@@ -59,15 +59,19 @@ class InvalidPassword(Exception):
|
||||
|
||||
|
||||
class FileImportFailed(Exception):
|
||||
def __init__(self, message=''):
|
||||
self.message = str(message)
|
||||
|
||||
def __str__(self):
|
||||
return _("Failed to import file.")
|
||||
return _("Failed to import from file.") + "\n" + self.message
|
||||
|
||||
|
||||
class FileImportFailedEncrypted(FileImportFailed):
|
||||
class FileExportFailed(Exception):
|
||||
def __init__(self, message=''):
|
||||
self.message = str(message)
|
||||
|
||||
def __str__(self):
|
||||
return (_('Failed to import file.') + ' ' +
|
||||
_('Perhaps it is encrypted...') + '\n' +
|
||||
_('Importing encrypted files is not supported.'))
|
||||
return _("Failed to export to file.") + "\n" + self.message
|
||||
|
||||
|
||||
# Throw this exception to unwind the stack like when an error occurs.
|
||||
@@ -784,3 +788,26 @@ def setup_thread_excepthook():
|
||||
|
||||
def versiontuple(v):
|
||||
return tuple(map(int, (v.split("."))))
|
||||
|
||||
|
||||
def import_meta(path, validater, load_meta):
|
||||
try:
|
||||
with open(path, 'r') as f:
|
||||
d = validater(json.loads(f.read()))
|
||||
load_meta(d)
|
||||
#backwards compatibility for JSONDecodeError
|
||||
except ValueError:
|
||||
traceback.print_exc(file=sys.stderr)
|
||||
raise FileImportFailed(_("Invalid JSON code."))
|
||||
except BaseException as e:
|
||||
traceback.print_exc(file=sys.stdout)
|
||||
raise FileImportFailed(e)
|
||||
|
||||
|
||||
def export_meta(meta, fileName):
|
||||
try:
|
||||
with open(fileName, 'w+') as f:
|
||||
json.dump(meta, f, indent=4, sort_keys=True)
|
||||
except (IOError, os.error) as e:
|
||||
traceback.print_exc(file=sys.stderr)
|
||||
raise FileExportFailed(e)
|
||||
|
||||
Reference in New Issue
Block a user