Esempio n. 1
0
 def __init__(self, name, underlyingType, attrib):
     self.units = str(attrib.get("units", ""))
     self.min = parsing.parseValue(attrib.get("min", None), underlyingType,
                                   True, False)
     self.max = parsing.parseValue(attrib.get("max", None), underlyingType,
                                   True, False)
     Setting.__init__(self, name, underlyingType, attrib)
Esempio n. 2
0
    def setValue(self, v):
        """Protection against setting the value to an invalid/unexpected type

        """
        try:
            tentative_value = parsing.parseValue(v, list, True)
        except ValueError:
            raise ValueError(
                "Cannot set {0} value to {1} as it is not a valid value for {2} "
                "and cannot be converted to one".format(
                    self.name, v, self.__class__.__name__))

        if self.containedType and tentative_value:
            ct = self.containedType
            try:
                if ct == str:
                    tentative_value = [str(i) for i in tentative_value]
                else:
                    tentative_value = [
                        parsing.parseValue(i, ct, False)
                        for i in tentative_value
                    ]
            except ValueError:
                raise ValueError(
                    "Cannot set {0} value to {1} as it contains items not of the correct type {2}"
                    .format(self.name, tentative_value, self.containedType))

        if (self.options and self.enforcedOptions and any(
            [value not in self.options for value in tentative_value])):
            raise ValueError(
                "Cannot set {0} value to {1} as it isn't in the allowed options {2}"
                .format(self.name, tentative_value, self.options))

        self._value = tuple(tentative_value or [])
Esempio n. 3
0
 def __init__(self, name, attrib):
     self.options = [
         item for item in parsing.parseValue(attrib.get("options", None),
                                             list, True)
     ]
     self.enforcedOptions = parsing.parseValue(
         attrib.get("enforcedOptions", None), bool, True)
     if self.enforcedOptions and not self.options:
         raise AttributeError(
             "Cannot use enforcedOptions in ({}) {} without supplying options."
             .format(self.__class__.__name__, self.name))
     Setting.__init__(self, name, str, attrib)
Esempio n. 4
0
    def setValue(self, v):
        """Protection against setting the value to an invalid/unexpected type

        """
        try:
            tenative_value = parsing.parseValue(v, bool, True)
        except ValueError:
            raise ValueError(
                "Cannot set {0} value to {1} as it is not a valid value for {2} "
                "and cannot be converted to one".format(
                    self.name, v, self.__class__.__name__))

        self._value = tenative_value
Esempio n. 5
0
    def __init__(self, name, attrib):
        self.containedType = parsing.parseType(
            attrib.get("containedType", None), True)
        self.options = [
            item for item in parsing.parseValue(attrib.get("options", None),
                                                list, True)
        ]
        self.enforcedOptions = parsing.parseValue(
            attrib.get("enforcedOptions", None), bool, True)

        if self.enforcedOptions and not self.options:
            raise AttributeError(
                "Cannot use enforcedOptions in ({}) {} without supplying options."
                .format(self.__class__.__name__, self.name))

        if self.containedType and self.containedType == type(None):
            raise RuntimeError(
                "Do not use NoneType for containedType in ListSetting. "
                "That does seem helpful and it will cause pickling issues.")
        Setting.__init__(self, name, list, attrib)
        self._default = tuple(
            self.default or []
        )  # convert mutable list to tuple so no one changes it after def.
Esempio n. 6
0
    def setValue(self, v):
        """Protection against setting the value to an invalid/unexpected type

        """
        try:
            tenative_value = parsing.parseValue(v, self.underlyingType, True)
        except ValueError:
            raise ValueError(
                "Cannot set {0} value to {1} as it is not a valid value for {2} "
                "and cannot be converted to one".format(
                    self.name, v, self.__class__.__name__))

        if self.min and tenative_value < self.min:
            raise ValueError(
                "Cannot set {0} value to {1} as it does not exceed the set minimum of {2}"
                .format(self.name, tenative_value, self.min))
        elif self.max and tenative_value > self.max:
            raise ValueError(
                "Cannot set {0} value to {1} as it exceeds the set maximum of {2}"
                .format(self.name, tenative_value, self.max))

        self._value = tenative_value
Esempio n. 7
0
    def test_parseValue(self):
        self.assertEqual(parsing.parseValue("5", int), 5)
        self.assertEqual(parsing.parseValue(5, int), 5)
        self.assertEqual(parsing.parseValue("5", float), 5.0)
        self.assertEqual(parsing.parseValue("True", bool), True)
        self.assertEqual(
            parsing.parseValue("['apple','banana','mango']", list),
            ["apple", "banana", "mango"],
        )
        self.assertEqual(
            parsing.parseValue({"apple": 1, "banana": 2, "mango": 3}, dict),
            {"apple": 1, "banana": 2, "mango": 3},
        )
        self.assertEqual(
            parsing.parseValue("{'apple':1,'banana':2,'mango':3}", dict),
            {"apple": 1, "banana": 2, "mango": 3},
        )
        self.assertEqual(parsing.parseValue("(1,2)", tuple), (1, 2))

        self.assertEqual(parsing.parseValue("None", int, True), 0)
        self.assertEqual(parsing.parseValue(None, int, True), 0)
        self.assertEqual(parsing.parseValue("None", bool, True), False)
        self.assertEqual(parsing.parseValue(None, bool, True), False)

        self.assertEqual(parsing.parseValue(None, bool, True, False), None)

        with self.assertRaises(TypeError):
            parsing.parseValue("5", str)
        with self.assertRaises(ValueError):
            parsing.parseValue("5", bool)
Esempio n. 8
0
 def __init__(self, name, attrib):
     self.relativeTo = attrib.get("relativeTo", None)
     self.mustExist = parsing.parseValue(attrib.get("mustExist", None),
                                         bool, True)
     StrSetting.__init__(self, name, attrib)