Commits

Anonymous committed 0ee139b Draft

Refactored for tidiness.

Comments (0)

Files changed (1)

argparse_config.src/argparse_config/__init__.py

     set_defaults(arg_parser, config_parser)
 
 def set_defaults(arg_parser, config_parser):
-    _walk_parser(arg_parser, SetDefaults(config_parser=config_parser))
+    SetDefaults(config_parser=config_parser).walk_parser(arg_parser)
 
 def write_defaults(arg_parser, parsed_args, only_non_defaults=False):
     action = WriteDefaults(parsed_args, only_non_defaults=only_non_defaults)
-    _walk_parser(arg_parser, action)
+    action.walk_parser(arg_parser)
     return action.contents
 
-def _walk_parser(arg_parser, actions_obj):
-    try:
-        actions_obj.start_section('default')
-        for action in arg_parser._actions:
-            if isinstance(action, _StoreAction):
-                actions_obj.process_parser_action(action)
-            elif isinstance(action, _StoreConstAction):
-                actions_obj.process_parser_action(action, is_store_const=True)
-            elif isinstance(action, _SubParsersAction):
-                for command, sub_parser in action.choices.items():
-                    actions_obj.start_section(command)
-                    for sub_action in sub_parser._actions:
-                        actions_obj.process_parser_action(sub_action)
-                    actions_obj.end_section()
-        actions_obj.end_section()
-    except DefaultError as e:
-        arg_parser.error(u'[{section_name}] config option "{option_string}" must be {type_transformer}() value, got: {value}'.format(
-            section_name=e.section_name,
-            option_string = e.option_string,
-            type_transformer=e.type_transformer.__name__,
-            value=e.value
-        ))
+class ArgParserWalker(object):
 
-class WriteDefaults(object):
+    def start_section(self, section_name):
+        raise NotImplementedError()
+    def end_section(self):
+        raise NotImplementedError()
+    def process_parser_action(self, action, is_store_const=False):
+        raise NotImplementedError()
+
+    def walk_parser(self, arg_parser):
+        try:
+            self.start_section('default')
+            for action in arg_parser._actions:
+                if isinstance(action, _StoreAction):
+                    self.process_parser_action(action)
+                elif isinstance(action, _StoreConstAction):
+                    self.process_parser_action(action, is_store_const=True)
+                elif isinstance(action, _SubParsersAction):
+                    for command, sub_parser in action.choices.items():
+                        self.start_section(command)
+                        for sub_action in sub_parser._actions:
+                            self.process_parser_action(sub_action)
+                        self.end_section()
+            self.end_section()
+        except DefaultError as e:
+            arg_parser.error(u'[{section_name}] config option "{option_string}" must be {type_transformer}() value, got: {value}'.format(
+                section_name=e.section_name,
+                option_string = e.option_string,
+                type_transformer=e.type_transformer.__name__,
+                value=e.value
+            ))
+
+class WriteDefaults(ArgParserWalker):
 
     def __init__(self, parsed_args, only_non_defaults=False):
         self.parsed_args = parsed_args
                 ))
 
 
-class SetDefaults(object):
+class SetDefaults(ArgParserWalker):
 
     def __init__(self, config_parser=None):
         self.sections = []