annotate python/hexify.py @ 718:cd9d65e6e2ab

monkeypatch example
author Jeff Hammel <k0scist@gmail.com>
date Thu, 30 Oct 2014 15:22:29 -0700
parents ffb75d832afe
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
664
ffb75d832afe make sneaky urls
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
2
0
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
3 import sys
664
ffb75d832afe make sneaky urls
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
4
ffb75d832afe make sneaky urls
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
5 def hexify(string, excludes=('/',)):
ffb75d832afe make sneaky urls
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
6 return ''.join([i if i in excludes else ('%'+ hex(ord(i))[-2:])
ffb75d832afe make sneaky urls
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
7 for i in string])
ffb75d832afe make sneaky urls
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
8
ffb75d832afe make sneaky urls
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
9 def hidden_url(string):
ffb75d832afe make sneaky urls
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
10 if '://' in string:
ffb75d832afe make sneaky urls
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
11 scheme, rest = string.split('://', 1)
ffb75d832afe make sneaky urls
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
12 if '/' in rest:
ffb75d832afe make sneaky urls
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
13 loc, rest = rest.split('/', 1)
ffb75d832afe make sneaky urls
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
14 return '{}://{}/{}'.format(scheme, loc, hexify(rest, excludes=('/',)))
ffb75d832afe make sneaky urls
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
15 else:
ffb75d832afe make sneaky urls
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
16 return string
ffb75d832afe make sneaky urls
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
17 else:
ffb75d832afe make sneaky urls
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
18 return hexify(string, excludes=('/',))
ffb75d832afe make sneaky urls
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
19
ffb75d832afe make sneaky urls
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
20 def main(args=sys.argv[1:]):
ffb75d832afe make sneaky urls
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
21 string = ' '.join(args)
ffb75d832afe make sneaky urls
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
22 print hidden_url(string)
ffb75d832afe make sneaky urls
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
23
ffb75d832afe make sneaky urls
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
24 if __name__ == '__main__':
ffb75d832afe make sneaky urls
Jeff Hammel <k0scist@gmail.com>
parents: 0
diff changeset
25 main()