Debian Codegraph @

keyrings/alt/escape.py

raw symbols SCIP
1 """
2 escape/unescape routines available for backends which need
3 alphanumeric usernames, services, or other values
4 """
5
6 import rescip-python python python-stdlib 3.11 re/__init__:
7 import stringscip-python python python-stdlib 3.11 string/__init__:
8
9 LEGAL_CHARSscip-python python keyrings.alt 5.0.2 `keyrings.alt.escape`/LEGAL_CHARS. = (
10 getattrscip-python python python-stdlib 3.11 builtins/getattr().(stringscip-python python python-stdlib 3.11 string/__init__:, 'letters', None) # Python 2
11 or stringscip-python python python-stdlib 3.11 string/__init__:.ascii_lettersscip-python python python-stdlib 3.11 string/ascii_letters.ascii_letters. # Python 3
12 ) + stringscip-python python python-stdlib 3.11 string/__init__:.digitsscip-python python python-stdlib 3.11 string/digits.digits.
13
14 ESCAPE_FMTscip-python python keyrings.alt 5.0.2 `keyrings.alt.escape`/ESCAPE_FMT. = "_%02X"
15
16
17 def _escape_charscip-python python keyrings.alt 5.0.2 `keyrings.alt.escape`/_escape_char().(cscip-python python keyrings.alt 5.0.2 `keyrings.alt.escape`/_escape_char().(c)):
18 "Single char escape. Return the char, escaped if not already legal"
19 if isinstance(cscip-python python keyrings.alt 5.0.2 `keyrings.alt.escape`/_escape_char().(c), intscip-python python python-stdlib 3.11 builtins/int#):
20 cscip-python python keyrings.alt 5.0.2 `keyrings.alt.escape`/_escape_char().(c) = chr(cscip-python python keyrings.alt 5.0.2 `keyrings.alt.escape`/_escape_char().(c))
21 return cscip-python python keyrings.alt 5.0.2 `keyrings.alt.escape`/_escape_char().(c) if cscip-python python keyrings.alt 5.0.2 `keyrings.alt.escape`/_escape_char().(c) in LEGAL_CHARSscip-python python keyrings.alt 5.0.2 `keyrings.alt.escape`/LEGAL_CHARS. else ESCAPE_FMTscip-python python keyrings.alt 5.0.2 `keyrings.alt.escape`/ESCAPE_FMT. % ord(cscip-python python keyrings.alt 5.0.2 `keyrings.alt.escape`/_escape_char().(c))
22
23
24 def escapescip-python python keyrings.alt 5.0.2 `keyrings.alt.escape`/escape().(valuescip-python python keyrings.alt 5.0.2 `keyrings.alt.escape`/escape().(value)):
25 """
26 Escapes given string so the result consists of alphanumeric chars and
27 underscore only.
28 """
29 return "".joinscip-python python python-stdlib 3.11 builtins/str#join().(_escape_charscip-python python keyrings.alt 5.0.2 `keyrings.alt.escape`/_escape_char().(c) for c in valuescip-python python keyrings.alt 5.0.2 `keyrings.alt.escape`/escape().(value).encode('utf-8'))
30
31
32 def _unescape_codescip-python python keyrings.alt 5.0.2 `keyrings.alt.escape`/_unescape_code().(regex_matchscip-python python keyrings.alt 5.0.2 `keyrings.alt.escape`/_unescape_code().(regex_match)):
33 ordinal = intscip-python python python-stdlib 3.11 builtins/int#(regex_matchscip-python python keyrings.alt 5.0.2 `keyrings.alt.escape`/_unescape_code().(regex_match).group('code'), 16)
34 return bytesscip-python python python-stdlib 3.11 builtins/bytes#([ordinal])
35
36
37 def unescapescip-python python keyrings.alt 5.0.2 `keyrings.alt.escape`/unescape().(valuescip-python python keyrings.alt 5.0.2 `keyrings.alt.escape`/unescape().(value)):
38 """
39 Inverse of escape.
40 """
41 pattern = ESCAPE_FMTscip-python python keyrings.alt 5.0.2 `keyrings.alt.escape`/ESCAPE_FMT..replacescip-python python python-stdlib 3.11 builtins/str#replace().('%02X', '(?P<code>[0-9A-Fa-f]{2})')
42 # the pattern must be bytes to operate on bytes
43 pattern_bytes = pattern.encodescip-python python python-stdlib 3.11 builtins/str#encode().('ascii')
44 re_esc = rescip-python python python-stdlib 3.11 re/__init__:.compilescip-python python python-stdlib 3.11 re/compile().(pattern_bytes)
45 return re_esc.subscip-python python python-stdlib 3.11 re/Pattern#sub().(_unescape_codescip-python python keyrings.alt 5.0.2 `keyrings.alt.escape`/_unescape_code()., valuescip-python python keyrings.alt 5.0.2 `keyrings.alt.escape`/unescape().(value).encode('ascii')).decodescip-python python python-stdlib 3.11 builtins/bytes#decode().('utf-8')