Commit 66440232 authored by Barry Warsaw's avatar Barry Warsaw

* `mailman conf` now has a `-t/--sort` flag which sorts the output by section

   and then key.  Contributed by Karl-Aksel Puulmann and David Soto
   (LP: 1162492)
parents 23d3a966 483f8861
......@@ -65,10 +65,20 @@ class Conf:
key-values pair from any section matching the given key will be
displayed.
"""))
command_parser.add_argument(
'-t', '--sort',
default=False, action='store_true',
help=_('Sort the output by sections and keys.'))
def _get_value(self, section, key):
return getattr(getattr(config, section), key)
def _sections(self, sort_p):
sections = config.schema._section_schemas
if sort_p:
sections = sorted(sections)
return sections
def _print_full_syntax(self, section, key, value, output):
print('[{}] {}: {}'.format(section, key, value), file=output)
......@@ -78,8 +88,10 @@ class Conf:
def _show_section_error(self, section):
self.parser.error('No such section: {}'.format(section))
def _print_values_for_section(self, section, output):
def _print_values_for_section(self, section, output, sort_p):
current_section = getattr(config, section)
if sort_p:
current_section = sorted(current_section)
for key in current_section:
self._print_full_syntax(section, key,
self._get_value(section, key), output)
......@@ -94,6 +106,7 @@ class Conf:
# Process the command, ignoring the closing of the output file.
section = args.section
key = args.key
sort_p = args.sort
# Case 1: Both section and key are given, so we can directly look up
# the value.
if section is not None and key is not None:
......@@ -106,12 +119,12 @@ class Conf:
# Case 2: Section is given, key is not given.
elif section is not None and key is None:
if self._section_exists(section):
self._print_values_for_section(section, output)
self._print_values_for_section(section, output, sort_p)
else:
self._show_section_error(section)
# Case 3: Section is not given, key is given.
elif section is None and key is not None:
for current_section in config.schema._section_schemas:
for current_section in self._sections(sort_p):
# We have to ensure that the current section actually exists
# and that it contains the given key.
if (self._section_exists(current_section) and
......@@ -124,12 +137,13 @@ class Conf:
# Case 4: Neither section nor key are given, just display all the
# sections and their corresponding key/value pairs.
elif section is None and key is None:
for current_section in config.schema._section_schemas:
for current_section in self._sections(sort_p):
# However, we have to make sure that the current sections and
# key which are being looked up actually exist before trying
# to print them.
if self._section_exists(current_section):
self._print_values_for_section(current_section, output)
self._print_values_for_section(
current_section, output, sort_p)
def process(self, args):
"""See `ICLISubCommand`."""
......
......@@ -14,6 +14,7 @@ a specific key-value pair, or several key-value pairs.
... key = None
... section = None
... output = None
... sort = False
>>> from mailman.commands.cli_conf import Conf
>>> command = Conf()
......@@ -64,5 +65,16 @@ If you specify both a section and a key, you will get the corresponding value.
>>> command.process(FakeArgs)
[email protected]
You can also sort the output. The output is first sorted by section, then by
key.
>>> FakeArgs.key = None
>>> FakeArgs.section = 'shell'
>>> FakeArgs.sort = True
>>> command.process(FakeArgs)
[shell] banner: Welcome to the GNU Mailman shell
[shell] prompt: >>>
[shell] use_ipython: no
.. _`Postfix command postconf(1)`: http://www.postfix.org/postconf.1.html
......@@ -31,6 +31,7 @@ import mock
import tempfile
import unittest
from StringIO import StringIO
from mailman.commands.cli_conf import Conf
from mailman.testing.layers import ConfigLayer
......@@ -40,6 +41,7 @@ class FakeArgs:
section = None
key = None
output = None
sort = False
class FakeParser:
......@@ -99,3 +101,13 @@ class TestConf(unittest.TestCase):
finally:
os.remove(filename)
self.assertEqual(contents, 'no\n')
def test_sort_by_section(self):
self.args.output = '-'
self.args.sort = True
output = StringIO()
with mock.patch('sys.stdout', output):
self.command.process(self.args)
last_line = ''
for line in output.getvalue().splitlines():
self.assertTrue(line > last_line)
......@@ -28,6 +28,12 @@ REST
* Expose ``hide_address`` to the ``.../preferences`` REST API. Contributed
by Sneha Priscilla.
Commands
--------
* `mailman conf` now has a `-t/--sort` flag which sorts the output by section
and then key. Contributed by Karl-Aksel Puulmann and David Soto
(LP: 1162492)
Configuration
-------------
* When creating the initial file system layout in ``var``, e.g. via
......
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