<s> import re <EOL> import json <EOL> from collections import OrderedDict <EOL> _register = OrderedDict ( ) <EOL> def register ( name , ttf_fname , fontd_fname ) : <EOL> """<STR_LIT>""" <EOL> with open ( fontd_fname , '<STR_LIT:r>' ) as f : <EOL> fontd = json . loads ( f . read ( ) ) <EOL> _register [ name ] = ttf_fname , fontd_fname , fontd <EOL> def icon ( code , size = None , color = None , font_name = None ) : <EOL> """<STR_LIT>""" <EOL> font = _register . keys ( ) [ <NUM_LIT:0> ] if font_name is None else font_name <EOL> font_data = _register [ font ] <EOL> s = "<STR_LIT>" % ( font_data [ <NUM_LIT:0> ] , unichr ( font_data [ <NUM_LIT:2> ] [ code ] ) ) <EOL> if size is not None : <EOL> s = "<STR_LIT>" % ( size , s ) <EOL> if color is not None : <EOL> s = "<STR_LIT>" % ( color , s ) <EOL> return s <EOL> def create_fontdict_file ( css_fname , output_fname ) : <EOL> """<STR_LIT>""" <EOL> with open ( css_fname , '<STR_LIT:r>' ) as f : <EOL> data = f . read ( ) <EOL> res = _parse ( data ) <EOL> with open ( output_fname , '<STR_LIT:w>' ) as o : <EOL> o . write ( json . dumps ( res ) ) <EOL> return res <EOL> def _parse ( data ) : <EOL> pat_start = re . compile ( '<STR_LIT>' , re . DOTALL ) <EOL> start = [ x for x in re . finditer ( pat_start , data ) ] [ <NUM_LIT:0> ] . start ( ) <EOL> data = data [ start : ] <EOL> data = data . replace ( "<STR_LIT:\\>" , '<STR_LIT>' ) <EOL> pat_keys = re . compile ( '<STR_LIT>' ) <EOL> keys = [ ] <EOL> lines = [ ] <EOL> for i in re . finditer ( pat_keys , data ) : <EOL> lineno = data . count ( "<STR_LIT:\n>" , <NUM_LIT:0> , i . start ( ) ) + <NUM_LIT:1> <EOL> lines . append ( lineno ) <EOL> keys . append ( i . group ( ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> values = [ ] <EOL> QUOTE = '<STR_LIT:">' <EOL> data = data . replace ( "<STR_LIT:'>" , '<STR_LIT:">' ) <EOL> pat_values = re . compile ( '<STR_LIT>' ) <EOL> for i in re . finditer ( pat_values , data ) : <EOL> lineno = data . count ( "<STR_LIT:\n>" , <NUM_LIT:0> , i . start ( ) ) + <NUM_LIT:1> <EOL> v = i . group ( ) . split ( QUOTE ) [ <NUM_LIT:1> ] <EOL> v = v . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> for j , lnum in enumerate ( lines ) : <EOL> if lnum <= lineno : <EOL> try : <EOL> val = int ( v . replace ( '<STR_LIT>' , '<STR_LIT>' ) , <NUM_LIT:0> ) <EOL> except : <EOL> val = <NUM_LIT:0> <EOL> values . append ( val ) <EOL> else : <EOL> break <EOL> [ lines . pop ( <NUM_LIT:0> ) for x in lines [ <NUM_LIT:0> : j ] ] <EOL> res = dict ( zip ( keys , values ) ) <EOL> return res </s>
