-
Notifications
You must be signed in to change notification settings - Fork 0
/
url_as_file.py
56 lines (50 loc) · 1.38 KB
/
url_as_file.py
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
import codecs
import string
from typing import Tuple
escape_char = "!"
_encode_table = {
escape_char: escape_char,
#replace illegal chars
"\\": "{",
"/": "}",
"<": "[",
">": "]",
":": ";",
"*": "+",
"?": "`",
"\"": "'",
"|": "~",
#avoid filename extension confusion
".": ","
}
_decode_table = {v: k for k, v in _encode_table.items()}
#Put the escape character in front of literal characters.
def url_as_file_encode(url: str) -> Tuple[str, int]:
l = []
for x in url:
if x in _decode_table.keys():
l.append(escape_char + x)
elif x in _encode_table.keys():
l.append(_encode_table[x])
else:
l.append(x)
return ''.join(l), len(url)
def url_as_file_decode(text: str) -> Tuple[str, int]:
out = []
is_escaped = False
for c in text:
if c in _decode_table:
if is_escaped:
out.append(c)
is_escaped = False
elif c == escape_char:
is_escaped = True
else:
out.append(_decode_table[c])
else:
out.append(c)
#is_escaped = False
return ''.join(out), len(text)
def url_as_file_search_function(encoding_name):
return codecs.CodecInfo(url_as_file_encode, url_as_file_decode, name='url-as-file')
codecs.register(url_as_file_search_function)