_ltsv.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """
  2. .. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
  3. """
  4. import re
  5. from typing import Final
  6. from ._common import to_str, validate_pathtype
  7. from .error import InvalidCharError
  8. __RE_INVALID_LTSV_LABEL: Final = re.compile("[^0-9A-Za-z_.-]", re.UNICODE)
  9. def validate_ltsv_label(label: str) -> None:
  10. """
  11. Verifying whether ``label`` is a valid
  12. `Labeled Tab-separated Values (LTSV) <http://ltsv.org/>`__ label or not.
  13. :param label: Label to validate.
  14. :raises pathvalidate.ValidationError:
  15. If invalid character(s) found in the ``label`` for a LTSV format label.
  16. """
  17. validate_pathtype(label, allow_whitespaces=False)
  18. match_list = __RE_INVALID_LTSV_LABEL.findall(to_str(label))
  19. if match_list:
  20. raise InvalidCharError(f"invalid character found for a LTSV format label: {match_list}")
  21. def sanitize_ltsv_label(label: str, replacement_text: str = "") -> str:
  22. """
  23. Replace all of the symbols in text.
  24. :param label: Input text.
  25. :param replacement_text: Replacement text.
  26. :return: A replacement string.
  27. :rtype: str
  28. """
  29. validate_pathtype(label, allow_whitespaces=False)
  30. return __RE_INVALID_LTSV_LABEL.sub(replacement_text, to_str(label))