How to use the pathvalidate._common.preprocess 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 / pathvalidate / _filepath.py View on Github external
def sanitize(self, value: PathType, replacement_text: str = "") -> PathType:
        if not value:
            return ""

        self.__fpath_validator.validate_abspath(value)

        unicode_filepath = preprocess(value)

        if self.__normalize:
            unicode_filepath = os.path.normpath(unicode_filepath)

        drive, unicode_filepath = self.__split_drive(unicode_filepath)
        sanitized_path = self._sanitize_regexp.sub(replacement_text, unicode_filepath)
        if self._is_windows():
            path_separator = "\\"
        else:
            path_separator = "/"

        sanitized_entries = []  # type: List[str]
        if drive:
            sanitized_entries.append(drive)
        for entry in sanitized_path.replace("\\", "/").split("/"):
            if entry in _NTFS_RESERVED_FILE_NAMES:
github thombashi / pathvalidate / pathvalidate / _app.py View on Github external
If the ``sheet_name`` includes invalid char(s):
        |invalid_excel_sheet_chars|.
    :raises pathvalidate.InvalidLengthError:
        If the ``sheet_name`` is longer than 31 characters.
    """

    validate_null_string(sheet_name)

    if len(sheet_name) > __MAX_SHEET_NAME_LEN:
        raise InvalidLengthError(
            "sheet name is too long: expected<={:d}, actual={:d}".format(
                __MAX_SHEET_NAME_LEN, len(sheet_name)
            )
        )

    unicode_sheet_name = preprocess(sheet_name)
    match = __RE_INVALID_EXCEL_SHEET_NAME.search(unicode_sheet_name)
    if match is not None:
        raise InvalidCharError(
            "invalid char found in the sheet name: '{:s}'".format(re.escape(match.group()))
        )
github thombashi / pathvalidate / pathvalidate / _symbol.py View on Github external
def validate_unprintable(text: str) -> None:
    # deprecated
    match_list = __RE_UNPRINTABLE.findall(preprocess(text))
    if match_list:
        raise InvalidCharError("unprintable character found: {}".format(match_list))
github thombashi / pathvalidate / pathvalidate / variable / _base.py View on Github external
def _validate(self, value):
        self._validate_null_string(value)

        unicode_var_name = preprocess(value)

        if self._is_reserved_keyword(unicode_var_name):
            raise InvalidReservedNameError(
                "{:s} is a reserved keyword by python".format(unicode_var_name)
            )

        match = self._invalid_var_name_re.search(unicode_var_name)
        if match is not None:
            raise InvalidCharError(
                "invalid char found in the variable name: '{}'".format(re.escape(match.group()))
            )

        match = self._invalid_var_name_head_re.search(unicode_var_name)
        if match is not None:
            raise InvalidCharError(
                "the first character of the variable name is invalid: '{}'".format(
github thombashi / pathvalidate / pathvalidate / _ltsv.py View on Github external
def validate_ltsv_label(label: str) -> None:
    """
    Verifying whether ``label`` is a valid
    `Labeled Tab-separated Values (LTSV) `__ label or not.

    :param label: Label to validate.
    :raises pathvalidate.ValidationError:
        If invalid character(s) found in the ``label`` for a LTSV format label.
    """

    validate_pathtype(label, error_msg="label is empty")

    match_list = __RE_INVALID_LTSV_LABEL.findall(preprocess(label))
    if match_list:
        raise InvalidCharError(
            "invalid character found for a LTSV format label: {}".format(match_list)
        )
github thombashi / pathvalidate / pathvalidate / _ltsv.py View on Github external
def sanitize_ltsv_label(label: str, replacement_text: str = "") -> str:
    """
    Replace all of the symbols in text.

    :param label: Input text.
    :param replacement_text: Replacement text.
    :return: A replacement string.
    :rtype: str
    """

    validate_pathtype(label, error_msg="label is empty")

    return __RE_INVALID_LTSV_LABEL.sub(replacement_text, preprocess(label))
github thombashi / pathvalidate / pathvalidate / _symbol.py View on Github external
def replace_unprintable(text: str, replacement_text: str = "") -> str:
    # deprecated
    try:
        return __RE_UNPRINTABLE.sub(replacement_text, preprocess(text))
    except (TypeError, AttributeError):
        raise TypeError("text must be a string")
github thombashi / pathvalidate / pathvalidate / _filename.py View on Github external
def validate(self, value: PathType) -> None:
        validate_pathtype(value)

        unicode_filename = preprocess(value)
        value_len = len(unicode_filename)

        self.validate_abspath(unicode_filename)

        if value_len > self.max_len:
            raise InvalidLengthError(
                "filename is too long: expected<={:d}, actual={:d}".format(self.max_len, value_len)
            )
        if value_len < self.min_len:
            raise InvalidLengthError(
                "filename is too short: expected>={:d}, actual={:d}".format(self.min_len, value_len)
            )

        self._validate_reserved_keywords(unicode_filename)

        if self._is_universal() or self._is_windows():
github thombashi / pathvalidate / pathvalidate / _app.py View on Github external
the ``sheet_name`` with the ``replacement_text``.
    Invalid characters are as follows:
    |invalid_excel_sheet_chars|.
    The ``sheet_name`` truncate to 31 characters
    (max sheet name length of Excel) from the head, if the length
    of the name is exceed 31 characters.

    :param str sheet_name: Excel sheet name to sanitize.
    :param str replacement_text: Replacement text.
    :return: A replacement string.
    :rtype: str
    :raises ValueError: If the ``sheet_name`` is an invalid sheet name.
    """

    try:
        unicode_sheet_name = preprocess(sheet_name)
    except AttributeError as e:
        raise ValueError(e)

    modify_sheet_name = __RE_INVALID_EXCEL_SHEET_NAME.sub(replacement_text, unicode_sheet_name)

    return modify_sheet_name[:__MAX_SHEET_NAME_LEN]