diff options
author | pazz <patricktotzke@gmail.com> | 2011-07-15 20:06:23 +0100 |
---|---|---|
committer | pazz <patricktotzke@gmail.com> | 2011-07-15 20:06:23 +0100 |
commit | 8e2f701bc3d64edcdc2be1e6c65aef5bfdf9c3b0 (patch) | |
tree | b472f5ec13fb0c32f78aeb31d0af9decdfc8988d | |
parent | 226362b694c37cc08776cf2a8ef9936812779de8 (diff) |
split commands and commandfactory stuff
-rw-r--r-- | alot/buffer.py | 6 | ||||
-rw-r--r-- | alot/commandfactory.py | 119 | ||||
-rw-r--r-- | alot/commands.py (renamed from alot/command.py) | 0 | ||||
-rw-r--r-- | alot/ui.py | 15 |
4 files changed, 131 insertions, 9 deletions
diff --git a/alot/buffer.py b/alot/buffer.py index b4dc3ffe..36818a4e 100644 --- a/alot/buffer.py +++ b/alot/buffer.py @@ -19,7 +19,6 @@ Copyright (C) 2011 Patrick Totzke <patricktotzke@gmail.com> import urwid import widgets -import command from walker import IteratorWalker from itertools import izip_longest @@ -55,7 +54,7 @@ class Buffer: parms = parms.copy() parms.update(self._autoparms) try: - cmd = command.factory(cmdname, **parms) + cmd = commandfactory(cmdname, **parms) self.apply_command(cmd) except AssertionError as e: string = "could not instanciate command %s with params %s" @@ -282,3 +281,6 @@ class EnvelopeBuffer(Buffer): self.body_wgt = widgets.MessageBodyWidget(self.email) displayed_widgets.append(self.body_wgt) self.body = urwid.ListBox(displayed_widgets) + + +from commandfactory import commandfactory diff --git a/alot/commandfactory.py b/alot/commandfactory.py new file mode 100644 index 00000000..544342ca --- /dev/null +++ b/alot/commandfactory.py @@ -0,0 +1,119 @@ +""" +This file is part of alot. + +Alot is free software: you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation, either version 3 of the License, or (at your +option) any later version. + +Alot is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with notmuch. If not, see <http://www.gnu.org/licenses/>. + +Copyright (C) 2011 Patrick Totzke <patricktotzke@gmail.com> +""" +import logging + + +from settings import get_hook +import commands + +commands = { + 'bufferlist': (commands.OpenBufferListCommand, {}), + 'buffer close': (commands.BufferCloseCommand, {}), + 'buffer next': (commands.BufferFocusCommand, {'offset': 1}), + 'buffer refresh': (commands.RefreshCommand, {}), + 'buffer previous': (commands.BufferFocusCommand, {'offset': -1}), + 'exit': (commands.ExitCommand, {}), + 'flush': (commands.FlushCommand, {}), + 'pyshell': (commands.PythonShellCommand, {}), + 'search': (commands.SearchCommand, {}), + 'shellescape': (commands.ExternalCommand, {}), + 'taglist': (commands.TagListCommand, {}), + 'edit': (commands.EditCommand, {}), + + 'buffer_focus': (commands.BufferFocusCommand, {}), + 'compose': (commands.ComposeCommand, {}), + 'open_thread': (commands.OpenThreadCommand, {}), + 'open_envelope': (commands.OpenEnvelopeCommand, {}), + 'search prompt': (commands.SearchPromptCommand, {}), + 'refine_search_prompt': (commands.RefineSearchPromptCommand, {}), + 'send': (commands.SendMailCommand, {}), + 'thread_tag_prompt': (commands.ThreadTagPromptCommand, {}), + 'toggle_thread_tag': (commands.ToggleThreadTagCommand, {'tag': 'inbox'}), + } + + +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'] = get_hook('pre_' + cmdname) + parms['posthook'] = get_hook('post_' + cmdname) + + logging.debug('cmd parms %s' % parms) + return cmdclass(**parms) + else: + logging.error('there is no command %s' % cmdname) + + +aliases = {'bc': 'buffer close', + 'bn': 'buffer next', + 'bp': 'buffer previous', + 'br': 'buffer refresh', + 'refresh': 'buffer refresh', + 'ls': 'bufferlist', + 'quit': 'exit', +} + + +def interpret_commandline(cmdline): + if not cmdline: + return None + logging.debug(cmdline + '"') + args = cmdline.strip().split(' ', 1) + cmd = args[0] + params = args[1:] + + # unfold aliases + if cmd in aliases: + cmd = aliases[cmd] + + # buffer commands depend on first parameter only + if cmd == 'buffer' and (params) == 1: + cmd = cmd + params[0] + # allow to shellescape without a space after '!' + if cmd.startswith('!'): + params = cmd[1:] + ''.join(params) + cmd = 'shellescape' + + if not params: + if cmd in ['exit', 'flush', 'pyshell', 'taglist', 'buffer close', + 'buffer next', 'buffer previous', 'buffer refresh', + 'bufferlist']: + return commandfactory(cmd) + else: + return None + else: + if cmd == 'search': + return commandfactory(cmd, query=params[0]) + elif cmd == 'shellescape': + return commandfactory(cmd, commandstring=params) + elif cmd == 'edit': + filepath = params[0] + if os.path.isfile(filepath): + return commandfactory(cmd, path=filepath) + + else: + return None diff --git a/alot/command.py b/alot/commands.py index b7a839a4..b7a839a4 100644 --- a/alot/command.py +++ b/alot/commands.py @@ -22,9 +22,10 @@ from urwid.command_map import command_map from settings import config from settings import get_palette -import command -from widgets import CompleteEdit from buffer import BufferListBuffer +from commandfactory import commandfactory +from commandfactory import interpret_commandline +from widgets import CompleteEdit from completion import CommandCompleter @@ -62,13 +63,13 @@ class UI: 'shift tab': ('buffer prev', {}), '\\': ('search prompt', {}), 'q': ('exit', {}), - ';': ('buffer list', {}), + ';': ('bufferlist', {}), 'L': ('taglist', {}), 's': ('shell', {}), '@': ('buffer refresh', {}), 'm': ('compose', {}), } - cmd = command.factory('search', query=initialquery) + cmd = commandfactory('search', query=initialquery) self.apply_command(cmd) self.mainloop.run() @@ -120,7 +121,7 @@ class UI: cmdline = self.prompt(prefix=':', completer=CommandCompleter(self.dbman)) if cmdline: - cmd = command.interpret(cmdline) + cmd = interpret_commandline(cmdline) if cmd: self.apply_command(cmd) else: @@ -140,7 +141,7 @@ class UI: self.logger.error(string % (buf, self.buffers)) elif len(buffers) == 1: self.logger.info('closing the last buffer, exiting') - cmd = command.factory('exit') + cmd = commandfactory('exit') self.apply_command(cmd) else: if self.current_buffer == buf: @@ -230,7 +231,7 @@ class UI: if key in self.bindings: self.logger.debug("got globally bounded key: %s" % key) (cmdname, parms) = self.bindings[key] - cmd = command.factory(cmdname, **parms) + cmd = commandfactory(cmdname, **parms) self.apply_command(cmd) elif key == ':': self.commandprompt() |