<s> import re <EOL> def generate ( modname ) : <EOL> import sys <EOL> import functools <EOL> mod = sys . modules [ modname ] <EOL> def add_attr ( k , v ) : <EOL> setattr ( mod , k , v ) <EOL> add_attr ( '<STR_LIT>' , <EOL> functools . partial ( _msg_type_to_str , mod ) ) <EOL> add_attr ( '<STR_LIT>' , <EOL> functools . partial ( _error_type_to_str , mod ) ) <EOL> add_attr ( '<STR_LIT>' , <EOL> functools . partial ( _error_code_to_str , mod ) ) <EOL> add_attr ( '<STR_LIT>' , <EOL> functools . partial ( _error_to_jsondict , mod ) ) <EOL> def _get_value_name ( mod , value , pattern ) : <EOL> for k , v in mod . __dict__ . items ( ) : <EOL> if k . startswith ( pattern ) : <EOL> if v == value : <EOL> return k <EOL> return '<STR_LIT>' <EOL> def _msg_type_to_str ( mod , type_ ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( _get_value_name ( mod , type_ , '<STR_LIT>' ) , type_ ) <EOL> def _error_type_to_str ( mod , type_ ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( _get_value_name ( mod , type_ , '<STR_LIT>' ) , type_ ) <EOL> def _get_error_names ( mod , type_ , code ) : <EOL> t_name = _get_value_name ( mod , type_ , '<STR_LIT>' ) <EOL> if t_name == '<STR_LIT>' : <EOL> return '<STR_LIT>' , '<STR_LIT>' <EOL> if t_name == '<STR_LIT>' : <EOL> c_name_p = '<STR_LIT>' <EOL> else : <EOL> c_name_p = '<STR_LIT>' <EOL> for m in re . findall ( "<STR_LIT>" , t_name ) : <EOL> c_name_p += m . upper ( ) <EOL> c_name_p += '<STR_LIT>' <EOL> c_name = _get_value_name ( mod , code , c_name_p ) <EOL> return t_name , c_name <EOL> def _error_code_to_str ( mod , type_ , code ) : <EOL> """<STR_LIT>""" <EOL> ( _ , c_name ) = _get_error_names ( mod , type_ , code ) <EOL> return '<STR_LIT>' % ( c_name , code ) <EOL> def _error_to_jsondict ( mod , type_ , code ) : <EOL> """<STR_LIT>""" <EOL> ( t_name , c_name ) = _get_error_names ( mod , type_ , code ) <EOL> return { '<STR_LIT:type>' : '<STR_LIT>' % ( t_name , type_ ) , <EOL> '<STR_LIT:code>' : '<STR_LIT>' % ( c_name , code ) } </s>
