diff options
author | Bryn M. Reeves <bmr@redhat.com> | 2018-05-27 19:58:59 +0100 |
---|---|---|
committer | Bryn M. Reeves <bmr@redhat.com> | 2018-06-20 18:01:21 +0100 |
commit | 622f19f1a9d6efb633f665fc190ebf22da029582 (patch) | |
tree | 9a2df23c2a90f674e0bc69a760d6ee3468819d78 | |
parent | 421d45d601fa6d3dcf1ca9bba0c2fdf10dd2a4e6 (diff) | |
download | sos-622f19f1a9d6efb633f665fc190ebf22da029582.tar.gz |
[sos] concatenate list-typed args in SoSOptions.merge()
When merging options (e.g. to combine command line and preset
arguments), concatenate list-typed argument values rather than
overwriting the target list with the source value.
This allows users to extend preset-defined arguments for options
like --only-plugins and --profiles on the command line.
Signed-off-by: Bryn M. Reeves <bmr@redhat.com>
-rw-r--r-- | sos/__init__.py | 37 |
1 files changed, 23 insertions, 14 deletions
diff --git a/sos/__init__.py b/sos/__init__.py index 20fdf448..3356e034 100644 --- a/sos/__init__.py +++ b/sos/__init__.py @@ -59,6 +59,13 @@ _arg_defaults = { } +def _is_seq(val): + """Return true if val is an instance of a known sequence type. + """ + val_type = type(val) + return val_type is list or val_type is tuple + + class SoSOptions(object): add_preset = "" alloptions = False @@ -93,13 +100,19 @@ class SoSOptions(object): verbosity = 0 verify = False - def _copy_opt(self, opt, src): + def _merge_opt(self, opt, src, replace): if hasattr(src, opt): - setattr(self, opt, getattr(src, opt)) + value = getattr(src, opt) + if replace or not _is_seq(value): + # Overwrite atomic values + setattr(self, opt, getattr(src, opt)) + else: + # Concatenate sequence types + setattr(self, opt, getattr(self, opt) + getattr(src, opt)) - def _copy_opts(self, src): + def _merge_opts(self, src, replace): for arg in _arg_names: - self._copy_opt(arg, src) + self._merge_opt(arg, src, replace) def __str(self, quote=False, sep=" ", prefix="", suffix=""): """Format a SoSOptions object as a human or machine readable string. @@ -117,16 +130,10 @@ class SoSOptions(object): fmt.strip(sep) fmt += suffix - def is_seq(val): - """Return true if val is an instance of a known sequence type. - """ - val_type = type(val) - return val_type is list or val_type is tuple - if not quote: # Convert Python source notation for sequences into plain strings args = [getattr(self, arg) for arg in _arg_names] - args = [",".join(a) if is_seq(a) else a for a in args] + args = [",".join(a) if _is_seq(a) else a for a in args] return fmt % tuple(args) @@ -165,7 +172,7 @@ class SoSOptions(object): :returntype: SoSOptions """ opts = SoSOptions() - opts._copy_opts(args) + opts._merge_opts(args, True) return opts def merge(self, src, replace=False): @@ -183,10 +190,12 @@ class SoSOptions(object): continue if arg in _arg_defaults.keys(): if replace or getattr(self, arg) == _arg_defaults[arg]: - self._copy_opt(arg, src) + self._merge_opt(arg, src, replace) + elif _is_seq(getattr(self, arg)): + self._merge_opt(arg, src, replace) else: if replace or not getattr(self, arg): - self._copy_opt(arg, src) + self._merge_opt(arg, src, replace) def dict(self): """Return this ``SoSOptions`` option values as a dictionary of |