diff options
author | Bryn M. Reeves <bmr@redhat.com> | 2018-05-25 21:48:21 +0100 |
---|---|---|
committer | Bryn M. Reeves <bmr@redhat.com> | 2018-06-20 18:01:21 +0100 |
commit | 05ca7f74b4593fe16493ee80356a6a077228e4e2 (patch) | |
tree | bd1b1cc6e4630c932008c984f9d431311b19ae97 | |
parent | 376e59b98dd192f481ab57429d5fa3c3c0c7d092 (diff) | |
download | sos-05ca7f74b4593fe16493ee80356a6a077228e4e2.tar.gz |
[sosreport] add SoSOptions.merge()
Add a method to merge a pair of SoSOptions, replacing unset or
default values with those from the source options (and optionally
overwriting already set values).
This will be used to allow the policy framework to define default
command line behaviour for specific products.
Signed-off-by: Bryn M. Reeves <bmr@redhat.com>
-rw-r--r-- | sos/sosreport.py | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/sos/sosreport.py b/sos/sosreport.py index 0b8b0cd1..b89c036a 100644 --- a/sos/sosreport.py +++ b/sos/sosreport.py @@ -348,6 +348,14 @@ class SoSOptions(object): experimental = False threads = 4 + def _copy_opt(self, opt, src): + if hasattr(src, opt): + setattr(self, opt, getattr(src, opt)) + + def _copy_opts(self, src): + for arg in _arg_names: + self._copy_opt(arg, src) + @classmethod def from_args(cls, args): """Initialise a new SoSOptions object from a ``Namespace`` @@ -358,11 +366,30 @@ class SoSOptions(object): :returntype: SoSOptions """ opts = SoSOptions() - for arg in _arg_names: - if hasattr(args, arg): - setattr(opts, arg, getattr(args, arg)) + opts._copy_opts(args) return opts + def merge(self, src, replace=False): + """Merge another set of ``SoSOptions`` into this object. + + Merge two ``SoSOptions`` objects by setting unset or default + values to their value in the ``src`` object. + + :param src: the ``SoSOptions`` object to copy from + :param replace: ``True`` if non-default values should be + overwritten. + """ + + for arg in _arg_names: + if not hasattr(src, arg): + continue + if arg in _arg_defaults.keys(): + if replace or getattr(self, arg) == _arg_defaults[arg]: + self._copy_opt(arg, src) + else: + if replace or not getattr(self, arg): + self._copy_opt(arg, src) + class SoSReport(object): |