annotate python/hexify.py @ 673:1b8ea91f00a0

STUB: python/epoch2date.py
author Jeff Hammel <k0scist@gmail.com>
date Tue, 06 May 2014 14:26:05 -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()