diff options
author | pazz <patricktotzke@gmail.com> | 2011-08-04 18:39:56 +0100 |
---|---|---|
committer | pazz <patricktotzke@gmail.com> | 2011-08-04 18:39:56 +0100 |
commit | 58ff02c6424607312ee83464f2247adf70e58cbd (patch) | |
tree | 7974579102d28daa7480cb4adc791de7b8a34f33 | |
parent | 5f17b05d77b0532fdba3208cadac5928235039c8 (diff) |
simplified commandfactory
-rw-r--r-- | alot/command.py | 156 | ||||
-rw-r--r-- | alot/completion.py | 3 |
2 files changed, 70 insertions, 89 deletions
diff --git a/alot/command.py b/alot/command.py index 975628ea..aa9d3797 100644 --- a/alot/command.py +++ b/alot/command.py @@ -724,103 +724,82 @@ class TaglistSelectCommand(Command): ui.apply_command(cmd) -#Factory +aliases = {'clo': 'close', + 'bn': 'bnext', + 'bp': 'bprevious', + 'bcf': 'buffer close focussed', + 'ls': 'bufferlist', + 'quit': 'exit', +} + COMMANDS = { + 'search': { + 'refine': (RefineCommand, {}), + 'refineprompt': (RefinePromptCommand, {}), + 'openthread': (OpenThreadCommand, {}), + 'toggletag': (ToggleThreadTagCommand, {'tag': 'inbox'}), + 'retag': (RetagCommand, {}), + 'retagprompt': (RetagPromptCommand, {}), + }, + 'envelope': { + 'send': (EnvelopeSendCommand, {}), + 'reedit': (EnvelopeEditCommand, {}), + 'subject': (EnvelopeSetCommand, {'key': 'Subject'}), + 'to': (EnvelopeSetCommand, {'key': 'To'}), + }, + 'bufferlist': { + 'closefocussed': (BufferCloseCommand, {'focussed': True}), + 'openfocussed': (BufferFocusCommand, {}), + }, + 'taglist': { + 'select': (TaglistSelectCommand, {}), + }, + 'thread': { + 'reply': (ReplyCommand, {}), + 'groupreply': (ReplyCommand, {'groupreply': True}), + 'forward': (ForwardCommand, {}), + 'bounce': (BounceMailCommand, {}), + }, + 'global': { 'bnext': (BufferFocusCommand, {'offset': 1}), 'bprevious': (BufferFocusCommand, {'offset': -1}), 'bufferlist': (OpenBufferlistCommand, {}), 'close': (BufferCloseCommand, {}), - 'closefocussed': (BufferCloseCommand, {'focussed': True}), - 'openfocussed': (BufferFocusCommand, {}), 'commandprompt': (CommandPromptCommand, {}), 'compose': (ComposeCommand, {}), 'edit': (EditCommand, {}), 'exit': (ExitCommand, {}), 'flush': (FlushCommand, {}), - 'openthread': (OpenThreadCommand, {}), 'prompt': (PromptCommand, {}), 'pyshell': (PythonShellCommand, {}), - 'refine': (RefineCommand, {}), - 'refineprompt': (RefinePromptCommand, {}), 'refresh': (RefreshCommand, {}), 'search': (SearchCommand, {}), 'shellescape': (ExternalCommand, {}), 'taglist': (TagListCommand, {}), - 'toggletag': (ToggleThreadTagCommand, {'tag': 'inbox'}), - - # envelope - 'send': (EnvelopeSendCommand, {}), - 'reedit': (EnvelopeEditCommand, {}), - 'subject': (EnvelopeSetCommand, {'key': 'Subject'}), - 'to': (EnvelopeSetCommand, {'key': 'To'}), - - 'retag': (RetagCommand, {}), - 'retagprompt': (RetagPromptCommand, {}), - # thread - 'reply': (ReplyCommand, {}), - 'groupreply': (ReplyCommand, {'groupreply': True}), - 'forward': (ForwardCommand, {}), - 'bounce': (BounceMailCommand, {}), - - # taglist - 'select': (TaglistSelectCommand, {}), - } - - -def commandfactory(cmdname, **kwargs): - if cmdname in COMMANDS: - (cmdclass, parms) = COMMANDS[cmdname] - parms = parms.copy() - parms.update(kwargs) - for (key, value) in kwargs.items(): - if callable(value): - parms[key] = value() - else: - parms[key] = value + } +} - parms['prehook'] = settings.hooks.get('pre_' + cmdname) - parms['posthook'] = settings.hooks.get('post_' + cmdname) - logging.debug('cmd parms %s' % parms) - return cmdclass(**parms) +def commandfactory(cmdname, mode='global', **kwargs): + if cmdname in COMMANDS[mode]: + (cmdclass, parms) = COMMANDS[mode][cmdname] + elif cmdname in COMMANDS['global']: + (cmdclass, parms) = COMMANDS['global'][cmdname] else: logging.error('there is no command %s' % cmdname) + parms = parms.copy() + parms.update(kwargs) + for (key, value) in kwargs.items(): + if callable(value): + parms[key] = value() + else: + parms[key] = value + parms['prehook'] = settings.hooks.get('pre_' + cmdname) + parms['posthook'] = settings.hooks.get('post_' + cmdname) -aliases = {'clo': 'close', - 'bn': 'bnext', - 'bp': 'bprevious', - 'bcf': 'buffer close focussed', - 'ls': 'bufferlist', - 'quit': 'exit', -} - -globalcommands = [ - 'bnext', - 'bprevious', - 'bufferlist', - 'close', - 'compose', - 'prompt', - 'edit', - 'exit', - 'flush', - 'pyshell', - 'refresh', - 'search', - 'shellescape', - 'taglist', -] - -ALLOWED_COMMANDS = { - 'search': ['refine', 'refineprompt', 'toggletag', 'openthread', 'retag', - 'retagprompt'] + globalcommands, - 'envelope': ['send', 'reedit', 'to', 'subject'] + globalcommands, - 'bufferlist': ['openfocussed', 'closefocussed'] + globalcommands, - 'taglist': ['select'] + globalcommands, - 'thread': globalcommands + ['toggletag', 'reply', 'groupreply', 'bounce', - 'forward'], -} + logging.debug('cmd parms %s' % parms) + return cmdclass(**parms) def interpret_commandline(cmdline, mode): @@ -844,8 +823,8 @@ def interpret_commandline(cmdline, mode): cmd = 'shellescape' # check if this command makes sense in current mode - if cmd not in ALLOWED_COMMANDS[mode]: - logging.debug('not allowed in mode %s: %s' % (mode, cmd)) + if cmd not in COMMANDS[mode] and cmd not in COMMANDS['global']: + logging.debug('unknown command: %s' % (cmd)) return None if not params: # commands that work without parameter @@ -855,31 +834,32 @@ def interpret_commandline(cmdline, mode): 'forward', 'groupreply', 'bounce', 'openthread', 'send', 'reedit', 'select', 'retagprompt']: - return commandfactory(cmd) + logging.debug('no parms for: %s' % (cmd)) + return commandfactory(cmd, mode=mode) else: return None else: if cmd == 'search': - return commandfactory(cmd, query=params) + return commandfactory(cmd, mode=mode, query=params) elif cmd == 'compose': - return commandfactory(cmd, headers={'To': params}) + return commandfactory(cmd, mode=mode, headers={'To': params}) elif cmd == 'prompt': - return commandfactory(cmd, startstring=params) + return commandfactory(cmd, mode=mode, startstring=params) elif cmd == 'refine': - return commandfactory(cmd, query=params) + return commandfactory(cmd, mode=mode, query=params) elif cmd == 'retag': - return commandfactory(cmd, tagsstring=params) + return commandfactory(cmd, mode=mode, tagsstring=params) elif cmd == 'subject': - return commandfactory(cmd, key='Subject', value=params) + return commandfactory(cmd, mode=mode, key='Subject', value=params) elif cmd == 'shellescape': - return commandfactory(cmd, commandstring=params) + return commandfactory(cmd, mode=mode, commandstring=params) elif cmd == 'to': - return commandfactory(cmd, key='To', value=params) + return commandfactory(cmd, mode=mode, key='To', value=params) elif cmd == 'toggletag': - return commandfactory(cmd, tag=params) + return commandfactory(cmd, mode=mode, tag=params) elif cmd == 'edit': filepath = os.path.expanduser(params) if os.path.isfile(filepath): - return commandfactory(cmd, path=filepath) + return commandfactory(cmd, mode=mode, path=filepath) else: return None diff --git a/alot/completion.py b/alot/completion.py index a64776f1..6c6917c8 100644 --- a/alot/completion.py +++ b/alot/completion.py @@ -103,7 +103,8 @@ class CommandCompleter(Completer): def complete(self, original): #TODO refine <tab> should get current querystring - cmdlist = command.ALLOWED_COMMANDS[self.mode] + cmdlist = command.COMMANDS['global'] + cmdlist.update(command.COMMANDS[self.mode]) olen = len(original) return [t[olen:] + '' for t in cmdlist if t.startswith(original)] |