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
|
|
|
|
2017-12-05 22:39:10 -08:00
|
|
|
import argparse
|
2017-07-03 09:13:08 -07:00
|
|
|
import fileinput
|
|
|
|
try:
|
|
|
|
from urllib.parse import quote_plus, quote, unquote_plus, unquote
|
|
|
|
except ImportError:
|
|
|
|
from urllib import quote_plus, quote, unquote_plus, unquote
|
|
|
|
|
|
|
|
|
2017-12-05 22:39:10 -08:00
|
|
|
def main():
|
|
|
|
opts = argparse.ArgumentParser(description="Make text URI-safe by encoding/decoding input")
|
|
|
|
opts.add_argument('file', type=str, nargs='*', help='File(s) to encode/decode')
|
|
|
|
opts.add_argument('-e', '--encode', action='store_true', help='Use encoder')
|
|
|
|
opts.add_argument('-E', '--encode-plus', action='store_true', help='Use encoder, but encode spaces as pluses')
|
|
|
|
opts.add_argument('-d', '--decode', action='store_true', help='Use decoder')
|
|
|
|
opts.add_argument('-D', '--decode-plus', action='store_true', help='Use decoder, but decode pluses as spaces')
|
|
|
|
args = opts.parse_args()
|
2017-07-03 09:13:08 -07:00
|
|
|
|
2017-10-03 10:05:00 -07:00
|
|
|
action = quote
|
2017-12-05 22:39:10 -08:00
|
|
|
if args.decode:
|
|
|
|
action = unquote
|
|
|
|
elif args.decode_plus:
|
|
|
|
action = unquote_plus
|
|
|
|
elif args.encode_plus:
|
|
|
|
action = quote_plus
|
2017-07-03 09:13:08 -07:00
|
|
|
|
2017-12-05 22:39:10 -08:00
|
|
|
text = [line for line in fileinput.input(args.file)]
|
|
|
|
print(action("\n".join(text).strip()))
|
2017-10-03 10:05:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2017-12-05 22:39:10 -08:00
|
|
|
main()
|