Skip to content

Commit

Permalink
Split offline-related functionality from cli.run into FlaskClient
Browse files Browse the repository at this point in the history
- prepare for #53
- simplify return values of safely_run()
- introduce Client.latest_exception attribute
  • Loading branch information
pylipp committed Dec 28, 2019
1 parent c93d842 commit b71bc27
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
4 changes: 1 addition & 3 deletions financeager/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ def _info(message):
formatting_options[option] = params.pop(option)

client = comm.client(configuration=configuration, sinks=sinks)
success, _ = client.safely_run(command, **params)

if success:
if client.safely_run(command, **params):
exit_code = SUCCESS

client.shutdown()
Expand Down
38 changes: 23 additions & 15 deletions financeager/communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,35 @@ def __init__(self, *, sinks):
"""
self.proxy = None
self.sinks = sinks
self.latest_exception = None

def safely_run(self, command, **params):
"""Execute self.proxy.run() while handling any errors. Return indicators
about whether execution was successful, and whether to store the
requested command offline, if execution failed due to a service-sided
error.
"""Execute self.proxy.run() while handling any errors.
A caught exception is stored in the 'latest_exception' attribute.
Return whether execution was successful.
:return: tuple(bool, bool)
:return: bool
"""
store_offline = False
success = False

try:
self.sinks.info(self.proxy.run(command, **params))
self.latest_exception = None
success = True
except InvalidRequest as e:
# Command is erroneous and hence not stored offline
self.sinks.error(e)
self.latest_exception = e

except CommunicationError as e:
self.sinks.error(e)
store_offline = True
except Exception:
self.latest_exception = e

except Exception as e:
self.sinks.error("Unexpected error: {}".format(
traceback.format_exc()))
store_offline = True
self.latest_exception = e

return success, store_offline
return success

def shutdown(self):
"""Routine to run at the end of the Client lifecycle."""
Expand All @@ -82,24 +84,30 @@ def safely_run(self, command, **params):
"""Execute base functionality.
If successful, attempt to recover offline backup. Otherwise store
request in offline backup.
Return whether execution was successful.
:return: tuple(bool, bool)
:return: bool
"""
success, store_offline = super().safely_run(command, **params)
success = super().safely_run(command, **params)

if success:
try:
# Avoid recursion by passing base class for invoking safely_run
if offline.recover(super()):
self.sinks.info("Recovered offline backup.")

except OfflineRecoveryError:
self.sinks.error("Offline backup recovery failed!")
success = False

if store_offline and offline.add(command, **params):
# If request was erroneous, it's not supposed to be stored offline
if not isinstance(self.latest_exception, InvalidRequest) and\
self.latest_exception is not None and\
offline.add(command, **params):
self.sinks.info(
"Stored '{}' request in offline backup.".format(command))

return success, store_offline
return success


class LocalServerClient(Client):
Expand Down
4 changes: 1 addition & 3 deletions financeager/offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ def _recover_data(client, content):
"""
while len(content):
data = content.pop()
success, _ = client.safely_run(**data)

if not success:
if not client.safely_run(**data):
return data


Expand Down

0 comments on commit b71bc27

Please sign in to comment.