From 8c6d69af0b552fab3c371947c694a7f1187054f7 Mon Sep 17 00:00:00 2001 From: "Bryn M. Reeves" Date: Sat, 26 May 2018 16:48:34 +0100 Subject: [sos] add __str__ and __repr__ to SoSOptions Signed-off-by: Bryn M. Reeves --- sos/__init__.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/sos/__init__.py b/sos/__init__.py index 6b165324..9ec9166a 100644 --- a/sos/__init__.py +++ b/sos/__init__.py @@ -94,6 +94,37 @@ class SoSOptions(object): for arg in _arg_names: self._copy_opt(arg, src) + def __str(self, quote=False, sep=" ", prefix="", suffix=""): + """Format a SoSOptions object as a human or machine readable string. + + :param quote: quote option values + :param sep: list separator string + :param prefix: arbitrary prefix string + :param suffix: arbitrary suffix string + :param literal: print values as Python literals + """ + fmt = prefix + arg_fmt = "=%s" if not quote else "='%s'" + for arg in _arg_names: + fmt += arg + arg_fmt + sep + fmt.strip(sep) + fmt += suffix + return fmt % (self.all_logs, self.batch, self.build, self.case_id, + self.chroot, self.compression_type, self.config_file, + self.debug, self.enableplugins, self.experimental, + self.label, self.list_plugins, self.list_profiles, + self.log_size, self.noplugins, self.noreport, + self.onlyplugins, self.plugopts, self.product, + self.profiles, self.quiet, self.sysroot, self.tmp_dir, + self.usealloptions, self.verbosity, self.verify) + + def __str__(self): + return self.__str() + + def __repr__(self): + return self.__str(quote=True, sep=", ", prefix="SoSOptions(", + suffix=")") + def __init__(self, **kwargs): """Initialise a new ``SoSOptions`` object from keyword arguments. @@ -135,7 +166,6 @@ class SoSOptions(object): :param replace: ``True`` if non-default values should be overwritten. """ - for arg in _arg_names: if not hasattr(src, arg): continue -- cgit