def distribute(self, value):
        """Method called by the decorator :meth:`Terminus.assign` that
        tries to assign the value passed to the constructor of the
        :class:`Terminus` derived class.

        Parameters
        ----------
        value : :class:`TypeVar`
            The value passed to the constructor of the :class:`Terminus`
            derived class.

        Raises
        ------
        :class:`~.TypeAttributeNotImplementedError`
            If the type attribute has not been defined in the derived class
            constructor.

        :class:`~.TypeAttributeTypeError`
            If the :attr:`type` is not of type :obj:`type`

        :class:`~.SettingTypeError`
            If the type of the passed value is not :attr:`type`.

        """
        if not hasattr(self, "type"):
            raise js.TypeAttributeNotImplementedError(self.__class__)
        if not isinstance(self.type, type):
            raise js.TypeAttributeTypeError(self.__class__)
        if not isinstance(value, self.type):
            raise js.SettingTypeError(self.type, type(value))
        self.value = value
Exemplo n.º 2
0
    def distribute(self, value):
        """Method called by the decorator :meth:`Terminus.assign` that
        tries to assign the values passed to the constructor of the
        :class:`Number` derived class.

        Parameters
        ----------
        value : :obj:`Union`[:obj:`float`, :obj:`int`, :obj:`dict`]

        Raises
        ------
        :class:`~.TypeAttributeNotImplementedError`
            If the type attribute has not been defined in the derived class
            constructor.

        :class:`~.TypeAttributeTypeError`
            If the :attr:`type` is not of type :obj:`type`

        :class:`~.SettingTypeError`
            If `values` is not a number or valid range dictionary.

        """
        if not hasattr(self, "type"):
            raise js.TypeAttributeNotImplementedError(self.__class__)
        if not isinstance(self.type, type):
            raise js.TypeAttributeTypeError(self.__class__)

        if type(value) is self.type:
            self.__value(value)
        elif type(value) is dict and "array" in value:
            self.__array(value)
        elif type(value) is dict:
            self.__range(value)
        else:
            raise js.SettingTypeError(
                f"{self.type} || {{'array': [{self.type}]}} || "
                f"{{'min': {self.type}, 'max': {self.type}, 'num': {int}}}",
                type(value))
Exemplo n.º 3
0
    def __array(self, value: dict):
        """The method that assigns the attributes if a array of value is passed.

        Parameters
        ----------
        value : :obj:`dict`[:obj:`str`, :obj:`list`]
            The dictionary with "array": listofvalues.

        Raises
        ------
        :class:`~.SettingTypeError`
            If the values in the array are not the same type as :attr:`type`

        """
        for item in value["array"]:
            if not isinstance(item, self.type):
                raise js.SettingTypeError(self.type, type(item))
        self.value = value["array"]
        self._range = True
        try:
            self._match = value["match"]
        except KeyError:
            self._match = None
    def distribute(self, value: str):
        """Method called by the decorator :meth:`Settings.assign` that
        tries to assign the values passed to the constructor of the
        :class:`Dict` derived class.

        Parameters
        ----------
        value: :obj:`str`
            The value to be stored.

        Raises
        ------
        :class:`~.OptionsAttributeNotImplementedError`
            If the :attr:`options` attribute has not been defined in the
            derived class constructor.

        :class:`~.OptionsAttributeTypeError`
            If the item contained in the :attr:`options` attribute are not
            strings.

        :class:`~.SettingTypeError`
            If the passed value `value` is not a :obj:`str`.

        :class:`~.SettingStringSelectionError`
            If the passed value `value` is not in :attr:`options`.

        """
        if not hasattr(self, "options"):
            raise js.OptionsAttributeNotImplementedError(self.__class__)
        if not isinstance(self.options, list):
            raise js.OptionsAttributeTypeError(self.__class__)
        if not isinstance(value, str):
            raise js.SettingTypeError(str, type(value))
        elif value not in self.options:
            raise js.SettingStringSelectionError(self.options)
        else:
            self.value = value
Exemplo n.º 5
0
    def distribute(self, values: list):
        """Method called by the decorator :meth:`Settings.assign` that
        tries to assign the values passed to the constructor of the
        :class:`List` derived class.

        Parameters
        ----------
        values : :obj:`list`
            The values passed to the constructor of the :class:`List`
            derived class.

        Returns
        -------
        None

        Raises
        ------
        :class:`~.TypeAttributeNotImplementedError`
            If the type attribute has not been defined in the derived class
            constructor.

        :class:`~.TypeAttributeTypeError`
            If the :attr:`type` is not of type :obj:`type`

        :class:`~.SettingTypeError`
            If `values` is not a :obj:`list`.

        :class:`~.SettingsListTypeError`
            If any of the items in `values` are not of the required type,
            as specified in the derived class.

        :class:`~.SettingsErrorMessage`
            If any exceptions are raised when instantiating any subsettings.

        """
        if not hasattr(self, "type"):
            raise js.TypeAttributeNotImplementedError(self.__class__)
        if not isinstance(self.type, type):
            raise js.TypeAttributeTypeError(self.__class__)

        if not isinstance(values, list):
            raise js.SettingTypeError(list, type(values))

        self.value = list()

        if self.type not in self.primitive:
            for idx, item in enumerate(values):
                try:
                    self.value.append(self.type(item))
                except js.SettingErrorMessage as e:
                    raise js.SettingErrorMessage(f"[{idx}]", e)
                except js.SettingTypeError as e:
                    raise js.SettingErrorMessage(f"[{idx}]", original_error=e)
                except js.SettingRangeTypeError as e:
                    raise js.SettingErrorMessage(f"[{idx}]", original_error=e)
                except js.SettingRangeKeyError as e:
                    raise js.SettingErrorMessage(f"[{idx}]", original_error=e)
                except js.ConsistencyError as e:
                    raise js.SettingErrorMessage(f"[{idx}]", original_error=e)
        else:
            for idx, item in enumerate(values):
                try:
                    if not isinstance(item, self.type):
                        raise js.SettingTypeError(self.type, type(item))
                    self.value.append(item)
                except js.SettingStringSelectionError as e:
                    raise js.SettingErrorMessage(f"[{idx}]", original_error=e)
Exemplo n.º 6
0
    def distribute(self, values: dict):
        """The method which loops over the attribute/type pairs in the derived
        class' constructor and tries to find and assign values to them from the
        `values` parameter.

        Parameters
        ----------
        values : :obj:`dict`
            The dictionary that contains the values to be assigned to the
            derived class' attributes.

        Returns
        -------
        None

        Raises
        ------
        :class:`~.SettingNotFoundError`
            If one of the derived class' attributes cannot be found in
            `values`.

        :class:`~.js.SettingTypeError`
            If one of the type of one of the settings found in `values`
            is not of the required type, as defined in the derived class'
            constructor.

        :class:`~.SettingCheckError`
            If a check error is caught when assigning a setting to a
            :class:`~.Terminus` instance.

        Note
        ----
        If the setting found in the `values` is None, then this function will
        assign None as the setting value by default, independent of the
        expected value.

        """
        for setting, setting_type in self.__dict__.items():
            if not isinstance(values, dict):
                raise js.SettingTypeError(dict, type(values))
            try:
                try:
                    value = values[setting]
                except KeyError:
                    raise js.SettingNotFoundError()
            except js.SettingNotFoundError as e:
                raise js.SettingErrorMessage(setting, original_error=e)
            if setting_type not in self.primitive:
                try:
                    setattr(self, setting, setting_type(value))
                except js.SettingTypeError as e:
                    raise js.SettingErrorMessage(setting, original_error=e)
                except js.SettingCheckError as e:
                    raise js.SettingErrorMessage(setting, original_error=e)
                except js.SettingNotFoundError as e:
                    raise js.SettingErrorMessage(setting, original_error=e)
                except js.SettingRangeTypeError as e:
                    raise js.SettingErrorMessage(setting, original_error=e)
                except js.SettingRangeKeyError as e:
                    raise js.SettingErrorMessage(setting, original_error=e)
                except js.SettingStringSelectionError as e:
                    raise js.SettingErrorMessage(setting, original_error=e)
                except js.ConsistencyError as e:
                    raise js.SettingErrorMessage(setting, original_error=e)
                except js.SettingErrorMessage as e:
                    raise js.SettingErrorMessage(setting, branch_error=e)
            elif value is None:
                setattr(self, setting, value)
            else:
                try:
                    if isinstance(value, setting_type):
                        setattr(self, setting, value)
                    else:
                        raise js.SettingTypeError(setting_type, type(value))
                except js.SettingTypeError as e:
                    raise js.SettingErrorMessage(setting, original_error=e)
        self.__source__ = values