Skip to content

Commit

Permalink
Add pipeto --as_file option. Resolve #703.
Browse files Browse the repository at this point in the history
There just doesn't seem to be any way to invoke a usable editor with a
subprocess that's passed stdin. Therefore this option doesn't literally
pipe mail into the command but writes mail to a temporary file and
passes the filename at the end of the external command.
  • Loading branch information
ryneeverett committed Mar 23, 2020
1 parent 21832c5 commit 6a53e26
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
37 changes: 29 additions & 8 deletions alot/commands/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from email.utils import getaddresses, parseaddr
from email.message import Message

import urwid
from io import BytesIO

from . import Command, registerCommand
Expand Down Expand Up @@ -634,6 +635,8 @@ def matches(msgt):
'choices': [
'raw', 'decoded', 'id', 'filepath', 'mimepart',
'plain', 'html']}),
(['--as_file'], {'action': 'store_true',
'help': 'pass mail as a file to the given application'}),
(['--separately'], {'action': 'store_true',
'help': 'call command once for each message'}),
(['--background'], {'action': 'store_true',
Expand All @@ -651,7 +654,7 @@ class PipeCommand(Command):
repeatable = True

def __init__(self, cmd, all=False, separately=False, background=False,
shell=False, notify_stdout=False, format='raw',
shell=False, notify_stdout=False, format='raw', as_file=False,
add_tags=False, noop_msg='no command specified',
confirm_msg='', done_msg=None, **kwargs):
"""
Expand All @@ -674,6 +677,8 @@ def __init__(self, cmd, all=False, separately=False, background=False,
'filepath': paths to message files on disk
'mimepart': only pipe the currently selected mime part
:type format: str
:param as_file: pass mail as a file to the given application
:type as_file: bool
:param add_tags: add 'Tags' header to the message
:type add_tags: bool
:param noop_msg: error notification to show if `cmd` is empty
Expand All @@ -694,6 +699,7 @@ def __init__(self, cmd, all=False, separately=False, background=False,
self.shell = shell
self.notify_stdout = notify_stdout
self.output_format = format
self.as_file = as_file
self.add_tags = add_tags
self.noop_msg = noop_msg
self.confirm_msg = confirm_msg
Expand Down Expand Up @@ -758,14 +764,29 @@ async def apply(self, ui):
self.cmd = [' '.join(self.cmd)]

# do the monkey
def callback(out):
if self.notify_stdout:
ui.notify(out)
if self.done_msg:
ui.notify(self.done_msg)

for mail in pipestrings:
await ui.apply_command(ExternalCommand(self.cmd,
cmd = self.cmd

# Pass mail as temporary file rather than piping through stdin.
if self.as_file:
with tempfile.NamedTemporaryFile(delete=False) as tmpfile:
tmpfile.write(mail.encode(urwid.util.detected_encoding))
tempfile_name = tmpfile.name
mail = None
if self.shell:
cmd = [' '.join([cmd[0], tempfile_name])]
else:
cmd.append(tempfile_name)

def callback(out):
if self.as_file:
os.unlink(tempfile_name)
if self.notify_stdout:
ui.notify(out)
if self.done_msg:
ui.notify(self.done_msg)

await ui.apply_command(ExternalCommand(cmd,
stdin=mail,
shell=True,
thread=self.background,
Expand Down
1 change: 1 addition & 0 deletions docs/source/usage/modes/thread.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ The following commands are available in thread mode:
optional arguments
:---all: pass all messages
:---format: output format; valid choices are: 'raw','decoded','id','filepath','mimepart','plain','html' (defaults to: 'raw')
:---as_file: pass mail as a file to the given application
:---separately: call command once for each message
:---background: don't stop the interface
:---add_tags: add 'Tags' header to the message
Expand Down

0 comments on commit 6a53e26

Please sign in to comment.