/usr/lib/python3/dist-packages/keyring/util/escape.py is in python3-keyring 10.6.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | """
escape/unescape routines available for backends which need
alphanumeric usernames, services, or other values
"""
import re
import string
import sys
# True if we are running on Python 3.
# taken from six.py
PY3 = sys.version_info[0] == 3
# allow use of unicode literals
if PY3:
def u(s):
return s
def _unichr(c):
return chr(c)
else:
def u(s):
return s.decode('utf-8')
def _unichr(c):
return unichr(c)
LEGAL_CHARS = (
getattr(string, 'letters', None) # Python 2
or getattr(string, 'ascii_letters') # Python 3
) + string.digits
ESCAPE_FMT = "_%02X"
def _escape_char(c):
"Single char escape. Return the char, escaped if not already legal"
if isinstance(c, int):
c = _unichr(c)
return c if c in LEGAL_CHARS else ESCAPE_FMT % ord(c)
def escape(value):
"""
Escapes given string so the result consists of alphanumeric chars and
underscore only.
"""
return "".join(_escape_char(c) for c in value.encode('utf-8'))
def _unescape_code(regex_match):
ordinal = int(regex_match.group('code'), 16)
if sys.version_info >= (3,):
return bytes([ordinal])
return chr(ordinal)
def unescape(value):
"""
Inverse of escape.
"""
re_esc = re.compile(
# the pattern must be bytes to operate on bytes
ESCAPE_FMT.replace('%02X', '(?P<code>[0-9A-Fa-f]{2})').encode('ascii')
)
return re_esc.sub(_unescape_code, value.encode('ascii')).decode('utf-8')
|