50 lines
1.1 KiB
Python
Executable file
50 lines
1.1 KiB
Python
Executable file
#!/usr/bin/env python
|
|
|
|
"""
|
|
Encode/decode url strings in stdin or the given file(s).
|
|
"""
|
|
|
|
import getopt
|
|
import fileinput
|
|
import sys
|
|
import os
|
|
try:
|
|
from urllib.parse import quote_plus, quote, unquote_plus, unquote
|
|
except ImportError:
|
|
from urllib import quote_plus, quote, unquote_plus, unquote
|
|
|
|
|
|
def usage():
|
|
"""Print out help for this script"""
|
|
print("Usage: {} [-edEDh] file1[ file2[ ...]]".format(os.path.basename(sys.argv[0])))
|
|
|
|
|
|
def main(commands):
|
|
try:
|
|
optlist, args = getopt.getopt(sys.argv[1:], 'edEDh')
|
|
sys.argv = [sys.argv[0]] + args
|
|
except getopt.GetoptError as err:
|
|
print(err)
|
|
sys.exit(1)
|
|
|
|
action = quote
|
|
for flag, _ in optlist:
|
|
if flag in commands:
|
|
action = commands[flag]
|
|
elif flag == '-h':
|
|
usage()
|
|
sys.exit()
|
|
else:
|
|
assert False, "unhandled option"
|
|
|
|
print(action("\n".join([line for line in fileinput.input()]).strip()))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main({
|
|
"-e": quote,
|
|
"-E": quote_plus,
|
|
"-d": unquote,
|
|
"-D": unquote_plus
|
|
})
|