Rename urlencode => url & rewrite
This commit is contained in:
parent
2d7f36d18a
commit
2733333619
58
bin/url
Executable file
58
bin/url
Executable file
|
@ -0,0 +1,58 @@
|
|||
#!/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 encode(text, plus=False):
|
||||
return quote(text.strip())
|
||||
|
||||
def encode_plus(text):
|
||||
return quote_plus(text.strip())
|
||||
|
||||
def decode(text):
|
||||
return unquote(text.strip())
|
||||
|
||||
def decode_plus(text):
|
||||
return unquote_plus(text.strip())
|
||||
|
||||
def usage():
|
||||
# TODO Improve this
|
||||
print("Usage: {} [-edEDh] file1[ file2[ ...]]".format(os.path.basename(sys.argv[0])))
|
||||
|
||||
#
|
||||
if __name__ == '__main__':
|
||||
COMMANDS = {
|
||||
"-e": encode,
|
||||
"-E": encode_plus,
|
||||
"-d": decode,
|
||||
"-D": decode_plus
|
||||
}
|
||||
|
||||
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 = encode
|
||||
for o, a in optlist:
|
||||
if o in COMMANDS:
|
||||
action = COMMANDS[o]
|
||||
elif o == '-h':
|
||||
usage()
|
||||
sys.exit()
|
||||
else:
|
||||
assert False, "unhandled option"
|
||||
|
||||
input = "\n".join([line for line in fileinput.input()])
|
||||
print(action(input))
|
|
@ -1,12 +0,0 @@
|
|||
#!/usr/bin/env python
|
||||
import sys
|
||||
try:
|
||||
from urllib.parse import quote_plus
|
||||
except ImportError:
|
||||
from urllib import quote_plus
|
||||
|
||||
if sys.argv[1:]:
|
||||
text = " ".join(sys.argv[1:])
|
||||
else:
|
||||
text = "".join(sys.stdin.readlines())
|
||||
print(quote_plus(text.strip()))
|
Loading…
Reference in a new issue