<s> from collections import namedtuple <EOL> import datetime <EOL> import os <EOL> import sys <EOL> import xlrd <EOL> def banner_str ( s ) : <EOL> return ( "<STR_LIT>" % s ) . center ( BANNER_PADDING , '<STR_LIT:#>' ) <EOL> BANNER_PADDING = <NUM_LIT> <EOL> PROFILE_OUTPUT_FILE_HEADER_MAGIC = '<STR_LIT>' % ( <EOL> '<STR_LIT:#>' * BANNER_PADDING , <EOL> banner_str ( '<STR_LIT>' ) , <EOL> '<STR_LIT:#>' * BANNER_PADDING , <EOL> ) <EOL> PROFILE_OUTPUT_FILE_HEADER_FMT = '''<STR_LIT>''' % PROFILE_OUTPUT_FILE_HEADER_MAGIC <EOL> Field = namedtuple ( '<STR_LIT>' , ( '<STR_LIT:name>' , '<STR_LIT:type>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> DynamicField = namedtuple ( '<STR_LIT>' , ( '<STR_LIT:name>' , '<STR_LIT:type>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> Type = namedtuple ( '<STR_LIT>' , ( '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> TypeValue = namedtuple ( '<STR_LIT>' , ( '<STR_LIT:name>' , '<STR_LIT:value>' ) ) <EOL> SpecialFunctionType = namedtuple ( '<STR_LIT>' , ( '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> FIELD_BASE_TYPES = { <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:3> , <EOL> '<STR_LIT>' : <NUM_LIT:4> , <EOL> '<STR_LIT>' : <NUM_LIT:5> , <EOL> '<STR_LIT>' : <NUM_LIT:6> , <EOL> '<STR_LIT:string>' : <NUM_LIT:7> , <EOL> '<STR_LIT>' : <NUM_LIT:8> , <EOL> '<STR_LIT>' : <NUM_LIT:9> , <EOL> '<STR_LIT>' : <NUM_LIT:10> , <EOL> '<STR_LIT>' : <NUM_LIT:11> , <EOL> '<STR_LIT>' : <NUM_LIT:12> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } <EOL> SPECIAL_TYPES = { <EOL> '<STR_LIT:bool>' : SpecialFunctionType ( '<STR_LIT:bool>' , '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : SpecialFunctionType ( '<STR_LIT>' , '<STR_LIT>' , None ) , <EOL> } <EOL> SPECIAL_TYPES_IN_TYPES_SPREADSHEET = { <EOL> '<STR_LIT>' : SpecialFunctionType ( '<STR_LIT>' , '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : SpecialFunctionType ( '<STR_LIT>' , '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : SpecialFunctionType ( '<STR_LIT>' , '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : SpecialFunctionType ( '<STR_LIT>' , '<STR_LIT>' , None ) , <EOL> } <EOL> SPECIAL_TYPES_ALL = dict ( SPECIAL_TYPES . items ( ) + SPECIAL_TYPES_IN_TYPES_SPREADSHEET . items ( ) ) <EOL> if len ( sys . argv ) <= <NUM_LIT:1> or not os . path . exists ( sys . argv [ <NUM_LIT:1> ] ) : <EOL> print "<STR_LIT>" % os . path . basename ( sys . argv [ <NUM_LIT:0> ] ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> profile_xls_filename = sys . argv [ <NUM_LIT:1> ] <EOL> workbook = xlrd . open_workbook ( profile_xls_filename ) <EOL> write_buffer = "<STR_LIT>" <EOL> def write ( s ) : <EOL> global write_buffer <EOL> write_buffer += str ( s ) <EOL> def writeln ( s = '<STR_LIT>' ) : <EOL> write ( str ( s ) + "<STR_LIT:\n>" ) <EOL> def parse_types ( ) : <EOL> types_sheet = workbook . sheet_by_name ( '<STR_LIT>' ) <EOL> types = { } <EOL> for row in range ( <NUM_LIT:1> , types_sheet . nrows ) : <EOL> row_values = [ str ( v ) . strip ( ) if isinstance ( v , ( str , unicode ) ) else v for v in types_sheet . row_values ( row , end_colx = <NUM_LIT:4> ) ] <EOL> if not any ( row_values ) : <EOL> continue <EOL> possible_type_name , possible_base_type , name , value = row_values <EOL> if possible_type_name : <EOL> type_name = possible_type_name <EOL> base_type = possible_base_type <EOL> if possible_type_name : <EOL> types [ type_name ] = Type ( type_name , base_type , { } ) <EOL> elif name : <EOL> if '<STR_LIT:int>' in base_type or base_type == '<STR_LIT>' : <EOL> if type ( value ) == float and value % <NUM_LIT:1> == <NUM_LIT:0.0> : <EOL> value = int ( value ) <EOL> types [ type_name ] . values [ value ] = TypeValue ( name , value ) <EOL> types . update ( SPECIAL_TYPES ) <EOL> for value in [ v . value for v in types [ '<STR_LIT>' ] . values . copy ( ) . itervalues ( ) <EOL> if v . name in ( '<STR_LIT>' , '<STR_LIT>' ) ] : <EOL> del types [ '<STR_LIT>' ] . values [ value ] <EOL> return types <EOL> def parse_fields ( ) : <EOL> messages_sheet = workbook . sheet_by_name ( '<STR_LIT>' ) <EOL> fields = { } <EOL> last_field = None <EOL> last_f_def_num = None <EOL> for row in range ( <NUM_LIT:1> , messages_sheet . nrows ) : <EOL> row_values = [ str ( v ) . strip ( ) if isinstance ( v , ( str , unicode ) ) else v for v in messages_sheet . row_values ( row , end_colx = <NUM_LIT:4> ) ] <EOL> if not any ( row_values ) : <EOL> continue <EOL> if not any ( row_values [ : <NUM_LIT:3> ] ) and row_values [ <NUM_LIT:3> ] : <EOL> continue <EOL> possible_message_name , f_def_num , f_name , f_type = row_values <EOL> if possible_message_name : <EOL> message_name = possible_message_name <EOL> fields [ message_name ] = { } <EOL> else : <EOL> if not ( message_name and f_name and f_type ) : <EOL> pass <EOL> else : <EOL> is_dynamic_field = False <EOL> try : <EOL> f_def_num = int ( f_def_num ) <EOL> except ValueError : <EOL> is_dynamic_field = True <EOL> if not isinstance ( last_field , DynamicField ) : <EOL> last_field = DynamicField ( * ( tuple ( last_field ) + ( { } , ) ) ) <EOL> fields [ message_name ] [ last_f_def_num ] = last_field <EOL> ref_field_names = [ str ( n ) . strip ( ) for n in messages_sheet . row_values ( row ) [ <NUM_LIT:11> ] . split ( '<STR_LIT:U+002C>' ) ] <EOL> ref_field_values = [ str ( n ) . strip ( ) for n in messages_sheet . row_values ( row ) [ <NUM_LIT:12> ] . split ( '<STR_LIT:U+002C>' ) ] <EOL> if len ( ref_field_names ) != len ( ref_field_values ) : <EOL> raise Exception ( "<STR_LIT>" % f_name ) <EOL> try : <EOL> f_scale = int ( messages_sheet . row_values ( row ) [ <NUM_LIT:6> ] ) <EOL> if f_scale == <NUM_LIT:1> : <EOL> raise ValueError <EOL> except ValueError : <EOL> f_scale = None <EOL> try : <EOL> f_offset = int ( messages_sheet . row_values ( row ) [ <NUM_LIT:7> ] ) <EOL> except ValueError : <EOL> f_offset = None <EOL> f_units = str ( messages_sheet . row_values ( row ) [ <NUM_LIT:8> ] ) . strip ( ) <EOL> if not f_units : <EOL> f_units = None <EOL> field = Field ( f_name , f_type , f_scale , f_units , f_offset ) <EOL> if is_dynamic_field : <EOL> for i in range ( len ( ref_field_names ) ) : <EOL> last_field . possibilities . setdefault ( ref_field_names [ i ] , { } ) [ ref_field_values [ i ] ] = field <EOL> else : <EOL> fields [ message_name ] [ f_def_num ] = field <EOL> last_field = field <EOL> last_f_def_num = f_def_num <EOL> event = fields [ '<STR_LIT>' ] <EOL> for k , v in event . iteritems ( ) : <EOL> if v . name == '<STR_LIT:data>' : <EOL> data_num = k <EOL> elif v . name == '<STR_LIT>' : <EOL> data16_num = k <EOL> try : <EOL> event [ data16_num ] = DynamicField ( * tuple ( event [ data16_num ] + ( event [ data_num ] . possibilities . copy ( ) , ) ) ) <EOL> except NameError : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return fields <EOL> def autogen_python ( types , fields ) : <EOL> global write_buffer <EOL> functions = { } <EOL> writeln ( "<STR_LIT>" % banner_str ( '<STR_LIT>' ) ) <EOL> for _ , type in sorted ( types . iteritems ( ) ) : <EOL> write ( "<STR_LIT>" % ( repr ( type . name ) , FIELD_BASE_TYPES [ type . base_type ] ) ) <EOL> if type . name in SPECIAL_TYPES_ALL : <EOL> special_type = SPECIAL_TYPES_ALL [ type . name ] <EOL> if type . base_type != special_type . base_type : <EOL> raise Exception ( "<STR_LIT>" % type . name ) <EOL> func_name = special_type . func_name <EOL> if not special_type . func_name : <EOL> func_name = '<STR_LIT>' % type . name . replace ( '<STR_LIT:->' , '<STR_LIT:_>' ) <EOL> functions . setdefault ( func_name , [ ] ) . append ( type . name ) <EOL> writeln ( "<STR_LIT>" % ( func_name , type . base_type ) ) <EOL> else : <EOL> writeln ( "<STR_LIT>" % type . base_type ) <EOL> for _ , value in sorted ( type . values . iteritems ( ) ) : <EOL> writeln ( "<STR_LIT>" % ( value . value , repr ( value . name ) ) ) <EOL> writeln ( "<STR_LIT>" ) <EOL> writeln ( "<STR_LIT>" % banner_str ( '<STR_LIT>' ) ) <EOL> for msg_num , message in sorted ( types [ '<STR_LIT>' ] . values . iteritems ( ) ) : <EOL> msg_name = message . name <EOL> writeln ( "<STR_LIT>" % ( msg_num , repr ( msg_name ) ) ) <EOL> msg_fields = fields [ msg_name ] <EOL> for f_num , field in sorted ( msg_fields . iteritems ( ) ) : <EOL> write ( "<STR_LIT>" % f_num ) <EOL> def field_gen ( field ) : <EOL> is_base_type = False <EOL> is_special_function_type = False <EOL> write ( "<STR_LIT>" % ( field . __class__ . __name__ , repr ( field . name ) ) ) <EOL> special_type_name = "<STR_LIT>" % ( msg_name , field . name ) <EOL> if field . type in types or special_type_name in types : <EOL> type_name = field . type <EOL> special_type = SPECIAL_TYPES_ALL . get ( special_type_name ) <EOL> if special_type : <EOL> type_name = special_type_name <EOL> is_special_function_type = True <EOL> if special_type . base_type != field . type : <EOL> raise Exception ( "<STR_LIT>" % field . name ) <EOL> write ( "<STR_LIT>" % repr ( type_name ) ) <EOL> elif field . type in FIELD_BASE_TYPES : <EOL> write ( "<STR_LIT>" % FIELD_BASE_TYPES [ field . type ] ) <EOL> is_base_type = True <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % field . type ) <EOL> write ( "<STR_LIT>" % ( <EOL> repr ( field . units ) , repr ( field . scale ) , repr ( field . offset ) ) ) <EOL> if isinstance ( field , DynamicField ) : <EOL> write ( "<STR_LIT>" ) <EOL> else : <EOL> write ( '<STR_LIT>' ) <EOL> write ( "<STR_LIT>" ) <EOL> if is_base_type or is_special_function_type : <EOL> writeln ( field . type ) <EOL> else : <EOL> writeln ( types [ field . type ] . base_type ) <EOL> field_gen ( field ) <EOL> if isinstance ( field , DynamicField ) : <EOL> for ref_name , dynamic_fields in field . possibilities . iteritems ( ) : <EOL> writeln ( '<STR_LIT>' % repr ( ref_name ) ) <EOL> for ref_value , dynamic_field in dynamic_fields . iteritems ( ) : <EOL> write ( '<STR_LIT>' % repr ( ref_value ) ) <EOL> field_gen ( dynamic_field ) <EOL> writeln ( '<STR_LIT>' ) <EOL> writeln ( '<STR_LIT>' ) <EOL> writeln ( "<STR_LIT>" ) <EOL> writeln ( "<STR_LIT>" % banner_str ( '<STR_LIT>' ) ) <EOL> for func in sorted ( set ( functions . iterkeys ( ) ) ) : <EOL> writeln ( "<STR_LIT>" % func ) <EOL> writeln ( "<STR_LIT>" % banner_str ( '<STR_LIT>' ) ) <EOL> req_func_out = '<STR_LIT:#>' * BANNER_PADDING + "<STR_LIT>" <EOL> req_func_out += "<STR_LIT>" <EOL> for func_name , type_names in sorted ( functions . iteritems ( ) ) : <EOL> req_func_out += "<STR_LIT>" % func_name <EOL> req_func_out += "<STR_LIT>" <EOL> for type_name in type_names : <EOL> req_func_out += "<STR_LIT>" % type_name <EOL> req_func_out += '<STR_LIT>' <EOL> req_func_out += ( '<STR_LIT:#>' * BANNER_PADDING ) + "<STR_LIT>" <EOL> write_buffer = req_func_out + write_buffer <EOL> def main ( ) : <EOL> global write_buffer <EOL> profile_output_filename = None <EOL> if len ( sys . argv ) >= <NUM_LIT:3> : <EOL> profile_output_filename = sys . argv [ <NUM_LIT:2> ] <EOL> if os . path . exists ( profile_output_filename ) : <EOL> old_profile = open ( profile_output_filename , '<STR_LIT:r>' ) . read ( ) <EOL> if PROFILE_OUTPUT_FILE_HEADER_MAGIC not in old_profile : <EOL> print "<STR_LIT>" % profile_output_filename <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> del old_profile <EOL> profile_header = PROFILE_OUTPUT_FILE_HEADER_FMT % ( <EOL> os . path . basename ( profile_output_filename ) , <EOL> datetime . datetime . now ( ) . strftime ( "<STR_LIT>" ) , <EOL> os . path . basename ( __file__ ) , <EOL> os . path . basename ( profile_xls_filename ) , <EOL> ) <EOL> if profile_output_filename : <EOL> print "<STR_LIT>" % profile_xls_filename <EOL> types = parse_types ( ) <EOL> fields = parse_fields ( ) <EOL> autogen_python ( types , fields ) <EOL> if profile_output_filename : <EOL> print "<STR_LIT>" % profile_output_filename <EOL> profile_output_file = open ( profile_output_filename , '<STR_LIT:w>' ) <EOL> copyright_header = open ( os . path . abspath ( __file__ ) ) . readlines ( ) <EOL> copyright_header = ( '<STR_LIT>' . join ( copyright_header [ <NUM_LIT:1> : copyright_header . index ( '<STR_LIT:\n>' ) ] ) ) . strip ( ) <EOL> profile_output_file . write ( copyright_header + "<STR_LIT>" ) <EOL> profile_output_file . write ( profile_header ) <EOL> profile_output_file . write ( write_buffer ) <EOL> profile_output_file . close ( ) <EOL> else : <EOL> print write_buffer <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
