Commit 047732fa authored by Barry Warsaw's avatar Barry Warsaw

For 'bin/mailman members':

* -a option uses input_filename internally and a metavar of FILENAME
* Change the assertion for when there isn't 1 argument
* Do a better job of converting input to unicode.
parent a078172e
......@@ -54,11 +54,12 @@ class Members:
self.parser = parser
command_parser.add_argument(
'-a', '--add',
dest='filename',
dest='input_filename', metavar='FILENAME',
help=_("""\
Add all member addresses in FILENAME. FILENAME can be '-' to
indicate standard input. Blank lines and lines That start with a
'#' are ignored."""))
'#' are ignored. Without this option, this command displays
mailing list members."""))
# Required positional argument.
command_parser.add_argument(
'listname', metavar='LISTNAME', nargs=1,
......@@ -70,32 +71,29 @@ class Members:
def process(self, args):
"""See `ICLISubCommand`."""
assert len(args.listname) == 1, (
'Unexpected positional arguments: %s' % args.listname)
assert len(args.listname) == 1, 'Missing mailing list name'
fqdn_listname = args.listname[0]
mlist = getUtility(IListManager).get(fqdn_listname)
if mlist is None:
self.parser.error(_('No such list: $fqdn_listname'))
if args.filename is None:
if args.input_filename is None:
for address in sorted(mlist.members.addresses,
key=attrgetter('address')):
print formataddr((address.real_name, address.original_address))
return
elif args.filename == '-':
elif args.input_filename == '-':
fp = sys.stdin
else:
fp = codecs.open(args.filename, 'r', 'utf-8')
fp = codecs.open(args.input_filename, 'r', 'utf-8')
try:
for line in fp:
# Ignore blank lines and lines that start with a '#'.
if line.startswith('#') or len(line.strip()) == 0:
continue
# Parse the line and ensure that the values are unicodes.
real_name, email = parseaddr(line)
# If not given in the input data, parseaddr() will return the
# empty string, as opposed to the empty unicode. We need a
# unicode real name here.
if real_name == '':
real_name = u''
real_name = real_name.decode(fp.encoding)
email = email.decode(fp.encoding)
try:
add_member(mlist, email, real_name, None,
DeliveryMode.regular,
......
......@@ -7,7 +7,7 @@ You can add members to a mailing list from the command line.
>>> mlist = create_list('test@example.com')
>>> class FakeArgs:
... filename = None
... input_filename = None
... listname = None
>>> args = FakeArgs()
......@@ -29,7 +29,7 @@ be parsed by email.utils.parseaddr().
... ):
... print >> fp, address
>>> args.filename = path
>>> args.input_filename = path
>>> args.listname = [mlist.fqdn_listname]
>>> command.process(args)
......@@ -41,6 +41,7 @@ taken from standard input.
>>> from StringIO import StringIO
>>> fp = StringIO()
>>> fp.encoding = 'us-ascii'
>>> for address in ('dperson@example.com',
... 'Elly Person <eperson@example.com>',
... 'fperson@example.com (Fred Person)',
......@@ -50,7 +51,7 @@ taken from standard input.
>>> import sys
>>> sys.stdin = fp
>>> args.filename = '-'
>>> args.input_filename = '-'
>>> command.process(args)
>>> sys.stdin = sys.__stdin__
......@@ -69,7 +70,7 @@ Blank lines and lines that begin with '#' are ignored.
... ):
... print >> fp, address
>>> args.filename = path
>>> args.input_filename = path
>>> command.process(args)
>>> sorted(address.address for address in mlist.members.addresses)
[u'aperson@example.com', u'bperson@example.com', u'cperson@example.com',
......@@ -101,7 +102,7 @@ Displaying members
With no arguments, the command displays all members of the list.
>>> args.filename = None
>>> args.input_filename = None
>>> command.process(args)
aperson@example.com
Bart Person <bperson@example.com>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment