How to use the pathvalidate.error.ErrorReason function in pathvalidate

To help you get started, we’ve selected a few pathvalidate examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github thombashi / pathvalidate / test / test_ltsv.py View on Github external
def test_exception_invalid_char(self, value):
        with pytest.raises(ValidationError) as e:
            validate_ltsv_label(value)
        assert e.value.reason == ErrorReason.INVALID_CHARACTER
github thombashi / pathvalidate / test / test_symbol.py View on Github external
def test_exception_invalid_char(self, value):
        with pytest.raises(ValidationError) as e:
            validate_unprintable(value)
        assert e.value.reason == ErrorReason.INVALID_CHARACTER
github thombashi / pathvalidate / pathvalidate / error.py View on Github external
def __init__(self, *args, **kwargs) -> None:
        kwargs["reason"] = ErrorReason.INVALID_LENGTH

        super().__init__(args, **kwargs)
github thombashi / pathvalidate / pathvalidate / _common.py View on Github external
def validate_pathtype(text: PathType, error_msg: Optional[str] = None) -> None:
    from .error import ErrorReason, ValidationError

    if _is_not_null_string(text) or is_pathlike_obj(text):
        return

    if is_null_string(text):
        if not error_msg:
            error_msg = "the value must be a not empty"

        raise ValidationError(
            description=error_msg, reason=ErrorReason.NULL_NAME,
        )

    raise TypeError("text must be a string: actual={}".format(type(text)))
github thombashi / pathvalidate / pathvalidate / _filename.py View on Github external
def validate_abspath(self, value: str) -> None:
        if any([ntpath.isabs(value), posixpath.isabs(value)]):
            raise ValidationError(
                description="found an absolute path ({}), expected a filename".format(value),
                platform=self.platform,
                reason=ErrorReason.FOUND_ABS_PATH,
            )
github thombashi / pathvalidate / pathvalidate / _filename.py View on Github external
except ValidationError as e:
            if e.reason == ErrorReason.NULL_NAME:
                return ""
            raise

        sanitized_filename = self._sanitize_regexp.sub(replacement_text, str(value))
        sanitized_filename = sanitized_filename[: self.max_len]

        try:
            self.__validator.validate(sanitized_filename)
        except ValidationError as e:
            if e.reason == ErrorReason.RESERVED_NAME and e.reusable_name is False:
                sanitized_filename = re.sub(
                    re.escape(e.reserved_name), "{}_".format(e.reserved_name), sanitized_filename
                )
            elif e.reason == ErrorReason.INVALID_CHARACTER:
                if self.platform in [Platform.UNIVERSAL, Platform.WINDOWS]:
                    sanitized_filename = sanitized_filename.rstrip(" .")

        if is_pathlike_obj(value):
            return Path(sanitized_filename)

        return sanitized_filename
github thombashi / pathvalidate / pathvalidate / error.py View on Github external
def __init__(self, *args, **kwargs) -> None:
        kwargs["reason"] = ErrorReason.RESERVED_NAME

        super().__init__(args, **kwargs)
github thombashi / pathvalidate / pathvalidate / error.py View on Github external
def __str__(self) -> str:
        item_list = []

        if Exception.__str__(self):
            item_list.append(Exception.__str__(self))

        if self.reason:
            item_list.append("reason={}".format(cast(ErrorReason, self.reason).value))
        if self.platform:
            item_list.append("target-platform={}".format(self.platform.value))
        if self.description:
            item_list.append("description={}".format(self.description))
        if self.__reusable_name is not None:
            item_list.append("reusable_name={}".format(self.reusable_name))

        return ", ".join(item_list).strip()
github thombashi / pathvalidate / pathvalidate / error.py View on Github external
def __init__(self, *args, **kwargs) -> None:
        kwargs["reason"] = ErrorReason.NULL_NAME

        super().__init__(args, **kwargs)
github thombashi / pathvalidate / pathvalidate / _filename.py View on Github external
def sanitize(self, value: PathType, replacement_text: str = "") -> PathType:
        try:
            validate_pathtype(value)
        except ValidationError as e:
            if e.reason == ErrorReason.NULL_NAME:
                return ""
            raise

        sanitized_filename = self._sanitize_regexp.sub(replacement_text, str(value))
        sanitized_filename = sanitized_filename[: self.max_len]

        try:
            self.__validator.validate(sanitized_filename)
        except ValidationError as e:
            if e.reason == ErrorReason.RESERVED_NAME and e.reusable_name is False:
                sanitized_filename = re.sub(
                    re.escape(e.reserved_name), "{}_".format(e.reserved_name), sanitized_filename
                )
            elif e.reason == ErrorReason.INVALID_CHARACTER:
                if self.platform in [Platform.UNIVERSAL, Platform.WINDOWS]:
                    sanitized_filename = sanitized_filename.rstrip(" .")