2017-07-03 09:13:08 -07:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2017-10-03 10:05:00 -07:00
|
|
|
"""
|
|
|
|
Encode/decode url strings in stdin or the given file(s).
|
|
|
|
"""
|
2017-07-03 09:13:08 -07:00
|
|
|
|
|
|
|
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():
|
2017-10-03 10:05:00 -07:00
|
|
|
"""Print out help for this script"""
|
2017-07-03 09:13:08 -07:00
|
|
|
print("Usage: {} [-edEDh] file1[ file2[ ...]]".format(os.path.basename(sys.argv[0])))
|
|
|
|
|
|
|
|
|
2017-10-03 10:05:00 -07:00
|
|
|
def main(commands):
|
2017-07-03 09:13:08 -07:00
|
|
|
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)
|
|
|
|
|
2017-10-03 10:05:00 -07:00
|
|
|
action = quote
|
|
|
|
for flag, _ in optlist:
|
|
|
|
if flag in commands:
|
|
|
|
action = commands[flag]
|
|
|
|
elif flag == '-h':
|
2017-07-03 09:13:08 -07:00
|
|
|
usage()
|
|
|
|
sys.exit()
|
|
|
|
else:
|
|
|
|
assert False, "unhandled option"
|
|
|
|
|
2017-10-03 10:05:00 -07:00
|
|
|
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
|
|
|
|
})
|