From 8a7ef34b079786ddeba73e8beba11af660d9433e Mon Sep 17 00:00:00 2001 From: Barry Warsaw Date: Wed, 15 Jun 2011 08:24:14 -0400 Subject: Plumb key=value arguments through to the `bin/mailman inject` command. --- src/mailman/commands/cli_inject.py | 17 ++- src/mailman/commands/docs/inject.rst | 226 +++++++++++++++++++++++++++++++++++ src/mailman/commands/docs/inject.txt | 199 ------------------------------ 3 files changed, 240 insertions(+), 202 deletions(-) create mode 100644 src/mailman/commands/docs/inject.rst delete mode 100644 src/mailman/commands/docs/inject.txt (limited to 'src') diff --git a/src/mailman/commands/cli_inject.py b/src/mailman/commands/cli_inject.py index 1d019d9e0..a3e61e2a4 100644 --- a/src/mailman/commands/cli_inject.py +++ b/src/mailman/commands/cli_inject.py @@ -50,7 +50,7 @@ class Inject: self.parser = parser command_parser.add_argument( '-q', '--queue', - type=unicode, help=_("""\ + type=unicode, 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.""")) @@ -66,9 +66,16 @@ class Inject: # Required positional argument. command_parser.add_argument( 'listname', metavar='LISTNAME', nargs=1, - help=_("""\ + 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`.""" @@ -104,4 +111,8 @@ class Inject: else: with open(args.filename) as fp: message_text = fp.read() - inject_text(mlist, message_text, switchboard=queue) + keywords = {} + for keyvalue in args.keywords: + key, equals, value = keyvalue.partition('=') + keywords[key] = value + inject_text(mlist, message_text, switchboard=queue, **keywords) diff --git a/src/mailman/commands/docs/inject.rst b/src/mailman/commands/docs/inject.rst new file mode 100644 index 000000000..1c0843ff3 --- /dev/null +++ b/src/mailman/commands/docs/inject.rst @@ -0,0 +1,226 @@ +============================== +Command line message injection +============================== + +You can inject a message directly into a queue directory via the command +line. +:: + + >>> from mailman.commands.cli_inject import Inject + >>> command = Inject() + + >>> class FakeArgs: + ... queue = None + ... show = False + ... filename = None + ... listname = None + ... keywords = [] + >>> args = FakeArgs() + + >>> class FakeParser: + ... def error(self, message): + ... print message + >>> command.parser = FakeParser() + +It's easy to find out which queues are available. +:: + + >>> args.show = True + >>> command.process(args) + Available queues: + archive + bad + bounces + command + digest + in + lmtp + maildir + news + out + pipeline + rest + retry + shunt + virgin + + >>> args.show = False + +Usually, the text of the message to inject is in a file. + + >>> import os, tempfile + >>> fd, filename = tempfile.mkstemp() + >>> with os.fdopen(fd, 'w') as fp: + ... print >> fp, """\ + ... From: aperson@example.com + ... To: test@example.com + ... Subject: testing + ... + ... This is a test message. + ... """ + +However, the mailing list name is always required. + + >>> args.filename = filename + >>> command.process(args) + List name is required + +Let's provide a list name and try again. +:: + + >>> mlist = create_list('test@example.com') + >>> transaction.commit() + + >>> in_queue = config.switchboards['in'] + >>> len(in_queue.files) + 0 + >>> args.listname = ['test@example.com'] + >>> command.process(args) + +By default, the incoming queue is used. +:: + + >>> len(in_queue.files) + 1 + + >>> from mailman.testing.helpers import get_queue_messages + >>> item = get_queue_messages('in')[0] + >>> print item.msg.as_string() + From: aperson@example.com + To: test@example.com + Subject: testing + Message-ID: ... + Date: ... + + This is a test message. + + + + >>> dump_msgdata(item.msgdata) + _parsemsg : False + listname : test@example.com + original_size: 90 + version : 3 + +But a different queue can be specified on the command line. +:: + + >>> args.queue = 'virgin' + >>> command.process(args) + + >>> len(in_queue.files) + 0 + >>> virgin_queue = config.switchboards['virgin'] + >>> len(virgin_queue.files) + 1 + >>> item = get_queue_messages('virgin')[0] + >>> print item.msg.as_string() + From: aperson@example.com + To: test@example.com + Subject: testing + Message-ID: ... + Date: ... + + This is a test message. + + + + >>> dump_msgdata(item.msgdata) + _parsemsg : False + listname : test@example.com + original_size: 90 + version : 3 + + +Standard input +============== + +The message text can also be provided on standard input. +:: + + >>> from StringIO import StringIO + + # Remember: we've got unicode literals turned on. + >>> standard_in = StringIO(str("""\ + ... From: bperson@example.com + ... To: test@example.com + ... Subject: another test + ... + ... This is another test message. + ... """)) + + >>> import sys + >>> sys.stdin = standard_in + >>> args.filename = '-' + >>> args.queue = None + + >>> command.process(args) + >>> len(in_queue.files) + 1 + >>> item = get_queue_messages('in')[0] + >>> print item.msg.as_string() + From: bperson@example.com + To: test@example.com + Subject: another test + Message-ID: ... + Date: ... + + This is another test message. + + + + >>> dump_msgdata(item.msgdata) + _parsemsg : False + listname : test@example.com + original_size: 100 + version : 3 + + # Clean up. + >>> sys.stdin = sys.__stdin__ + >>> args.filename = filename + + +Metadata +======== + +Additional metadata keys can be provided on the command line. These key/value +pairs get added to the message metadata dictionary when the message is +injected. +:: + + >>> args = FakeArgs() + >>> args.filename = filename + >>> args.listname = ['test@example.com'] + >>> args.keywords = ['foo=one', 'bar=two'] + >>> command.process(args) + + >>> items = get_queue_messages('in') + >>> dump_msgdata(items[0].msgdata) + _parsemsg : False + bar : two + foo : one + listname : test@example.com + original_size: 90 + version : 3 + + +Errors +====== + +It is an error to specify a queue that doesn't exist. + + >>> args.queue = 'xxbogusxx' + >>> command.process(args) + No such queue: xxbogusxx + +It is also an error to specify a mailing list that doesn't exist. + + >>> args.queue = None + >>> args.listname = ['bogus'] + >>> command.process(args) + No such list: bogus + + +.. + # Clean up the tempfile. + >>> os.remove(filename) diff --git a/src/mailman/commands/docs/inject.txt b/src/mailman/commands/docs/inject.txt deleted file mode 100644 index e8c405b07..000000000 --- a/src/mailman/commands/docs/inject.txt +++ /dev/null @@ -1,199 +0,0 @@ -============================== -Command line message injection -============================== - -You can inject a message directly into a queue directory via the command -line. -:: - - >>> from mailman.commands.cli_inject import Inject - >>> command = Inject() - - >>> class FakeArgs: - ... queue = None - ... show = False - ... filename = None - ... listname = None - >>> args = FakeArgs() - - >>> class FakeParser: - ... def error(self, message): - ... print message - >>> command.parser = FakeParser() - -It's easy to find out which queues are available. -:: - - >>> args.show = True - >>> command.process(args) - Available queues: - archive - bad - bounces - command - digest - in - lmtp - maildir - news - out - pipeline - rest - retry - shunt - virgin - - >>> args.show = False - -Usually, the text of the message to inject is in a file. - - >>> import os, tempfile - >>> fd, filename = tempfile.mkstemp() - >>> with os.fdopen(fd, 'w') as fp: - ... print >> fp, """\ - ... From: aperson@example.com - ... To: test@example.com - ... Subject: testing - ... - ... This is a test message. - ... """ - -However, the mailing list name is always required. - - >>> args.filename = filename - >>> command.process(args) - List name is required - -Let's provide a list name and try again. -:: - - >>> mlist = create_list('test@example.com') - >>> transaction.commit() - - >>> in_queue = config.switchboards['in'] - >>> len(in_queue.files) - 0 - >>> args.listname = ['test@example.com'] - >>> command.process(args) - -By default, the incoming queue is used. -:: - - >>> len(in_queue.files) - 1 - - >>> from mailman.testing.helpers import get_queue_messages - >>> item = get_queue_messages('in')[0] - >>> print item.msg.as_string() - From: aperson@example.com - To: test@example.com - Subject: testing - Message-ID: ... - Date: ... - - This is a test message. - - - - >>> dump_msgdata(item.msgdata) - _parsemsg : False - listname : test@example.com - original_size: 90 - version : 3 - -But a different queue can be specified on the command line. -:: - - >>> args.queue = 'virgin' - >>> command.process(args) - - >>> len(in_queue.files) - 0 - >>> virgin_queue = config.switchboards['virgin'] - >>> len(virgin_queue.files) - 1 - >>> item = get_queue_messages('virgin')[0] - >>> print item.msg.as_string() - From: aperson@example.com - To: test@example.com - Subject: testing - Message-ID: ... - Date: ... - - This is a test message. - - - - >>> dump_msgdata(item.msgdata) - _parsemsg : False - listname : test@example.com - original_size: 90 - version : 3 - - # Clean up the tempfile. - >>> os.remove(filename) - - -Standard input -============== - -The message text can also be provided on standard input. -:: - - >>> from StringIO import StringIO - - # Remember: we've got unicode literals turned on. - >>> standard_in = StringIO(str("""\ - ... From: bperson@example.com - ... To: test@example.com - ... Subject: another test - ... - ... This is another test message. - ... """)) - - >>> import sys - >>> sys.stdin = standard_in - >>> args.filename = '-' - >>> args.queue = None - - >>> command.process(args) - >>> len(in_queue.files) - 1 - >>> item = get_queue_messages('in')[0] - >>> print item.msg.as_string() - From: bperson@example.com - To: test@example.com - Subject: another test - Message-ID: ... - Date: ... - - This is another test message. - - - - >>> dump_msgdata(item.msgdata) - _parsemsg : False - listname : test@example.com - original_size: 100 - version : 3 - - # Clean up. - >>> sys.stdin = sys.__stdin__ - >>> args.filename = filename - - -Errors -====== - -It is an error to specify a queue that doesn't exist. - - >>> args.queue = 'xxbogusxx' - >>> command.process(args) - No such queue: xxbogusxx - -It is also an error to specify a mailing list that doesn't exist. - - >>> args.queue = None - >>> args.listname = ['bogus'] - >>> command.process(args) - No such list: bogus -- cgit v1.2.3-70-g09d2