Commit 3ffddba8 authored by Barry Warsaw's avatar Barry Warsaw

- Convert the CLI modules to use print_function.

 - Use the @transactional decorator.
 - Use the transaction context manager.
parent aa12d1c5
......@@ -17,7 +17,7 @@
"""Importing list data into Mailman 3."""
from __future__ import absolute_import, unicode_literals
from __future__ import absolute_import, print_function, unicode_literals
__metaclass__ = type
__all__ = [
......@@ -31,8 +31,8 @@ import cPickle
from zope.component import getUtility
from zope.interface import implements
from mailman.config import config
from mailman.core.i18n import _
from mailman.database.transaction import transactional
from mailman.interfaces.command import ICLISubCommand
from mailman.interfaces.listmanager import IListManager
from mailman.utilities.importer import import_config_pck
......@@ -59,6 +59,7 @@ class Import21:
'pickle_file', metavar='FILENAME', nargs=1,
help=_('The path to the config.pck file to import.'))
@transactional
def process(self, args):
"""See `ICLISubCommand`."""
# Could be None or sequence of length 0.
......@@ -90,10 +91,7 @@ class Import21:
return
else:
if not isinstance(config_dict, dict):
print >> sys.stderr, _(
'Ignoring non-dictionary: {0!r}').format(
config_dict)
print(_('Ignoring non-dictionary: {0!r}').format(
config_dict), file=sys.stderr)
continue
import_config_pck(mlist, config_dict)
# Commit the changes to the database.
config.db.commit()
......@@ -17,7 +17,7 @@
"""The 'lists' subcommand."""
from __future__ import absolute_import, unicode_literals
from __future__ import absolute_import, print_function, unicode_literals
__metaclass__ = type
__all__ = [
......@@ -31,9 +31,9 @@ from zope.component import getUtility
from zope.interface import implements
from mailman.app.lifecycle import create_list, remove_list
from mailman.config import config
from mailman.core.constants import system_preferences
from mailman.core.i18n import _
from mailman.database.transaction import transaction, transactional
from mailman.email.message import UserNotification
from mailman.interfaces.address import (
IEmailValidator, InvalidEmailAddressError)
......@@ -98,11 +98,11 @@ class Lists:
# Maybe no mailing lists matched.
if len(mailing_lists) == 0:
if not args.quiet:
print _('No matching mailing lists found')
print(_('No matching mailing lists found'))
return
count = len(mailing_lists)
if not args.quiet:
print _('$count matching mailing lists found:')
print(_('$count matching mailing lists found:'))
# Calculate the longest identifier.
longest = 0
output = []
......@@ -120,8 +120,8 @@ class Lists:
else:
format_string = '{0:{2}}'
for identifier, description in output:
print format_string.format(
identifier, description, longest, 70 - longest)
print(format_string.format(
identifier, description, longest, 70 - longest))
......@@ -214,13 +214,13 @@ class Create:
self.parser.error(_('Undefined domain: $domain'))
return
# Find the language associated with the code, then set the mailing
# list's preferred language to that. The changes then must be
# committed to the database.
mlist.preferred_language = getUtility(ILanguageManager)[language_code]
config.db.commit()
# list's preferred language to that.
language_manager = getUtility(ILanguageManager)
with transaction():
mlist.preferred_language = language_manager[language_code]
# Do the notification.
if not args.quiet:
print _('Created mailing list: $mlist.fqdn_listname')
print(_('Created mailing list: $mlist.fqdn_listname'))
if args.notify:
d = dict(
listname = mlist.fqdn_listname,
......@@ -262,11 +262,12 @@ class Remove:
The 'fully qualified list name', i.e. the posting address of the
mailing list."""))
@transactional
def process(self, args):
"""See `ICLISubCommand`."""
def log(message):
if not args.quiet:
print message
print(message)
assert len(args.listname) == 1, (
'Unexpected positional arguments: %s' % args.listname)
fqdn_listname = args.listname[0]
......@@ -277,4 +278,3 @@ class Remove:
else:
log(_('Removed list: $fqdn_listname'))
remove_list(fqdn_listname, mlist)
config.db.commit()
......@@ -37,6 +37,7 @@ from zope.interface import implements
from mailman.app.membership import add_member
from mailman.config import config
from mailman.core.i18n import _
from mailman.database.transaction import transactional
from mailman.interfaces.command import ICLISubCommand
from mailman.interfaces.listmanager import IListManager
from mailman.interfaces.member import (
......@@ -177,6 +178,7 @@ class Members:
if fp is not sys.stdout:
fp.close()
@transactional
def add_members(self, mlist, args):
"""Add the members in a file to a mailing list.
......@@ -207,9 +209,8 @@ class Members:
except AlreadySubscribedError:
# It's okay if the address is already subscribed, just
# print a warning and continue.
print('Already subscribed (skipping):',
print('Already subscribed (skipping):',
email, display_name)
finally:
if fp is not sys.stdin:
fp.close()
config.db.commit()
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