<s> """<STR_LIT>""" <EOL> import argparse <EOL> import collections <EOL> import glob <EOL> import json <EOL> import os <EOL> from os import path <EOL> import sys <EOL> from nototools import emoji_ordering <EOL> from nototools import tool_utils <EOL> from nototools import unicode_data <EOL> def _is_ascii_digit ( cp ) : <EOL> return ord ( '<STR_LIT:0>' ) <= cp <= ord ( '<STR_LIT>' ) <EOL> def _is_ascii_uppercase ( cp ) : <EOL> return ord ( '<STR_LIT:A>' ) <= cp <= ord ( '<STR_LIT>' ) <EOL> _NAME_FIXES = { <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> '<STR_LIT>' : "<STR_LIT>" <EOL> } <EOL> def _unicode_name ( cp ) : <EOL> name = unicode_data . name ( cp ) . title ( ) <EOL> for k , v in _NAME_FIXES . iteritems ( ) : <EOL> name = name . replace ( k , v ) <EOL> return name <EOL> _REGIONAL_INDICATOR_START = <NUM_LIT> <EOL> _REGIONAL_INDICATOR_END = <NUM_LIT> <EOL> def _is_regional_indicator ( cp ) : <EOL> return _REGIONAL_INDICATOR_START <= cp <= _REGIONAL_INDICATOR_END <EOL> def _is_flag_sequence ( cps ) : <EOL> return len ( cps ) == <NUM_LIT:2> and all ( _is_regional_indicator ( cp ) for cp in cps ) <EOL> def _flag_sequence_name ( cps ) : <EOL> return '<STR_LIT>' . join ( <EOL> unichr ( cp - _REGIONAL_INDICATOR_START + ord ( '<STR_LIT:A>' ) ) for cp in cps ) <EOL> _FITZ_START = <NUM_LIT> <EOL> _FITZ_END = <NUM_LIT> <EOL> def _is_fitzpatrick ( cp ) : <EOL> return _FITZ_START <= cp <= _FITZ_END <EOL> def _is_fitz_sequence ( cps ) : <EOL> return len ( cps ) == <NUM_LIT:2> and _is_fitzpatrick ( cps [ <NUM_LIT:1> ] ) <EOL> _FITZ_NAMES = { <EOL> _FITZ_START : '<STR_LIT>' , <EOL> _FITZ_START + <NUM_LIT:1> : '<STR_LIT:3>' , <EOL> _FITZ_START + <NUM_LIT:2> : '<STR_LIT:4>' , <EOL> _FITZ_START + <NUM_LIT:3> : '<STR_LIT:5>' , <EOL> _FITZ_START + <NUM_LIT:4> : '<STR_LIT>' <EOL> } <EOL> def _fitz_sequence_name ( cps ) : <EOL> return _unicode_name ( cps [ <NUM_LIT:0> ] ) <EOL> def _is_keycap_sequence ( cps ) : <EOL> return len ( cps ) == <NUM_LIT:2> and cps [ <NUM_LIT:1> ] == <NUM_LIT> <EOL> _KEYCAP_NAMES = { cp : unicode_data . name ( cp ) [ <NUM_LIT:6> : ] for cp in range ( <NUM_LIT> , <NUM_LIT> + <NUM_LIT:10> ) } <EOL> def _keycap_sequence_name ( cps ) : <EOL> return '<STR_LIT>' + _KEYCAP_NAMES . get ( cps [ <NUM_LIT:0> ] , unicode_data . name ( cps [ <NUM_LIT:0> ] ) ) <EOL> def _create_extra_sequence_names ( ) : <EOL> BOY = <NUM_LIT> <EOL> GIRL = <NUM_LIT> <EOL> MAN = <NUM_LIT> <EOL> WOMAN = <NUM_LIT> <EOL> HEART = <NUM_LIT> <EOL> KISS_MARK = <NUM_LIT> <EOL> EYE = <NUM_LIT> <EOL> SPEECH = <NUM_LIT> <EOL> return { <EOL> ( MAN , HEART , KISS_MARK , MAN ) : '<STR_LIT>' , <EOL> ( WOMAN , HEART , KISS_MARK , WOMAN ) : '<STR_LIT>' , <EOL> ( WOMAN , HEART , KISS_MARK , MAN ) : '<STR_LIT>' , <EOL> ( WOMAN , HEART , MAN ) : '<STR_LIT>' , <EOL> ( MAN , HEART , MAN ) : '<STR_LIT>' , <EOL> ( WOMAN , HEART , WOMAN ) : '<STR_LIT>' , <EOL> ( MAN , WOMAN , GIRL ) : '<STR_LIT>' , <EOL> ( MAN , WOMAN , GIRL , GIRL ) : '<STR_LIT>' , <EOL> ( MAN , WOMAN , GIRL , BOY ) : '<STR_LIT>' , <EOL> ( MAN , WOMAN , BOY ) : '<STR_LIT>' , <EOL> ( MAN , WOMAN , BOY , BOY ) : '<STR_LIT>' , <EOL> ( MAN , MAN , GIRL ) : '<STR_LIT>' , <EOL> ( MAN , MAN , GIRL , GIRL ) : '<STR_LIT>' , <EOL> ( MAN , MAN , GIRL , BOY ) : '<STR_LIT>' , <EOL> ( MAN , MAN , BOY ) : '<STR_LIT>' , <EOL> ( MAN , MAN , BOY , BOY ) : '<STR_LIT>' , <EOL> ( WOMAN , WOMAN , GIRL ) : '<STR_LIT>' , <EOL> ( WOMAN , WOMAN , GIRL , GIRL ) : '<STR_LIT>' , <EOL> ( WOMAN , WOMAN , GIRL , BOY ) : '<STR_LIT>' , <EOL> ( WOMAN , WOMAN , BOY ) : '<STR_LIT>' , <EOL> ( WOMAN , WOMAN , BOY , BOY ) : '<STR_LIT>' , <EOL> ( EYE , SPEECH ) : '<STR_LIT>' , <EOL> } <EOL> _EXTRA_SEQUENCE_NAMES = _create_extra_sequence_names ( ) <EOL> def _extra_sequence_name ( cps ) : <EOL> trim_cps = tuple ( [ cp for cp in cps if cp not in [ <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> return _EXTRA_SEQUENCE_NAMES . get ( trim_cps , '<STR_LIT>' ) <EOL> def _sequence_name ( cps ) : <EOL> if len ( cps ) == <NUM_LIT:1> : <EOL> name = _unicode_name ( cps [ <NUM_LIT:0> ] ) <EOL> elif _is_flag_sequence ( cps ) : <EOL> name = _flag_sequence_name ( cps ) <EOL> elif _is_fitz_sequence ( cps ) : <EOL> name = _fitz_sequence_name ( cps ) <EOL> elif _is_keycap_sequence ( cps ) : <EOL> name = _keycap_sequence_name ( cps ) <EOL> else : <EOL> name = _extra_sequence_name ( cps ) <EOL> if not name : <EOL> name = '<STR_LIT:+>' . join ( _unicode_name ( cp ) for cp in cps ) <EOL> return name <EOL> def _keep_sequence ( cps ) : <EOL> if len ( cps ) > <NUM_LIT:1> : <EOL> return True <EOL> cp = cps [ <NUM_LIT:0> ] <EOL> if ( unicode_data . is_private_use ( cp ) or <EOL> unicode_data . category ( cp ) [ <NUM_LIT:0> ] not in [ '<STR_LIT:L>' , '<STR_LIT:P>' , '<STR_LIT:S>' ] or <EOL> _is_regional_indicator ( cp ) or <EOL> _is_ascii_digit ( cp ) or <EOL> cp == ord ( '<STR_LIT:#>' ) or <EOL> cp == ord ( '<STR_LIT:*>' ) ) : <EOL> return False <EOL> return True <EOL> _FLAG_ALIASES = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def _flag_name_to_estr ( flag_name ) : <EOL> return '<STR_LIT>' . join ( unichr ( <EOL> _REGIONAL_INDICATOR_START + ord ( cp ) - ord ( '<STR_LIT:A>' ) ) for cp in flag_name ) <EOL> def _add_flag_aliases ( estr_to_file , added , skipped , missing ) : <EOL> for src_name , dst_name in _FLAG_ALIASES . items ( ) : <EOL> src_estr = _flag_name_to_estr ( src_name ) <EOL> src_cps = [ ord ( cp ) for cp in src_estr ] <EOL> dst_estr = _flag_name_to_estr ( dst_name ) <EOL> if src_estr in estr_to_file : <EOL> skipped . append ( src_cps ) <EOL> continue <EOL> fname = estr_to_file . get ( dst_estr ) <EOL> if not fname : <EOL> missing . append ( src_cps ) <EOL> continue <EOL> added . append ( src_cps ) <EOL> estr_to_file [ src_estr ] = fname <EOL> def _report_info ( title , cps_list ) : <EOL> if not cps_list : <EOL> return <EOL> print '<STR_LIT>' % ( title , len ( cps_list ) ) <EOL> for cps in sorted ( cps_list ) : <EOL> print '<STR_LIT>' % ( <EOL> '<STR_LIT:_>' . join ( '<STR_LIT>' % cp for cp in cps ) , <EOL> '<STR_LIT:U+002C>' . join ( unicode_data . name ( cp , '<STR_LIT>' ) for cp in cps ) ) <EOL> def _estr_to_file ( srcdir , eo , verbose = False ) : <EOL> """<STR_LIT>""" <EOL> added = [ ] <EOL> skipped = [ ] <EOL> missing = [ ] <EOL> result = { } <EOL> emoji_strings = eo . emoji_strings ( ) <EOL> for f in sorted ( glob . glob ( path . join ( srcdir , '<STR_LIT>' ) ) ) : <EOL> fname = path . basename ( f ) <EOL> parts = fname [ <NUM_LIT:7> : - <NUM_LIT:4> ] . split ( '<STR_LIT:_>' ) <EOL> cps = [ int ( part , <NUM_LIT:16> ) for part in parts ] <EOL> if not _keep_sequence ( cps ) : <EOL> skipped . append ( cps ) <EOL> continue <EOL> estr = u'<STR_LIT>' . join ( unichr ( cp ) for cp in cps ) <EOL> if estr not in emoji_strings : <EOL> missing . append ( cps ) <EOL> continue <EOL> result [ estr ] = f <EOL> _add_flag_aliases ( result , added , skipped , missing ) <EOL> if verbose : <EOL> _report_info ( '<STR_LIT>' , added ) <EOL> _report_info ( '<STR_LIT>' , skipped ) <EOL> _report_info ( '<STR_LIT>' , missing ) <EOL> assert not missing <EOL> return result <EOL> def _name_data ( estr , estr_to_file ) : <EOL> cps = tuple ( [ ord ( cp ) for cp in estr ] ) <EOL> name = _sequence_name ( cps ) <EOL> sequence = '<STR_LIT>' . join ( '<STR_LIT>' % cp for cp in cps ) <EOL> try : <EOL> fname = path . basename ( estr_to_file [ estr ] ) <EOL> except KeyError as e : <EOL> print '<STR_LIT:error>' , e <EOL> print '<STR_LIT>' , estr , '<STR_LIT:_>' . join ( '<STR_LIT>' % ord ( cp ) for cp in estr ) <EOL> print '<STR_LIT:\n>' . join ( '<STR_LIT>' % ( estr , '<STR_LIT:_>' . join ( '<STR_LIT>' % ord ( cp ) for cp in estr ) ) <EOL> for estr in estr_to_file . keys ( ) ) <EOL> raise e <EOL> return fname , sequence , name <EOL> def generate_names ( <EOL> srcdir , outfile , groupfile , force = False , pretty_print = False , verbose = False ) : <EOL> if not path . isdir ( srcdir ) : <EOL> print >> sys . stderr , '<STR_LIT>' % srcdir <EOL> return <EOL> parent = tool_utils . ensure_dir_exists ( path . dirname ( outfile ) ) <EOL> if path . exists ( outfile ) : <EOL> if not force : <EOL> print >> sys . stderr , '<STR_LIT>' % outfile <EOL> return <EOL> if not path . isfile ( outfile ) : <EOL> print >> sys . stderr , '<STR_LIT>' % outfile <EOL> return <EOL> eo = emoji_ordering . from_file ( groupfile ) <EOL> estr_to_file = _estr_to_file ( srcdir , eo , verbose ) <EOL> skipped = collections . defaultdict ( list ) <EOL> data = [ ] <EOL> for category in eo . category_names ( ) : <EOL> name_data = [ ] <EOL> for estr in eo . emoji_in_category ( category ) : <EOL> if not estr in estr_to_file : <EOL> skipped [ category ] . append ( estr ) <EOL> else : <EOL> name_data . append ( _name_data ( estr , estr_to_file ) ) <EOL> data . append ( { '<STR_LIT>' : category , '<STR_LIT>' : name_data } ) <EOL> if verbose and skipped : <EOL> total = <NUM_LIT:0> <EOL> print '<STR_LIT>' <EOL> for category in eo . category_names ( ) : <EOL> estrs = skipped . get ( category ) <EOL> if not estrs : <EOL> continue <EOL> count = len ( estrs ) <EOL> total += count <EOL> print '<STR_LIT>' % ( category , count ) <EOL> cps_list = [ [ ord ( cp ) for cp in estr ] <EOL> for estr in estrs ] <EOL> _report_info ( '<STR_LIT:U+0020>' , cps_list ) <EOL> print '<STR_LIT>' % total <EOL> with open ( outfile , '<STR_LIT:w>' ) as f : <EOL> indent = <NUM_LIT:2> if pretty_print else None <EOL> separators = None if pretty_print else ( '<STR_LIT:U+002C>' , '<STR_LIT::>' ) <EOL> json . dump ( data , f , indent = indent , separators = separators ) <EOL> print '<STR_LIT>' % outfile <EOL> def main ( ) : <EOL> DEFAULT_OUTFILE = '<STR_LIT>' <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , required = True ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' % <EOL> DEFAULT_OUTFILE , metavar = '<STR_LIT>' , default = DEFAULT_OUTFILE ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> metavar = '<STR_LIT>' , required = True ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' ) <EOL> args = parser . parse_args ( ) <EOL> generate_names ( <EOL> args . srcdir , args . outfile , args . emoji_ordering , force = args . force , <EOL> pretty_print = args . pretty_print , verbose = args . verbose ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
