comparison sat/tools/common/regex.py @ 4037:524856bd7b19

massive refactoring to switch from camelCase to snake_case: historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a pre-PEP8 code, to use the same coding style as in Twisted. However, snake_case is more readable and it's better to follow PEP8 best practices, so it has been decided to move on full snake_case. Because Libervia has a huge codebase, this ended with a ugly mix of camelCase and snake_case. To fix that, this patch does a big refactoring by renaming every function and method (including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case. This is a massive change, and may result in some bugs.
author Goffi <goffi@goffi.org>
date Sat, 08 Apr 2023 13:54:42 +0200
parents 85b8a899f407
children
comparison
equal deleted inserted replaced
4036:c4464d7ae97b 4037:524856bd7b19
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
36 def reJoin(exps): 36 def re_join(exps):
37 """Join (OR) various regexes""" 37 """Join (OR) various regexes"""
38 return re.compile("|".join(exps)) 38 return re.compile("|".join(exps))
39 39
40 40
41 def reSubDict(pattern, repl_dict, string): 41 def re_sub_dict(pattern, repl_dict, string):
42 """Replace key, value found in dict according to pattern 42 """Replace key, value found in dict according to pattern
43 43
44 @param pattern(basestr): pattern using keys found in repl_dict 44 @param pattern(basestr): pattern using keys found in repl_dict
45 @repl_dict(dict): keys found in this dict will be replaced by 45 @repl_dict(dict): keys found in this dict will be replaced by
46 corresponding values 46 corresponding values
47 @param string(basestr): string to use for the replacement 47 @param string(basestr): string to use for the replacement
48 """ 48 """
49 return pattern.sub(lambda m: repl_dict[re.escape(m.group(0))], string) 49 return pattern.sub(lambda m: repl_dict[re.escape(m.group(0))], string)
50 50
51 51
52 path_escape_re = reJoin(list(path_escape.keys())) 52 path_escape_re = re_join(list(path_escape.keys()))
53 path_escape_rev_re = reJoin(list(path_escape_rev.keys())) 53 path_escape_rev_re = re_join(list(path_escape_rev.keys()))
54 54
55 55
56 def pathEscape(string): 56 def path_escape(string):
57 """Escape string so it can be use in a file path 57 """Escape string so it can be use in a file path
58 58
59 @param string(basestr): string to escape 59 @param string(basestr): string to escape
60 @return (str, unicode): escaped string, usable in a file path 60 @return (str, unicode): escaped string, usable in a file path
61 """ 61 """
62 return reSubDict(path_escape_re, path_escape, string) 62 return re_sub_dict(path_escape_re, path_escape, string)
63 63
64 64
65 def pathUnescape(string): 65 def path_unescape(string):
66 """Unescape string from value found in file path 66 """Unescape string from value found in file path
67 67
68 @param string(basestr): string found in file path 68 @param string(basestr): string found in file path
69 @return (str, unicode): unescaped string 69 @return (str, unicode): unescaped string
70 """ 70 """
71 return reSubDict(path_escape_rev_re, path_escape_rev, string) 71 return re_sub_dict(path_escape_rev_re, path_escape_rev, string)
72 72
73 73
74 def ansiRemove(string): 74 def ansi_remove(string):
75 """Remove ANSI escape codes from string 75 """Remove ANSI escape codes from string
76 76
77 @param string(basestr): string to filter 77 @param string(basestr): string to filter
78 @return (str, unicode): string without ANSI escape codes 78 @return (str, unicode): string without ANSI escape codes
79 """ 79 """
80 return RE_ANSI_REMOVE.sub("", string) 80 return RE_ANSI_REMOVE.sub("", string)
81 81
82 82
83 def urlFriendlyText(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()