Anonymous avatar Anonymous committed 0ec8888 Draft

saner output from config file generation (one subcommand block at a time)

Comments (0)

Files changed (3)

argparse_config.src/README.rst

 rules, from a given set of commandline arguments. That looks like this::
 
     >>> parsed_args = arg_parser.parse_args(['--repository', 'https://bitbucket.org/tikitu/argparse_config', 'merge'])
-    >>> print argparse_config.generate_config(arg_parser, parsed_args, only_non_defaults=True)
+    >>> print argparse_config.generate_config(arg_parser, parsed_args, section='default', only_non_defaults=True)
     [default]
     repository: https://bitbucket.org/tikitu/argparse_config
 
-    [merge]
-
-    [commit]
-
 Some complications make this less useful than it could be, sadly:
 
-* If you use subcommands, you can only parse the args for one of them at a time.
+* If you use subcommands, you can only parse the args for one of them at a time
+  (use the `section` argument to specify which, or leave it off to get the `[default]`
+  section).
 * We can't tell the difference between default values written in code (which should
   not be added to the config file) and written in a previously-read-in config file
   (which should). This is why `only_non_defaults` exists.

argparse_config.src/argparse_config/__init__.py

 import ConfigParser
 from argparse import _SubParsersAction, _StoreAction, _StoreConstAction
 
-__version__ = '0.4'
+__version__ = '0.5'
 
 def read_config_file(arg_parser, filename):
     config_parser = ConfigParser.SafeConfigParser(allow_no_value=True)
 def read_config_parser(arg_parser, config_parser):
     ReadConfig(config_parser=config_parser).walk_parser(arg_parser)
 
-def generate_config(arg_parser, parsed_args, only_non_defaults=False):
-    action = GenerateConfig(parsed_args, only_non_defaults=only_non_defaults)
+def generate_config(arg_parser, parsed_args, section='default', only_non_defaults=False):
+    action = GenerateConfig(parsed_args, section, only_non_defaults=only_non_defaults)
     action.walk_parser(arg_parser)
     return action.contents
 
 
 class GenerateConfig(ArgParserWalker):
 
-    def __init__(self, parsed_args, only_non_defaults=False):
+    def __init__(self, parsed_args, section, only_non_defaults=False):
         self.parsed_args = parsed_args
         self._contents = []
         self._only_non_defaults = only_non_defaults
+        self._section = section
+        self._in_section = False
 
     def start_section(self, section_name):
-        if self._contents:
-            self._contents.append(u'')
-        self._contents.append(u'[{0}]'.format(section_name))
+        if section_name == self._section:
+            self._in_section = True
+            if self._contents:
+                self._contents.append(u'')
+            self._contents.append(u'[{0}]'.format(section_name))
 
     def end_section(self):
-        pass
+        self._in_section = False
 
     @property
     def contents(self):
         return u'\n'.join(self._contents + [u''])
 
     def process_parser_action(self, action, is_store_const=False):
+        if not self._in_section:
+            return
         # take the longest string, since that is likely the most informative
         action_name = list(action.option_strings)
         action_name.sort(key=lambda s: len(s), reverse=True)

argparse_config.src/argparse_config/tests/__init__.py

         subcommand_parser.add_argument('--object', default='nail')
 
         parsed_args = arg_parser.parse_args(['subcommand'])
-        new_config_file = generate_config(arg_parser, parsed_args)
+        new_config_file = generate_config(arg_parser, parsed_args, section='subcommand')
         self.assertEquals(new_config_file, textwrap.dedent("""
-        [default]
-
         [subcommand]
         tool: hammer
         object: nail
 
         read_config_parser(arg_parser, config_parser)
         parsed_args = arg_parser.parse_args(['subcommand', '--object', 'thumb'])
+        new_config_file = generate_config(arg_parser, parsed_args, section='subcommand')
+        self.assertEquals(new_config_file, textwrap.dedent("""
+        [subcommand]
+        tool: hammer
+        object: thumb
+        """.lstrip('\n')))
         new_config_file = generate_config(arg_parser, parsed_args)
         self.assertEquals(new_config_file, textwrap.dedent("""
         [default]
         int_value: 3
         flag:
+        """.lstrip('\n')))
 
-        [subcommand]
-        tool: hammer
-        object: thumb
-        """.lstrip('\n')))
 
     @with_config_file()
     def test_everything(self, config_parser):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.