music-kraken-core/src/music_kraken/utils/string_processing.py

36 lines
750 B
Python
Raw Normal View History

2023-04-18 20:39:19 +00:00
from transliterate.exceptions import LanguageDetectionError
from transliterate import translit
2023-05-10 14:39:44 +00:00
from pathvalidate import sanitize_filename
2023-04-18 20:39:19 +00:00
def unify(string: str) -> str:
"""
2023-04-18 20:39:19 +00:00
returns a unified str, to make comparisons easy.
a unified string has the following attributes:
- is lowercase
"""
2023-04-18 20:39:19 +00:00
try:
string = translit(string, reversed=True)
except LanguageDetectionError:
pass
return string.lower()
2023-04-03 09:17:55 +00:00
2023-04-18 20:39:19 +00:00
2023-04-03 09:17:55 +00:00
def fit_to_file_system(string: str) -> str:
string = string.strip()
2023-04-18 20:39:19 +00:00
2023-04-03 09:17:55 +00:00
while string[0] == ".":
if len(string) == 0:
return string
2023-04-18 20:39:19 +00:00
2023-04-03 09:17:55 +00:00
string = string[1:]
2023-04-18 20:39:19 +00:00
2023-04-03 09:17:55 +00:00
string = string.replace("/", "|").replace("\\", "|")
2023-04-18 20:39:19 +00:00
2023-05-10 14:39:44 +00:00
string = sanitize_filename(string)
2023-04-03 09:17:55 +00:00
return string