view python/hexify.py @ 767:35f8751c0930

it is very annoying to have ones overrides overridden; see also http://stackoverflow.com/questions/25381304/why-type-cd-on-mac-os-states-that-cd-is-a-function
author Jeff Hammel <k0scist@gmail.com>
date Thu, 28 Jan 2016 14:02:17 -0800
parents ffb75d832afe
children
line wrap: on
line source

#!/usr/bin/env python

import sys

def hexify(string, excludes=('/',)):
    return ''.join([i if i in excludes else ('%'+ hex(ord(i))[-2:])
                    for i in string])

def hidden_url(string):
    if '://' in string:
        scheme, rest = string.split('://', 1)
        if '/' in rest:
            loc, rest = rest.split('/', 1)
            return '{}://{}/{}'.format(scheme, loc, hexify(rest, excludes=('/',)))
        else:
            return string
    else:
        return hexify(string, excludes=('/',))

def main(args=sys.argv[1:]):
    string = ' '.join(args)
    print hidden_url(string)

if __name__ == '__main__':
    main()