vscripts/punycode.py

53 lines
1.4 KiB
Python
Executable file

#!/usr/bin/env python
import argparse
import sys
class Domain(object):
def __init__(self, domain):
"""Constructor"""
if sys.version_info[0] < 3:
domain = domain.decode('utf-8').encode('utf-8')
idna = domain.decode('utf-8').encode('idna')
self.ascii = idna
self.utf_8 = idna.decode('idna').encode('utf-8')
else:
idna = bytes(domain, 'idna')
self.ascii = idna.decode('ascii')
self.utf_8 = idna.decode('idna')
if self.ascii == self.utf_8:
self.type = 'ascii'
self.utf_8 = None
else:
if self.utf_8 == domain:
self.type = 'unicode'
else:
self.type = 'punycode'
def display(self):
print('INPUT: \x1B[1m{}\x1B[0m\nASCII: \x1B[1m{}\x1B[0m'
.format(self.type, self.ascii))
if self.utf_8 is not None:
print('UTF-8: \x1B[1m{}\x1B[0m'.format(self.utf_8))
def parse_arguments():
"""Parse and store arguments."""
desc = 'A simple punycode to unicode and back converter.'
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('domain', help='domain name to convert')
# Store the supplied args
return parser.parse_args()
def main(main_sysargs):
args = parse_arguments()
domain = Domain(args.domain)
domain.display()
if __name__ == '__main__':
main(sys.argv)