comparison libervia/backend/tools/common/regex.py @ 4270:0d7bb4df2343

Reformatted code base using black.
author Goffi <goffi@goffi.org>
date Wed, 19 Jun 2024 18:44:57 +0200
parents 4b842c1fb686
children 6baea959dc33
comparison
equal deleted inserted replaced
4269:64a85ce8be70 4270:0d7bb4df2343
24 24
25 path_escape = {"%": "%25", "/": "%2F", "\\": "%5c"} 25 path_escape = {"%": "%25", "/": "%2F", "\\": "%5c"}
26 path_escape_rev = {re.escape(v): k for k, v in path_escape.items()} 26 path_escape_rev = {re.escape(v): k for k, v in path_escape.items()}
27 path_escape = {re.escape(k): v for k, v in path_escape.items()} 27 path_escape = {re.escape(k): v for k, v in path_escape.items()}
28 #  thanks to Martijn Pieters (https://stackoverflow.com/a/14693789) 28 #  thanks to Martijn Pieters (https://stackoverflow.com/a/14693789)
29 RE_ANSI_REMOVE = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])') 29 RE_ANSI_REMOVE = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
30 RE_TEXT_URL = re.compile(r'[^a-zA-Z0-9,_]+') 30 RE_TEXT_URL = re.compile(r"[^a-zA-Z0-9,_]+")
31 TEXT_MAX_LEN = 60 31 TEXT_MAX_LEN = 60
32 # min lenght is currently deactivated 32 # min lenght is currently deactivated
33 TEXT_WORD_MIN_LENGHT = 0 33 TEXT_WORD_MIN_LENGHT = 0
34 34
35 35
82 82
83 def url_friendly_text(text): 83 def url_friendly_text(text):
84 """Convert text to url-friendly one""" 84 """Convert text to url-friendly one"""
85 # we change special chars to ascii one, 85 # we change special chars to ascii one,
86 # trick found at https://stackoverflow.com/a/3194567 86 # trick found at https://stackoverflow.com/a/3194567
87 text = unicodedata.normalize('NFD', text).encode('ascii', 'ignore').decode('utf-8') 87 text = unicodedata.normalize("NFD", text).encode("ascii", "ignore").decode("utf-8")
88 text = RE_TEXT_URL.sub(' ', text).lower() 88 text = RE_TEXT_URL.sub(" ", text).lower()
89 text = '-'.join([t for t in text.split() if t and len(t)>=TEXT_WORD_MIN_LENGHT]) 89 text = "-".join([t for t in text.split() if t and len(t) >= TEXT_WORD_MIN_LENGHT])
90 while len(text) > TEXT_MAX_LEN: 90 while len(text) > TEXT_MAX_LEN:
91 if '-' in text: 91 if "-" in text:
92 text = text.rsplit('-', 1)[0] 92 text = text.rsplit("-", 1)[0]
93 else: 93 else:
94 text = text[:TEXT_MAX_LEN] 94 text = text[:TEXT_MAX_LEN]
95 return text 95 return text