Beispiel #1
0
    def set(self, key: str, item: typing.Any, convert=True) -> None:
        if not convert:
            return self.__setitem__(key, item)

        if key in self.colors:
            try:
                hex_ = str(item)
                if hex_.startswith("#"):
                    hex_ = hex_[1:]
                if len(hex_) == 3:
                    hex_ = "".join(s for s in hex_ for _ in range(2))
                if len(hex_) != 6:
                    raise InvalidConfigError("Invalid color name or hex.")
                try:
                    int(hex_, 16)
                except ValueError:
                    raise InvalidConfigError("Invalid color name or hex.")

            except InvalidConfigError:
                name = str(item).lower()
                name = re.sub(r"[\-+|. ]+", " ", name)
                hex_ = ALL_COLORS.get(name)
                if hex_ is None:
                    name = re.sub(r"[\-+|. ]+", "", name)
                    hex_ = ALL_COLORS.get(name)
                    if hex_ is None:
                        raise
            return self.__setitem__(key, "#" + hex_)

        if key in self.time_deltas:
            try:
                isodate.parse_duration(item)
            except isodate.ISO8601Error:
                try:
                    converter = UserFriendlyTime()
                    time = self.bot.loop.run_until_complete(
                        converter.convert(None, item)
                    )
                    if time.arg:
                        raise ValueError
                except BadArgument as exc:
                    raise InvalidConfigError(*exc.args)
                except Exception:
                    raise InvalidConfigError(
                        "Unrecognized time, please use ISO-8601 duration format "
                        'string or a simpler "human readable" time.'
                    )
                item = isodate.duration_isoformat(time.dt - converter.now)
            return self.__setitem__(key, item)

        if key in self.booleans:
            try:
                return self.__setitem__(key, strtobool(item))
            except ValueError:
                raise InvalidConfigError("Must be a yes/no value.")

        # elif key in self.special_types:
        #     if key == "status":

        return self.__setitem__(key, item)
Beispiel #2
0
    async def clean_data(self, key: str,
                         val: typing.Any) -> typing.Tuple[str, str]:
        value_text = val
        clean_value = val

        # when setting a color
        if key in self.colors:
            hex_ = ALL_COLORS.get(val)

            if hex_ is None:
                hex_ = str(hex_)
                if hex_.startswith("#"):
                    hex_ = hex_[1:]
                if len(hex_) == 3:
                    hex_ = "".join(s for s in hex_ for _ in range(2))
                if len(hex_) != 6:
                    raise InvalidConfigError("Invalid color name or hex.")
                try:
                    int(val, 16)
                except ValueError:
                    raise InvalidConfigError("Invalid color name or hex.")
                clean_value = "#" + val
                value_text = clean_value
            else:
                clean_value = hex_
                value_text = f"{val} ({clean_value})"

        elif key in self.time_deltas:
            try:
                isodate.parse_duration(val)
            except isodate.ISO8601Error:
                try:
                    converter = UserFriendlyTime()
                    time = await converter.convert(None, val)
                    if time.arg:
                        raise ValueError
                except BadArgument as exc:
                    raise InvalidConfigError(*exc.args)
                except Exception:
                    raise InvalidConfigError(
                        "Unrecognized time, please use ISO-8601 duration format "
                        'string or a simpler "human readable" time.')
                clean_value = isodate.duration_isoformat(time.dt -
                                                         converter.now)
                value_text = f"{val} ({clean_value})"

        elif key in self.booleans:
            try:
                clean_value = value_text = strtobool(val)
            except ValueError:
                raise InvalidConfigError("Must be a yes/no value.")

        return clean_value, value_text
Beispiel #3
0
    async def clean_data(self, key: str,
                         val: typing.Any) -> typing.Tuple[str, str]:
        value_text = val
        clean_value = val

        # when setting a color
        if key in self.colors:
            hex_ = ALL_COLORS.get(val)

            if hex_ is None:
                if not isinstance(val, str):
                    raise InvalidConfigError('Invalid color name or hex.')
                if val.startswith('#'):
                    val = val[1:]
                if len(val) != 6:
                    raise InvalidConfigError('Invalid color name or hex.')
                for letter in val:
                    if letter not in {
                            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                            'a', 'b', 'c', 'd', 'e', 'f'
                    }:
                        raise InvalidConfigError('Invalid color name or hex.')
                clean_value = '#' + val
                value_text = clean_value
            else:
                clean_value = hex_
                value_text = f'{val} ({clean_value})'

        elif key in self.time_deltas:
            try:
                isodate.parse_duration(val)
            except isodate.ISO8601Error:
                try:
                    converter = UserFriendlyTime()
                    time = await converter.convert(None, val)
                    if time.arg:
                        raise ValueError
                except BadArgument as exc:
                    raise InvalidConfigError(*exc.args)
                except Exception:
                    raise InvalidConfigError(
                        'Unrecognized time, please use ISO-8601 duration format '
                        'string or a simpler "human readable" time.')
                clean_value = isodate.duration_isoformat(time.dt -
                                                         converter.now)
                value_text = f'{val} ({clean_value})'

        return clean_value, value_text
Beispiel #4
0
    async def clean_data(self, key: str,
                         val: typing.Any) -> typing.Tuple[str, str]:
        value_text = val
        clean_value = val

        # when setting a color
        if key in self.colors:
            hex_ = ALL_COLORS.get(val)

            if hex_ is None:
                if not isinstance(val, str):
                    raise InvalidConfigError("Invalid color name or hex.")
                if val.startswith("#"):
                    val = val[1:]
                if len(val) != 6:
                    raise InvalidConfigError("Invalid color name or hex.")
                for letter in val:
                    if letter not in {
                            "0",
                            "1",
                            "2",
                            "3",
                            "4",
                            "5",
                            "6",
                            "7",
                            "8",
                            "9",
                            "a",
                            "b",
                            "c",
                            "d",
                            "e",
                            "f",
                    }:
                        raise InvalidConfigError("Invalid color name or hex.")
                clean_value = "#" + val
                value_text = clean_value
            else:
                clean_value = hex_
                value_text = f"{val} ({clean_value})"

        elif key in self.time_deltas:
            try:
                isodate.parse_duration(val)
            except isodate.ISO8601Error:
                try:
                    converter = UserFriendlyTime()
                    time = await converter.convert(None, val)
                    if time.arg:
                        raise ValueError
                except BadArgument as exc:
                    raise InvalidConfigError(*exc.args)
                except Exception:
                    raise InvalidConfigError(
                        "Unrecognized time, please use ISO-8601 duration format "
                        'string or a simpler "human readable" time.')
                clean_value = isodate.duration_isoformat(time.dt -
                                                         converter.now)
                value_text = f"{val} ({clean_value})"

        return clean_value, value_text