<s> """<STR_LIT>""" <EOL> from collections import defaultdict <EOL> from itertools import izip <EOL> import re <EOL> default_charset = """<STR_LIT>""" <EOL> _dewhite = re . compile ( r"<STR_LIT>" ) <EOL> _char = r"<STR_LIT>" <EOL> _char_map = re . compile ( "<STR_LIT>" + _char + "<STR_LIT>" + _char + "<STR_LIT:$>" ) <EOL> _range_map = re . compile ( "<STR_LIT>" + _char + r"<STR_LIT>" + _char + "<STR_LIT>" + _char + "<STR_LIT:..>" + _char + "<STR_LIT:$>" ) <EOL> _stray_char = re . compile ( "<STR_LIT>" + _char + "<STR_LIT:$>" ) <EOL> _stray_range = re . compile ( "<STR_LIT>" + _char + r"<STR_LIT>" + _char + "<STR_LIT:$>" ) <EOL> _checker_range = re . compile ( "<STR_LIT>" + _char + r"<STR_LIT>" + _char + "<STR_LIT>" ) <EOL> def charspec_to_int ( string ) : <EOL> if string . startswith ( "<STR_LIT>" ) : <EOL> return int ( string [ <NUM_LIT:2> : ] , <NUM_LIT:16> ) <EOL> elif len ( string ) == <NUM_LIT:1> : <EOL> return ord ( string ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % string ) <EOL> def charset_table_to_dict ( tablestring ) : <EOL> """<STR_LIT>""" <EOL> map = defaultdict ( lambda : None ) <EOL> for line in tablestring . split ( "<STR_LIT:\n>" ) : <EOL> if not line or line . startswith ( "<STR_LIT:#>" ) : continue <EOL> line = _dewhite . sub ( "<STR_LIT>" , line ) <EOL> for item in line . split ( "<STR_LIT:U+002C>" ) : <EOL> if not item : continue <EOL> match = _range_map . match ( item ) <EOL> if match : <EOL> start1 = charspec_to_int ( match . group ( <NUM_LIT:1> ) ) <EOL> end1 = charspec_to_int ( match . group ( <NUM_LIT:2> ) ) <EOL> start2 = charspec_to_int ( match . group ( <NUM_LIT:3> ) ) <EOL> end2 = charspec_to_int ( match . group ( <NUM_LIT:4> ) ) <EOL> assert ( end1 - start1 ) == ( end2 - start2 ) <EOL> try : <EOL> for fromord , tooord in izip ( xrange ( start1 , end1 + <NUM_LIT:1> ) , xrange ( start2 , end2 + <NUM_LIT:1> ) ) : <EOL> map [ fromord ] = unichr ( tooord ) <EOL> except ValueError : <EOL> pass <EOL> continue <EOL> match = _char_map . match ( item ) <EOL> if match : <EOL> fromord = charspec_to_int ( match . group ( <NUM_LIT:1> ) ) <EOL> toord = charspec_to_int ( match . group ( <NUM_LIT:2> ) ) <EOL> try : <EOL> map [ fromord ] = unichr ( toord ) <EOL> except ValueError : <EOL> pass <EOL> continue <EOL> match = _stray_char . match ( item ) <EOL> if match : <EOL> ord = charspec_to_int ( match . group ( <NUM_LIT:0> ) ) <EOL> try : <EOL> map [ ord ] = unichr ( ord ) <EOL> except ValueError : <EOL> pass <EOL> continue <EOL> match = _stray_range . match ( item ) <EOL> if match : <EOL> start = charspec_to_int ( match . group ( <NUM_LIT:1> ) ) <EOL> end = charspec_to_int ( match . group ( <NUM_LIT:2> ) ) <EOL> try : <EOL> for ord in xrange ( start , end + <NUM_LIT:1> ) : <EOL> map [ ord ] = unichr ( ord ) <EOL> except ValueError : <EOL> pass <EOL> continue <EOL> match = _checker_range . match ( item ) <EOL> if match : <EOL> fromord = charspec_to_int ( match . group ( <NUM_LIT:1> ) ) <EOL> toord = charspec_to_int ( match . group ( <NUM_LIT:2> ) ) <EOL> assert toord - fromord % <NUM_LIT:2> == <NUM_LIT:0> <EOL> for ord in xrange ( fromord , toord + <NUM_LIT:1> , <NUM_LIT:2> ) : <EOL> try : <EOL> map [ ord ] = unichr ( ord + <NUM_LIT:1> ) <EOL> map [ ord + <NUM_LIT:1> ] = unichr ( ord + <NUM_LIT:1> ) <EOL> except ValueError : <EOL> pass <EOL> continue <EOL> raise Exception ( "<STR_LIT>" % item ) <EOL> return map </s>
