From 11320f095eff59e5fd266c747d6d2d4a2cd96637 Mon Sep 17 00:00:00 2001 From: Patrick Totzke Date: Sun, 11 Dec 2011 15:38:05 +0000 Subject: move hook lookups to AlotConfigParser issue #146 --- alot/commands/__init__.py | 4 ++-- alot/commands/envelope.py | 4 ++-- alot/commands/thread.py | 4 ++-- alot/settings.py | 11 +++++++++-- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/alot/commands/__init__.py b/alot/commands/__init__.py index 2fbb8552..76ba0e0c 100644 --- a/alot/commands/__init__.py +++ b/alot/commands/__init__.py @@ -191,8 +191,8 @@ def commandfactory(cmdline, mode='global'): parms.update(forcedparms) logging.debug('PARMS: %s' % parms) - parms['prehook'] = alot.settings.hooks.get('pre_' + cmdname) - parms['posthook'] = alot.settings.hooks.get('post_' + cmdname) + parms['prehook'] = alot.settings.config.get_hook('pre_' + cmdname) + parms['posthook'] = alot.settings.config.get_hook('post_' + cmdname) logging.debug('cmd parms %s' % parms) return cmdclass(**parms) diff --git a/alot/commands/envelope.py b/alot/commands/envelope.py index 94a2e58d..2597e9de 100644 --- a/alot/commands/envelope.py +++ b/alot/commands/envelope.py @@ -166,7 +166,7 @@ class EditCommand(Command): f.close() # call post-edit translate hook - translate = settings.hooks.get('post_edit_translate') + translate = settings.config.get_hook('post_edit_translate') if translate: template = translate(template, ui=ui, dbm=ui.dbman, aman=ui.accountman, log=ui.logger, @@ -187,7 +187,7 @@ class EditCommand(Command): bodytext = self.envelope.body # call pre-edit translate hook - translate = settings.hooks.get('pre_edit_translate') + translate = settings.config.get_hook('pre_edit_translate') if translate: bodytext = translate(bodytext, ui=ui, dbm=ui.dbman, aman=ui.accountman, log=ui.logger, diff --git a/alot/commands/thread.py b/alot/commands/thread.py index 60984f88..464fb91a 100644 --- a/alot/commands/thread.py +++ b/alot/commands/thread.py @@ -42,7 +42,7 @@ class ReplyCommand(Command): # set body text name, address = self.message.get_author() timestamp = self.message.get_date() - qf = settings.hooks.get('reply_prefix') + qf = settings.config.get_hook('reply_prefix') if qf: quotestring = qf(name, address, timestamp, ui=ui, dbm=ui.dbman, aman=ui.accountman, @@ -146,7 +146,7 @@ class ForwardCommand(Command): # set body text name, address = self.message.get_author() timestamp = self.message.get_date() - qf = settings.hooks.get('forward_prefix') + qf = settings.config.get_hook('forward_prefix') if qf: quote = qf(name, address, timestamp, ui=ui, dbm=ui.dbman, aman=ui.accountman, diff --git a/alot/settings.py b/alot/settings.py index 3987fd8c..f7571dcb 100644 --- a/alot/settings.py +++ b/alot/settings.py @@ -40,6 +40,13 @@ class AlotConfigParser(FallbackConfigParser): FallbackConfigParser.__init__(self) self.hooks = None + def get_hook(self, key): + """return hook (`callable`) identified by `key`""" + if self.hooks: + if key in self.hooks.__dict__: + return self.hooks.__dict__[key] + return None + def read(self, file): if not os.path.isfile(file): return @@ -49,9 +56,9 @@ class AlotConfigParser(FallbackConfigParser): hf = os.path.expanduser(self.get('general', 'hooksfile')) if hf is not None: try: - config.hooks = imp.load_source('hooks', hf) + self.hooks = imp.load_source('hooks', hf) except: - pass + logging.debug('unable to load hooks file:%s' % hf) # fix quoted keys / values for section in self.sections(): -- cgit v1.2.3