X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2FOptionString.py;h=7163d057ec6df4ef57cfcb85f3ff6a671168eb06;hb=e7a8aec57027b1791178bccacd58facacc322f6a;hp=d6cb40f714af329645250f3915388df849ad8625;hpb=c481185ca0662f4e7800af2ae6a8f3f81c753470;p=csit.git diff --git a/resources/libraries/python/OptionString.py b/resources/libraries/python/OptionString.py index d6cb40f714..7163d057ec 100644 --- a/resources/libraries/python/OptionString.py +++ b/resources/libraries/python/OptionString.py @@ -36,19 +36,21 @@ class OptionString(object): the best fitting one, without much logic near the call site. """ - def __init__(self, prefix="", *args): + def __init__(self, parts=tuple(), prefix=""): """Create instance with listed strings as parts to use. Prefix will be converted to string and stripped. The typical (nonempty) prefix values are "-" and "--". + TODO: Support users calling with parts being a string? + + :param parts: List of of stringifiable objects to become parts. :param prefix: Subtring to prepend to every parameter (not value). - :param args: List of positional arguments to become parts. + :type parts: Iterable of object :type prefix: object - :type args: list of object """ + self.parts = [str(part) for part in parts] self.prefix = str(prefix).strip() # Not worth to call change_prefix. - self.parts = list(args) def __repr__(self): """Return string executable as Python constructor call. @@ -56,12 +58,11 @@ class OptionString(object): :returns: Executable constructor call as string. :rtype: str """ - return "".join([ - "OptionString(prefix=", repr(self.prefix), ",", - repr(self.parts)[1:-1], ")"]) + return "OptionString(parts={parts!r},prefix={prefix!r})".format( + parts=self.parts, prefix=self.prefix) # TODO: Would we ever need a copy() method? - # Currently, supersting "master" is mutable but unique, + # Currently, superstring "master" is mutable but unique, # substring "slave" can be used to extend, but does not need to be mutated. def change_prefix(self, prefix): @@ -93,8 +94,9 @@ class OptionString(object): return self def _check_and_add(self, part, prefixed): - """Convert to string, strip, add conditionally prefixed if non-empty. + """Convert to string, strip, conditionally add prefixed if non-empty. + Value of None is converted to empty string. Emptiness is tested before adding prefix. :param part: Unchecked part to add to list of parts. @@ -104,7 +106,7 @@ class OptionString(object): :returns: The converted part without prefix, empty means not added. :rtype: str """ - part = str(part).strip() + part = "" if part is None else str(part).strip() if part: prefixed_part = self.prefix + part if prefixed else part self.parts.append(prefixed_part)