69 lines
1.6 KiB
Python
69 lines
1.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
import re
|
|
|
|
|
|
__author__ = "Mike Belov"
|
|
__copyright__ = "Copyright (C) Nginx, Inc. All rights reserved."
|
|
__license__ = ""
|
|
__maintainer__ = "Grant Hulegaard"
|
|
__email__ = "grant.hulegaard@nginx.com"
|
|
|
|
|
|
escape_regex = re.compile('\\\\(?P<value>[\!\@\#\$\%\^\&\*\(\)\[\]\{\}\;\:\,\.\/\<\>\?\|\`\~\-\=\_\+abfrtv0-9]{1})')
|
|
|
|
|
|
escape_values = {
|
|
'a': ('\\a', '\a'),
|
|
'b': ('\\b', '\b'),
|
|
'f': ('\\f', '\f'),
|
|
'r': ('\\r', '\r'),
|
|
't': ('\\t', '\t'),
|
|
'v': ('\\v', '\v'),
|
|
'!': ('\\!', '\!'),
|
|
'@': ('\\@', '\@'),
|
|
'#': ('\\#', '\#'),
|
|
'$': ('\\$', '\$'),
|
|
'%': ('\\%', '\%'),
|
|
'^': ('\\^', '\^'),
|
|
'&': ('\\&', '\&'),
|
|
'*': ('\\*', '\*'),
|
|
'(': ('\\(', '\('),
|
|
')': ('\\)', '\)'),
|
|
'[': ('\\[', '\['),
|
|
']': ('\\]', '\]'),
|
|
'{': ('\\{', '\}'),
|
|
'}': ('\\}', '\}'),
|
|
';': ('\\;', '\;'),
|
|
':': ('\\:', '\:'),
|
|
',': ('\\,', '\,'),
|
|
'.': ('\\.', '\.'),
|
|
'/': ('\\/', '\/'),
|
|
'<': ('\\<', '\<'),
|
|
'>': ('\\>', '\>'),
|
|
'?': ('\\?', '\?'),
|
|
'|': ('\\|', '\|'),
|
|
'`': ('\\`', '\`'),
|
|
'~': ('\\~', '\~'),
|
|
'-': ('\\-', '\-'),
|
|
'=': ('\\=', '\='),
|
|
'_': ('\\_', '\_'),
|
|
'+': ('\\+', '\+'),
|
|
'1': ('\\1', '\1'),
|
|
'2': ('\\2', '\2'),
|
|
'3': ('\\3', '\3'),
|
|
'4': ('\\4', '\4'),
|
|
'5': ('\\5', '\5'),
|
|
'6': ('\\6', '\6'),
|
|
'7': ('\\7', '\7'),
|
|
'8': ('\\8', '\8'),
|
|
'9': ('\\9', '\9'),
|
|
'0': ('\\0', '\0')
|
|
}
|
|
|
|
|
|
def prep_raw(value):
|
|
for match_char in set(escape_regex.findall(value)):
|
|
start, end = escape_values[match_char]
|
|
value = value.replace(start, end)
|
|
return value
|