diff options
author | Patrick Totzke <patricktotzke@gmail.com> | 2011-09-23 08:55:34 -0700 |
---|---|---|
committer | Patrick Totzke <patricktotzke@gmail.com> | 2011-09-23 08:55:34 -0700 |
commit | 667c8378410e3e2828604e82cb694b4626d1cd08 (patch) | |
tree | fc962e91a9e845d93501107788b58f5405d5d172 /alot | |
parent | 035fac6cfd4195b14177f6d8fdc76252d580685c (diff) | |
parent | 7aa14cee565777939411e70508c73f8688e9dbac (diff) |
Merge pull request #59 from teythoon/enhance_external_command
Enhance external command
Diffstat (limited to 'alot')
-rw-r--r-- | alot/command.py | 18 | ||||
-rw-r--r-- | alot/helper.py | 9 |
2 files changed, 23 insertions, 4 deletions
diff --git a/alot/command.py b/alot/command.py index d719ca81..046b50db 100644 --- a/alot/command.py +++ b/alot/command.py @@ -140,11 +140,13 @@ class RefreshCommand(Command): class ExternalCommand(Command): """calls external command""" - def __init__(self, commandstring, spawn=False, refocus=True, + def __init__(self, commandstring, path=None, spawn=False, refocus=True, in_thread=False, on_success=None, **kwargs): """ :param commandstring: the command to call :type commandstring: str + :param path: a path to a file (or None) + :type path: str :param spawn: run command in a new terminal :type spawn: boolean :param in_thread: run asynchronously, don't block alot @@ -155,6 +157,7 @@ class ExternalCommand(Command): :type on_success: callable """ self.commandstring = commandstring + self.path = path self.spawn = spawn self.refocus = refocus self.in_thread = in_thread @@ -174,7 +177,14 @@ class ExternalCommand(Command): write_fd = ui.mainloop.watch_pipe(afterwards) def thread_code(*args): - cmd = self.commandstring + if self.path: + if '{}' in self.commandstring: + cmd = self.commandstring.replace('{}', helper.shell_quote(self.path)) + else: + cmd = '%s %s' % (self.commandstring, helper.shell_quote(self.path)) + else: + cmd = self.commandstring + if self.spawn: cmd = '%s %s' % (settings.config.get('general', 'terminal_cmd'), @@ -202,8 +212,8 @@ class EditCommand(ExternalCommand): else: self.spawn = settings.config.getboolean('general', 'spawn_editor') editor_cmd = settings.config.get('general', 'editor_cmd') - cmd = editor_cmd + ' ' + self.path - ExternalCommand.__init__(self, cmd, spawn=self.spawn, + + ExternalCommand.__init__(self, editor_cmd, path=self.path, spawn=self.spawn, in_thread=self.spawn, **kwargs) diff --git a/alot/helper.py b/alot/helper.py index d20ba844..d7916123 100644 --- a/alot/helper.py +++ b/alot/helper.py @@ -113,3 +113,12 @@ def attach(path, mail, filename=None): part.add_header('Content-Disposition', 'attachment', filename=filename) mail.attach(part) + +def shell_quote(text): + r''' + >>> print(shell_quote("hello")) + 'hello' + >>> print(shell_quote("hello'there")) + 'hello'"'"'there' + ''' + return "'%s'" % text.replace("'", """'"'"'""") |