comparison src/tools/common/regex.py @ 2297:ad2a8e8b52da

core (tools/common/regex): new ansiRemove method to remove ANSI escape codes from a string
author Goffi <goffi@goffi.org>
date Sun, 02 Jul 2017 19:53:44 +0200
parents 2daf7b4c6756
children 8b37a62336c3
comparison
equal deleted inserted replaced
2296:1a64fd7b648d 2297:ad2a8e8b52da
21 21
22 import re 22 import re
23 path_escape = {'%': '%25', '/': '%2F', '\\': '%5c'} 23 path_escape = {'%': '%25', '/': '%2F', '\\': '%5c'}
24 path_escape_rev = {re.escape(v):k for k, v in path_escape.iteritems()} 24 path_escape_rev = {re.escape(v):k for k, v in path_escape.iteritems()}
25 path_escape = {re.escape(k):v for k, v in path_escape.iteritems()} 25 path_escape = {re.escape(k):v for k, v in path_escape.iteritems()}
26 # thanks to Martijn Pieters (https://stackoverflow.com/a/14693789)
27 RE_ANSI_REMOVE = re.compile(r'\x1b[^m]*m')
26 28
27 29
28 def reJoin(exps): 30 def reJoin(exps):
29 """Join (OR) various regexes""" 31 """Join (OR) various regexes"""
30 return re.compile('|'.join(exps)) 32 return re.compile('|'.join(exps))
58 60
59 @param string(basestr): string found in file path 61 @param string(basestr): string found in file path
60 @return (str, unicode): unescaped string 62 @return (str, unicode): unescaped string
61 """ 63 """
62 return reSubDict(path_escape_rev_re, path_escape_rev, string) 64 return reSubDict(path_escape_rev_re, path_escape_rev, string)
65
66 def ansiRemove(string):
67 """Remove ANSI escape codes from string
68
69 @param string(basestr): string to filter
70 @return (str, unicode): string without ANSI escape codes
71 """
72 return RE_ANSI_REMOVE.sub('', string)