-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunescapeURI
executable file
·103 lines (74 loc) · 2.72 KB
/
unescapeURI
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
#
# unescapeURI: Handle % and + escape in URLs, even for Unicode.
# 2024-04-14: Written by Steven J. DeRose.
#
from urllib.parse import unquote
__metadata__ = {
"title" : "unescapeURI",
"description" : "Handle % and + escape in URLs, even for Unicode.",
"rightsHolder" : "Steven J. DeRose",
"creator" : "http://viaf.org/viaf/50334488",
"type" : "http://purl.org/dc/dcmitype/Software",
"language" : "Python 3.9",
"created" : "2024-04-14",
"modified" : "2024-04-14",
"publisher" : "http://github.com/sderose",
"license" : "https://creativecommons.org/licenses/by-sa/3.0/"
}
__version__ = __metadata__["modified"]
descr = """
=Name=
unescapeURI: Handle % and + escape in URLs, even for Unicode.
=Description=
==Usage==
unescapeURI.py [options] [s]
Resolve and %xx escapes in s, Interpret the result as utf-8, and display.
If --plus is set, also turn any unescaped "+" into spaces.
=See also=
My `escapeURI`.
=History=
* 2024-04-14: Written by Steven J. DeRose to replace old Perl version.
=Rights=
By Steven J. DeRose. This work is dedicated to the public domain.
For the most recent version, see [http://www.derose.net/steve/utilities]
or [https://github.com/sderose].
=Options=
"""
###############################################################################
# Main
#
if __name__ == "__main__":
import argparse
def processOptions() -> argparse.Namespace:
try:
from BlockFormatter import BlockFormatter
parser = argparse.ArgumentParser(
description=descr, formatter_class=BlockFormatter)
except ImportError:
parser = argparse.ArgumentParser(description=descr)
parser.add_argument(
"--plus", action="store_true",
help="Also turn (unescaped) '+' into space.")
parser.add_argument(
"--quiet", "-q", action="store_true",
help="Suppress most messages.")
parser.add_argument(
"--version", action="version", version=__version__,
help="Display version information, then exit.")
parser.add_argument(
"uri", type=str, nargs=argparse.REMAINDER,
help="A percent-escaped string to fix.")
args0 = parser.parse_args()
return(args0)
###########################################################################
#
args = processOptions()
if (not args.uri):
args.uri = "https://example.edu/foo+bar%e2%80%a2+baz"
print("No string supplied. For example:\n%s ==>" % (args.uri))
s = args.uri
if (args.plus): s = s.replace("+", " ")
s = unquote(s)
s = s.encode('utf-8').decode('utf-8')
print(s)