-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathescapeURI
executable file
·121 lines (89 loc) · 3.16 KB
/
escapeURI
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python3
#
# escapeURI: %-escape a string as needed in a URI.
# 2024-03-11: Written by Steven J. DeRose.
#
import sys
import urllib.parse
__metadata__ = {
"title" : "escapeURI",
"description" : "%-escape a string as needed in a URI.",
"rightsHolder" : "Steven J. DeRose",
"creator" : "http://viaf.org/viaf/50334488",
"type" : "http://purl.org/dc/dcmitype/Software",
"language" : "Python 3.9",
"created" : "2024-03-11",
"modified" : "2024-03-11",
"publisher" : "http://github.com/sderose",
"license" : "https://creativecommons.org/licenses/by-sa/3.0/"
}
__version__ = __metadata__["modified"]
descr = """
=Name=
escapeURI: %-escape a string as needed in a URI.
=Description=
Converts a string(s) to UTF-8 and insert needed %-escapes for it to go ok
inside a URI.
Use --plus to also turn spaces into "+" signs.
Letters, digits, and '_.-~' are never quoted. Use --safe '...' to
make other characters (such as "/" for scheme and directory separators, ":",
"#" for fragment separator, "?" for queries, and so on.
==Usage==
escapeURI [options] [string]
OR
escapeURI [options]
To escape a single string, put it on the command line (quoting if needed to
avoid shell issues).
If no such string is found there, then each line of STDIN will be read and escaped.
This uses Python's ''urllib.parse.quote_plus()'', q.v.
=See also=
My `unescapeURI`.
=History=
* 2024-03-11: Written by Steven J. DeRose.
=Rights=
Written 2024-03-11 by Steven J. DeRose. This work is dedicated to the public domain.
=Options=
"""
###############################################################################
# Main
#
def doit(s:str) -> str:
if (args.plus):
return urllib.parse.quote_plus(s, safe=args.safe)
else:
return urllib.parse.quote(s, safe=args.safe)
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="Encode spaces as plus-signs.")
parser.add_argument(
"--safe", type=str, default="/",
help="List of characters NOT to escape (default: '/').")
parser.add_argument(
"--version", action="version", version=__version__,
help="Display version information, then exit.")
parser.add_argument(
"rest", type=str, nargs='...', #argparse.REMAINDER,
help="A string to escape.")
args0 = parser.parse_args()
return(args0)
args = processOptions()
#print(repr(args.rest))
if (args.rest):
rest = "".join(args.rest)
theBytes = bytes(rest.encode('utf-8'))
print(doit(theBytes))
else:
while (True):
rec = sys.stdin.readline()
if (not rec): break
theBytes = bytes(rec.encode('utf-8'))
print(doit(theBytes))