vscripts/punycode.py

54 lines
1.4 KiB
Python
Raw Normal View History

2015-05-25 15:55:40 +03:00
#!/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():
2015-05-25 15:55:40 +03:00
"""Parse and store arguments."""
desc = 'A simple punycode to unicode and back converter.'
parser = argparse.ArgumentParser(description=desc)
2015-05-25 15:55:40 +03:00
parser.add_argument('domain', help='domain name to convert')
2015-05-25 15:55:40 +03:00
# Store the supplied args
return parser.parse_args()
2015-05-25 15:55:40 +03:00
def main(main_sysargs):
args = parse_arguments()
2015-05-25 15:55:40 +03:00
domain = Domain(args.domain)
domain.display()
if __name__ == '__main__':
main(sys.argv)