Merge lp:~macobo/mailman/macobo-conf_sort_option into lp:mailman

Proposed by Karl-Aksel Puulmann
Status: Merged
Merged at revision: 7221
Proposed branch: lp:~macobo/mailman/macobo-conf_sort_option
Merge into: lp:mailman
Diff against target: 111 lines (+27/-5)
3 files modified
src/mailman/commands/cli_conf.py (+18/-5)
src/mailman/commands/docs/conf.rst (+8/-0)
src/mailman/commands/tests/test_conf.py (+1/-0)
To merge this branch: bzr merge lp:~macobo/mailman/macobo-conf_sort_option
Reviewer Review Type Date Requested Status
Barry Warsaw Approve
Review via email: mp+157469@code.launchpad.net

Description of the change

Since the -s option is taken by --section, I decided to use -x instead (perhaps should be changed).

When doing the changes, I didn't notice that David Soto had already made a branch to fix this issue.

To post a comment you must log in.
7212. By Karl-Aksel Puulmann

Fixed tests broken by previous commit

7213. By Karl-Aksel Puulmann

Added an example to the docs

Revision history for this message
Barry Warsaw (barry) wrote :

Thanks for the contribution to Mailman! Your branch looks quite good, and with just a few modifications, I am merging this branch. I did change -x to -t and added a few tests. I am giving you top credit for fixing the bug but I'm also going to mention David Soto's contribution. His branch lacked tests and there was no merge proposal from him, so your branch provided the bulk of the new code.

Again, thanks!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/mailman/commands/cli_conf.py'
2--- src/mailman/commands/cli_conf.py 2013-03-20 00:34:45 +0000
3+++ src/mailman/commands/cli_conf.py 2013-04-06 07:18:25 +0000
4@@ -65,10 +65,20 @@
5 key-values pair from any section matching the given key will be
6 displayed.
7 """))
8+ command_parser.add_argument(
9+ '-x', '--sort',
10+ default=False, action='store_true',
11+ help=_("Sort the output by sections and keys."))
12
13 def _get_value(self, section, key):
14 return getattr(getattr(config, section), key)
15
16+ def _sections(self, to_sort):
17+ sections = config.schema._section_schemas
18+ if to_sort:
19+ sections = sorted(sections)
20+ return sections
21+
22 def _print_full_syntax(self, section, key, value, output):
23 print('[{}] {}: {}'.format(section, key, value), file=output)
24
25@@ -78,8 +88,10 @@
26 def _show_section_error(self, section):
27 self.parser.error('No such section: {}'.format(section))
28
29- def _print_values_for_section(self, section, output):
30+ def _print_values_for_section(self, section, output, to_sort):
31 current_section = getattr(config, section)
32+ if to_sort:
33+ current_section = sorted(current_section)
34 for key in current_section:
35 self._print_full_syntax(section, key,
36 self._get_value(section, key), output)
37@@ -94,6 +106,7 @@
38 # Process the command, ignoring the closing of the output file.
39 section = args.section
40 key = args.key
41+ to_sort = args.sort
42 # Case 1: Both section and key are given, so we can directly look up
43 # the value.
44 if section is not None and key is not None:
45@@ -106,12 +119,12 @@
46 # Case 2: Section is given, key is not given.
47 elif section is not None and key is None:
48 if self._section_exists(section):
49- self._print_values_for_section(section, output)
50+ self._print_values_for_section(section, output, to_sort)
51 else:
52 self._show_section_error(section)
53 # Case 3: Section is not given, key is given.
54 elif section is None and key is not None:
55- for current_section in config.schema._section_schemas:
56+ for current_section in self._sections(to_sort):
57 # We have to ensure that the current section actually exists
58 # and that it contains the given key.
59 if (self._section_exists(current_section) and
60@@ -124,12 +137,12 @@
61 # Case 4: Neither section nor key are given, just display all the
62 # sections and their corresponding key/value pairs.
63 elif section is None and key is None:
64- for current_section in config.schema._section_schemas:
65+ for current_section in self._sections(to_sort):
66 # However, we have to make sure that the current sections and
67 # key which are being looked up actually exist before trying
68 # to print them.
69 if self._section_exists(current_section):
70- self._print_values_for_section(current_section, output)
71+ self._print_values_for_section(current_section, output, to_sort)
72
73 def process(self, args):
74 """See `ICLISubCommand`."""
75
76=== modified file 'src/mailman/commands/docs/conf.rst'
77--- src/mailman/commands/docs/conf.rst 2013-03-20 00:34:45 +0000
78+++ src/mailman/commands/docs/conf.rst 2013-04-06 07:18:25 +0000
79@@ -14,6 +14,7 @@
80 ... key = None
81 ... section = None
82 ... output = None
83+ ... sort = False
84 >>> from mailman.commands.cli_conf import Conf
85 >>> command = Conf()
86
87@@ -64,5 +65,12 @@
88 >>> command.process(FakeArgs)
89 noreply@example.com
90
91+You can also sort the output. The output will be first sorted by section, then by key.
92+
93+ >>> FakeArgs.section = 'shell'
94+ >>> FakeArgs.sort = True
95+ [shell] banner: Welcome to the GNU Mailman shell
96+ [shell] prompt: >>>
97+ [shell] use_ipython: no
98
99 .. _`Postfix command postconf(1)`: http://www.postfix.org/postconf.1.html
100
101=== modified file 'src/mailman/commands/tests/test_conf.py'
102--- src/mailman/commands/tests/test_conf.py 2013-03-20 00:34:45 +0000
103+++ src/mailman/commands/tests/test_conf.py 2013-04-06 07:18:25 +0000
104@@ -40,6 +40,7 @@
105 section = None
106 key = None
107 output = None
108+ sort = False
109
110
111 class FakeParser: