-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpct.py
More file actions
executable file
·41 lines (32 loc) · 1.23 KB
/
pct.py
File metadata and controls
executable file
·41 lines (32 loc) · 1.23 KB
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
#!/usr/bin/env python3
import argparse
import sys
import urllib.parse
USAGE = "%(prog)s [options]"
DESCRIPTION = """Percent-encode and decode strings (like in a URL).
This is a thin wrapper around urllib.parse.quote() and urllib.parse.unquote()."""
def main(argv):
parser = argparse.ArgumentParser(description=DESCRIPTION)
parser.add_argument('operation', choices=('encode', 'decode'),
help='Whether to "encode" or "decode".')
parser.add_argument('string', nargs='?',
help='The string to encode or decode. Omit to read from stdin.')
parser.add_argument('-p', '--preserve', default='',
help='Preserve these characters instead of encoding them. This is in addition to the default '
'preserved characters (letters, numbers, "_", ".", and "-").')
args = parser.parse_args(argv[1:])
if args.string:
lines = [args.string]
else:
lines = sys.stdin
if args.operation == 'encode':
for line in lines:
print(urllib.parse.quote(line, safe=args.preserve), end='')
elif args.operation == 'decode':
for line in lines:
print(urllib.parse.unquote(line), end='')
else:
raise AssertionError('Operation must be "encode" or "decode".')
print()
if __name__ == '__main__':
sys.exit(main(sys.argv))