comparison sat/tools/common/regex.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 003b8b4b56a7
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 """ regex tools common to backend and frontends """ 20 """ regex tools common to backend and frontends """
21 21
22 import re 22 import re
23 path_escape = {'%': '%25', '/': '%2F', '\\': '%5c'} 23
24 path_escape_rev = {re.escape(v):k for k, v in path_escape.iteritems()} 24 path_escape = {"%": "%25", "/": "%2F", "\\": "%5c"}
25 path_escape = {re.escape(k):v for k, v in path_escape.iteritems()} 25 path_escape_rev = {re.escape(v): k for k, v in path_escape.iteritems()}
26 # thanks to Martijn Pieters (https://stackoverflow.com/a/14693789) 26 path_escape = {re.escape(k): v for k, v in path_escape.iteritems()}
27 RE_ANSI_REMOVE = re.compile(r'\x1b[^m]*m') 27 #  thanks to Martijn Pieters (https://stackoverflow.com/a/14693789)
28 RE_ANSI_REMOVE = re.compile(r"\x1b[^m]*m")
28 29
29 30
30 def reJoin(exps): 31 def reJoin(exps):
31 """Join (OR) various regexes""" 32 """Join (OR) various regexes"""
32 return re.compile('|'.join(exps)) 33 return re.compile("|".join(exps))
33 34
34 35
35 def reSubDict(pattern, repl_dict, string): 36 def reSubDict(pattern, repl_dict, string):
36 """Replace key, value found in dict according to pattern 37 """Replace key, value found in dict according to pattern
37 38
39 @repl_dict(dict): keys found in this dict will be replaced by 40 @repl_dict(dict): keys found in this dict will be replaced by
40 corresponding values 41 corresponding values
41 @param string(basestr): string to use for the replacement 42 @param string(basestr): string to use for the replacement
42 """ 43 """
43 return pattern.sub(lambda m: repl_dict[re.escape(m.group(0))], string) 44 return pattern.sub(lambda m: repl_dict[re.escape(m.group(0))], string)
45
44 46
45 path_escape_re = reJoin(path_escape.keys()) 47 path_escape_re = reJoin(path_escape.keys())
46 path_escape_rev_re = reJoin(path_escape_rev.keys()) 48 path_escape_rev_re = reJoin(path_escape_rev.keys())
47 49
48 50
61 @param string(basestr): string found in file path 63 @param string(basestr): string found in file path
62 @return (str, unicode): unescaped string 64 @return (str, unicode): unescaped string
63 """ 65 """
64 return reSubDict(path_escape_rev_re, path_escape_rev, string) 66 return reSubDict(path_escape_rev_re, path_escape_rev, string)
65 67
68
66 def ansiRemove(string): 69 def ansiRemove(string):
67 """Remove ANSI escape codes from string 70 """Remove ANSI escape codes from string
68 71
69 @param string(basestr): string to filter 72 @param string(basestr): string to filter
70 @return (str, unicode): string without ANSI escape codes 73 @return (str, unicode): string without ANSI escape codes
71 """ 74 """
72 return RE_ANSI_REMOVE.sub('', string) 75 return RE_ANSI_REMOVE.sub("", string)