Python3: resources and libraries
[csit.git] / resources / libraries / python / OptionString.py
index 7c8b2d0..9a30d37 100644 (file)
 """Utility function for handling options without doubled or trailing spaces."""
 
 
-class OptionString(object):
+class OptionString:
     """Class serving as a builder for option strings.
 
-    Motivation: Both manual contatenation and .join() methods
+    Motivation: Both manual concatenation and .join() methods
     are prone to leaving superfluous spaces if some parts of options
     are optional (missing, empty).
 
@@ -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=u""):
         """Create instance with listed strings as parts to use.
 
         Prefix will be converted to string and stripped.
         The typical (nonempty) prefix values are "-" and "--".
 
-        :param prefix: Subtring to prepend to every parameter (not value).
-        :param args: List of positional arguments to become parts.
+        TODO: Support users calling with parts being a string?
+
+        :param parts: List of stringifiable objects to become parts.
+        :param prefix: Substring to prepend to every parameter (not value).
+        :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,10 @@ class OptionString(object):
         :returns: Executable constructor call as string.
         :rtype: str
         """
-        return "".join([
-            "OptionString(prefix=", repr(self.prefix), ",",
-            repr(self.parts)[1:-1], ")"])
+        return f"OptionString(parts={self.parts!r},prefix={self.prefix!r})"
 
     # 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):
@@ -105,7 +105,7 @@ class OptionString(object):
         :returns: The converted part without prefix, empty means not added.
         :rtype: str
         """
-        part = "" if part is None else str(part).strip()
+        part = u"" if part is None else str(part).strip()
         if part:
             prefixed_part = self.prefix + part if prefixed else part
             self.parts.append(prefixed_part)
@@ -119,7 +119,7 @@ class OptionString(object):
         Parameter is prefixed before adding.
 
         :param parameter: Parameter object, usually a word starting with dash.
-        :type variable: object
+        :type parameter: object
         :returns: Self, to enable method chaining.
         :rtype: OptionString
         """
@@ -136,7 +136,7 @@ class OptionString(object):
 
         :param parameter: Parameter object, usually a word starting with dash.
         :param condition: Do not add if truth value of this is false.
-        :type variable: object
+        :type parameter: object
         :type condition: object
         :returns: Self, to enable method chaining.
         :rtype: OptionString
@@ -154,7 +154,7 @@ class OptionString(object):
 
         :param parameter: Parameter object, usually a word starting with dash.
         :param value: Value object. Prefix is never added.
-        :type variable: object
+        :type parameter: object
         :type value: object
         :returns: Self, to enable method chaining.
         :rtype: OptionString
@@ -177,7 +177,7 @@ class OptionString(object):
 
         :param parameter: Parameter object, usually a word starting with dash.
         :param value: Value object. Prefix is never added.
-        :type variable: object
+        :type parameter: object
         :type value: object
         :returns: Self, to enable method chaining.
         :rtype: OptionString
@@ -186,7 +186,7 @@ class OptionString(object):
         # pylint: disable=protected-access
         if temp._check_and_add(parameter, prefixed=True):
             if temp._check_and_add(value, prefixed=False):
-                self.parts.append("=".join(temp.parts))
+                self.parts.append(u"=".join(temp.parts))
         return self
 
     def add_with_value_if(self, parameter, value, condition):
@@ -200,7 +200,7 @@ class OptionString(object):
         :param parameter: Parameter object, usually a word starting with dash.
         :param value: Value object. Prefix is never added.
         :param condition: Do not add if truth value of this is false.
-        :type variable: object
+        :type parameter: object
         :type value: object
         :type condition: object
         :returns: Self, to enable method chaining.
@@ -221,7 +221,7 @@ class OptionString(object):
         :param parameter: Parameter object, usually a word starting with dash.
         :param value: Value object. Prefix is never added.
         :param condition: Do not add if truth value of this is false.
-        :type variable: object
+        :type parameter: object
         :type value: object
         :type condition: object
         :returns: Self, to enable method chaining.
@@ -231,7 +231,7 @@ class OptionString(object):
             self.add_equals(parameter, value)
         return self
 
-    def add_with_value_from_dict(self, parameter, key, mapping, default=""):
+    def add_with_value_from_dict(self, parameter, key, mapping, default=u""):
         """Add parameter with value from dict under key, or default.
 
         If key is missing, default is used as value.
@@ -253,7 +253,7 @@ class OptionString(object):
         value = mapping.get(key, default)
         return self.add_with_value(parameter, value)
 
-    def add_equals_from_dict(self, parameter, key, mapping, default=""):
+    def add_equals_from_dict(self, parameter, key, mapping, default=u""):
         """Add parameter=value to options where value is from dict.
 
         If key is missing, default is used as value.
@@ -275,7 +275,7 @@ class OptionString(object):
         value = mapping.get(key, default)
         return self.add_equals(parameter, value)
 
-    def add_if_from_dict(self, parameter, key, mapping, default="False"):
+    def add_if_from_dict(self, parameter, key, mapping, default=u"False"):
         """Add parameter based on if the condition in dict is true.
 
         If key is missing, default is used as condition.
@@ -299,7 +299,7 @@ class OptionString(object):
         return self.add_if(parameter, condition)
 
     def add_with_value_if_from_dict(
-            self, parameter, value, key, mapping, default="False"):
+            self, parameter, value, key, mapping, default=u"False"):
         """Add parameter and value based on condition in dict.
 
         If key is missing, default is used as condition.
@@ -325,7 +325,7 @@ class OptionString(object):
         return self.add_with_value_if(parameter, value, condition)
 
     def add_equals_if_from_dict(
-            self, parameter, value, key, mapping, default="False"):
+            self, parameter, value, key, mapping, default=u"False"):
         """Add parameter=value based on condition in dict.
 
         If key is missing, default is used as condition.
@@ -360,4 +360,4 @@ class OptionString(object):
         :returns: Space separated string of options.
         :rtype: str
         """
-        return " ".join(self.parts)
+        return u" ".join(self.parts)