""" Export .NET encodings as dict marshal Copyright (c) 2009, MAL """ import sys, codecs, marshal import System import System.Text _debug = 0 CHECK_CODECS = [ # (.NET Code page, Python codec name) (65000, 'utf-7'), (65001, 'utf-8'), #(65005, 'utf-32-le'), # not supported by .NET 2.0 #(65006, 'utf-32-be'), # not supported by .NET 2.0 (1200, 'utf-16-le'), (1201, 'utf-16-be'), (28591, 'iso-8859-1'), (28592, 'iso-8859-2'), # not supported by IronPython 2.6 (28593, 'iso-8859-3'), # not supported by IronPython 2.6 (28594, 'iso-8859-4'), # not supported by IronPython 2.6 (28595, 'iso-8859-5'), # not supported by IronPython 2.6 ] UNICODE_RANGE = sys.maxunicode def encoding_map(code_page, encoding): net_codec = System.Text.Encoding.GetEncoding(code_page) map = {} for i in range(UNICODE_RANGE): u = unichr(i) net_bytes = [int(c) for c in net_codec.GetBytes(u)] map[i] = net_bytes return map for code_page, encoding in CHECK_CODECS: mapfile = encoding + '.map' print 'Exporting code Page %i as encoding %r using file %r' % ( code_page, encoding, mapfile) d = encoding_map(code_page, encoding) data = (code_page, encoding, d) open(mapfile, 'wb').write(marshal.dumps(data))