better code organization
function parameters should be lowercase Fix crash on invalid labels import Added invoice exporting and reduced duplicate code Better exception handling removed json module import some more cleanup Cleaned up some stuff Added exporting contacts
This commit is contained in:
35
lib/util.py
35
lib/util.py
@@ -60,16 +60,18 @@ 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 FileExportFailed(Exception):
|
||||
def __init__(self, reason=''):
|
||||
self.message = str(reason)
|
||||
|
||||
class FileImportFailedEncrypted(FileImportFailed):
|
||||
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.
|
||||
# However unlike other exceptions the user won't be informed.
|
||||
@@ -785,3 +787,24 @@ 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 reason:
|
||||
traceback.print_exc(file=sys.stderr)
|
||||
raise FileExportFailed(str(reason))
|
||||
|
||||
Reference in New Issue
Block a user