summaryrefslogtreecommitdiff
path: root/alot/ui.py
diff options
context:
space:
mode:
authorPatrick Totzke <patricktotzke@gmail.com>2013-02-20 19:29:30 +0000
committerPatrick Totzke <patricktotzke@gmail.com>2013-02-20 21:36:29 +0000
commit41e129aa92c52eb3a8cd19bba6f6dcdcb10200d6 (patch)
tree5ebe9752b55f07aa31328c4148b1b923372aa4f7 /alot/ui.py
parentacb4ca971cd702562e17137b54fef6443f7a001e (diff)
allow asynchronous pre/post command hooks
also, with this a pre-command-hook can cancel the further execution of the command by raising an Exception. cf. issue #395
Diffstat (limited to 'alot/ui.py')
-rw-r--r--alot/ui.py30
1 files changed, 12 insertions, 18 deletions
diff --git a/alot/ui.py b/alot/ui.py
index 78f40c25..b57b97f4 100644
--- a/alot/ui.py
+++ b/alot/ui.py
@@ -563,34 +563,28 @@ class UI(object):
:type cmd: :class:`~alot.commands.Command`
"""
if cmd:
- # call pre- hook
- if cmd.prehook:
- logging.info('calling pre-hook')
- try:
- cmd.prehook(ui=self, dbm=self.dbman)
- except:
- logging.exception('prehook failed')
- return False
-
# define (callback) function that invokes post-hook
def call_posthook(retval_from_apply):
if cmd.posthook:
logging.info('calling post-hook')
- try:
- cmd.posthook(ui=self, dbm=self.dbman)
- except:
- logging.exception('posthook failed')
+ return defer.maybeDeferred(cmd.posthook, ui=self, dbm=self.dbman)
# define error handler for Failures/Exceptions
# raised in cmd.apply()
def errorHandler(failure):
logging.error(failure.getTraceback())
- msg = "Error: %s,\n(check the log for details)"
- self.notify(msg % failure.getErrorMessage(), priority='error')
+ errmsg = failure.getErrorMessage()
+ if errmsg:
+ msg = "%s\n(check the log for details)"
+ self.notify(msg % failure.getErrorMessage(), priority='error')
# call cmd.apply
- logging.info('apply command: %s' % cmd)
- d = defer.maybeDeferred(cmd.apply, self)
- d.addErrback(errorHandler)
+ def call_apply(ignored):
+ return defer.maybeDeferred(cmd.apply, self)
+
+ prehook = cmd.prehook or (lambda **kwargs: None)
+ d = defer.maybeDeferred(prehook, ui=self, dbm=self.dbman)
+ d.addCallback(call_apply)
d.addCallback(call_posthook)
+ d.addErrback(errorHandler)
return d