File size: 1,203 Bytes
93e2cf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
.. codeauthor:: Tsuyoshi Hombashi <[email protected]>
"""

import re

from ._common import to_str, validate_pathtype
from .error import InvalidCharError


__RE_INVALID_LTSV_LABEL = re.compile("[^0-9A-Za-z_.-]", re.UNICODE)


def validate_ltsv_label(label: str) -> None:
    """
    Verifying whether ``label`` is a valid
    `Labeled Tab-separated Values (LTSV) <http://ltsv.org/>`__ 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, allow_whitespaces=False)

    match_list = __RE_INVALID_LTSV_LABEL.findall(to_str(label))
    if match_list:
        raise InvalidCharError(f"invalid character found for a LTSV format label: {match_list}")


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, allow_whitespaces=False)

    return __RE_INVALID_LTSV_LABEL.sub(replacement_text, to_str(label))