diff mbox series

[4/7] patman: Update defaults in subparsers

Message ID 20201103205416.356060-5-sjg@chromium.org
State Accepted
Commit 3145b63513552fc77f6584a89baf0966617eeadf
Delegated to: Simon Glass
Headers show
Series patman: Minor improvements to 'patman status' | expand

Commit Message

Simon Glass Nov. 3, 2020, 8:54 p.m. UTC
At present values from the settings file are only applied to the main
parser. With the new parser structure this means that some settings are
ignored.

Update the implementation to set defaults across the main parser and all
subparsers. Also fix up the comments, since ArgumentParser is being used
now.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 tools/patman/settings.py | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)

Comments

Simon Glass Nov. 15, 2020, 2:07 p.m. UTC | #1
At present values from the settings file are only applied to the main
parser. With the new parser structure this means that some settings are
ignored.

Update the implementation to set defaults across the main parser and all
subparsers. Also fix up the comments, since ArgumentParser is being used
now.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 tools/patman/settings.py | 28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)

Applied to u-boot-dm, thanks!
diff mbox series

Patch

diff --git a/tools/patman/settings.py b/tools/patman/settings.py
index 732bd401067..8c10eab2645 100644
--- a/tools/patman/settings.py
+++ b/tools/patman/settings.py
@@ -7,6 +7,7 @@  try:
 except:
     import ConfigParser
 
+import argparse
 import os
 import re
 
@@ -216,10 +217,10 @@  nxp = Zhikang Zhang <zhikang.zhang@nxp.com>
 ''' % (name, email), file=f)
     f.close();
 
-def _UpdateDefaults(parser, config):
+def _UpdateDefaults(main_parser, config):
     """Update the given OptionParser defaults based on config.
 
-    We'll walk through all of the settings from the parser
+    We'll walk through all of the settings from all parsers.
     For each setting we'll look for a default in the option parser.
     If it's found we'll update the option parser default.
 
@@ -228,13 +229,24 @@  def _UpdateDefaults(parser, config):
     say.
 
     Args:
-        parser: An instance of an OptionParser whose defaults will be
+        parser: An instance of an ArgumentParser whose defaults will be
             updated.
         config: An instance of _ProjectConfigParser that we will query
             for settings.
     """
-    defaults = parser.parse_known_args()[0]
-    defaults = vars(defaults)
+    # Find all the parsers and subparsers
+    parsers = [main_parser]
+    parsers += [subparser for action in main_parser._actions
+                  if isinstance(action, argparse._SubParsersAction)
+                  for _, subparser in action.choices.items()]
+
+    # Collect the defaults from each parser
+    defaults = {}
+    for parser in parsers:
+        pdefs = parser.parse_known_args()[0]
+        defaults.update(vars(pdefs))
+
+    # Go through the settings and collect defaults
     for name, val in config.items('settings'):
         if name in defaults:
             default_val = defaults[name]
@@ -242,10 +254,14 @@  def _UpdateDefaults(parser, config):
                 val = config.getboolean('settings', name)
             elif isinstance(default_val, int):
                 val = config.getint('settings', name)
+            elif isinstance(default_val, str):
+                val = config.get('settings', name)
             defaults[name] = val
         else:
             print("WARNING: Unknown setting %s" % name)
-        parser.set_defaults(**defaults)
+
+    # Set all the defaults (this propagates through all subparsers)
+    main_parser.set_defaults(**defaults)
 
 def _ReadAliasFile(fname):
     """Read in the U-Boot git alias file if it exists.