summaryrefslogtreecommitdiff
path: root/src/mailman/commands/cli_inject.py
diff options
context:
space:
mode:
authorBarry Warsaw2017-07-22 03:02:05 +0000
committerBarry Warsaw2017-07-22 03:02:05 +0000
commitf00b94f18e1d82d1488cbcee6053f03423bc2f49 (patch)
tree1a8e56dff0eab71e58e5fc9ecc5f3c614d7edca7 /src/mailman/commands/cli_inject.py
parentf54c045519300f6f70947d1114f46c2b8ae0d368 (diff)
downloadmailman-f00b94f18e1d82d1488cbcee6053f03423bc2f49.tar.gz
mailman-f00b94f18e1d82d1488cbcee6053f03423bc2f49.tar.zst
mailman-f00b94f18e1d82d1488cbcee6053f03423bc2f49.zip
Diffstat (limited to 'src/mailman/commands/cli_inject.py')
-rw-r--r--src/mailman/commands/cli_inject.py135
1 files changed, 61 insertions, 74 deletions
diff --git a/src/mailman/commands/cli_inject.py b/src/mailman/commands/cli_inject.py
index 0203a0c09..5c68b58b6 100644
--- a/src/mailman/commands/cli_inject.py
+++ b/src/mailman/commands/cli_inject.py
@@ -18,92 +18,79 @@
"""The `mailman inject` subcommand."""
import sys
+import click
from mailman.app.inject import inject_text
from mailman.config import config
from mailman.core.i18n import _
from mailman.interfaces.command import ICLISubCommand
from mailman.interfaces.listmanager import IListManager
+from mailman.utilities.options import I18nCommand
from public import public
from zope.component import getUtility
from zope.interface import implementer
+def show_queues(ctx, param, value):
+ if value:
+ print('Available queues:')
+ for switchboard in sorted(config.switchboards):
+ print(' ', switchboard)
+ sys.exit(0)
+ # Returning None tells click to process the rest of the command line.
+
+
+@click.command(
+ cls=I18nCommand,
+ help=_("Inject a message from a file into a mailing list's queue."))
+@click.option(
+ '--queue', '-q',
+ help=_("""\
+ The name of the queue to inject the message to. QUEUE must be one of the
+ directories inside the qfiles directory. If omitted, the incoming queue is
+ used."""))
+@click.option(
+ '--show', '-s',
+ is_flag=True, default=False, is_eager=True, expose_value=False,
+ callback=show_queues,
+ help=_('Show a list of all available queue names and exit.'))
+@click.option(
+ '--filename', '-f', 'message_file',
+ type=click.File(encoding='utf-8'),
+ help=_("""\
+ Name of file containing the message to inject. If not given, or
+ '-' (without the quotes) standard input is used."""))
+@click.option(
+ '--metadata', '-m', 'keywords',
+ multiple=True, metavar='KEY=VALUE',
+ help=_("""\
+ Additional metadata key/value pairs to add to the message metadata
+ dictionary. Use the format key=value. Multiple -m options are
+ allowed."""))
+@click.argument('listspec')
+@click.pass_context
+def inject(ctx, queue, message_file, keywords, listspec):
+ mlist = getUtility(IListManager).get(listspec)
+ if mlist is None:
+ ctx.fail(_('No such list: $listspec'))
+ queue_name = ('in' if queue is None else queue)
+ switchboard = config.switchboards.get(queue_name)
+ if switchboard is None:
+ ctx.fail(_('No such queue: $queue'))
+ try:
+ message_text = message_file.read()
+ except KeyboardInterrupt:
+ print('Interrupted')
+ sys.exit(1)
+ kws = {}
+ for keyvalue in keywords:
+ key, equals, value = keyvalue.partition('=')
+ kws[key] = value
+ inject_text(mlist, message_text, switchboard=queue, **kws)
+
+
@public
@implementer(ICLISubCommand)
class Inject:
- """Inject a message from a file into a mailing list's queue."""
-
name = 'inject'
-
- def add(self, parser, command_parser):
- """See `ICLISubCommand`."""
- self.parser = parser
- command_parser.add_argument(
- '-q', '--queue',
- help=_("""
- The name of the queue to inject the message to. QUEUE must be one
- of the directories inside the qfiles directory. If omitted, the
- incoming queue is used."""))
- command_parser.add_argument(
- '-s', '--show',
- action='store_true', default=False,
- help=_('Show a list of all available queue names and exit.'))
- command_parser.add_argument(
- '-f', '--filename',
- help=_("""
- Name of file containing the message to inject. If not given, or
- '-' (without the quotes) standard input is used."""))
- # Required positional argument.
- command_parser.add_argument(
- 'listname', metavar='LISTNAME', nargs=1,
- help=_("""
- The 'fully qualified list name', i.e. the posting address of the
- mailing list to inject the message into."""))
- command_parser.add_argument(
- '-m', '--metadata',
- dest='keywords', action='append', default=[], metavar='KEY=VALUE',
- help=_("""
- Additional metadata key/value pairs to add to the message metadata
- dictionary. Use the format key=value. Multiple -m options are
- allowed."""))
-
- def process(self, args):
- """See `ICLISubCommand`."""
- # Process --show first; if given, print output and exit, ignoring all
- # other command line switches.
- if args.show:
- print('Available queues:')
- for switchboard in sorted(config.switchboards):
- print(' ', switchboard)
- return
- # Could be None or sequence of length 0.
- if args.listname is None:
- self.parser.error(_('List name is required'))
- return
- assert len(args.listname) == 1, (
- 'Unexpected positional arguments: %s' % args.listname)
- fqdn_listname = args.listname[0]
- mlist = getUtility(IListManager).get(fqdn_listname)
- if mlist is None:
- self.parser.error(_('No such list: $fqdn_listname'))
- return
- queue = ('in' if args.queue is None else args.queue)
- switchboard = config.switchboards.get(queue)
- if switchboard is None:
- self.parser.error(_('No such queue: $queue'))
- return
- if args.filename in (None, '-'):
- try:
- message_text = sys.stdin.read()
- except KeyboardInterrupt:
- print('Interrupted')
- sys.exit(1)
- else:
- with open(args.filename) as fp:
- message_text = fp.read()
- keywords = {}
- for keyvalue in args.keywords:
- key, equals, value = keyvalue.partition('=')
- keywords[key] = value
- inject_text(mlist, message_text, switchboard=queue, **keywords)
+ command = inject