def generate_entity_meta(res_path): entity_filepath = os.path.join(res_path, config_param.excel_path, config_param.special_file) print "start to generate [%s]" % entity_filepath excel = config_excel.my_excel(entity_filepath) excel.set_sheet_by_index(0) # set default sheet min_row_no = excel.get_min_row_no() max_row_no = excel.get_max_row_no() min_col_no = excel.get_min_col_no() max_col_no = excel.get_max_col_no() meta_doc = Document() meta_root_node = meta_doc.createElement('metas') meta_doc.appendChild(meta_root_node) for row in range(min_row_no + config_param.entity_form_head_row, max_row_no): data_node = meta_doc.createElement('meta') meta_root_node.appendChild(data_node) for col in range(min_col_no, max_col_no): # if 2nd row cell value is 'unused', ignore second_cell_value = excel.get_cell_content(min_row_no + 1, col) if second_cell_value != None and str.lower( str(second_cell_value)) == config_param.entity_unused_flag: continue # ignore empty cell in first row if excel.get_cell_content(min_row_no, col) == None: continue if excel.get_cell_content(row, col) == None: data_node.setAttribute( str(excel.get_cell_content(min_row_no, col)), "") else: data_node.setAttribute( str(excel.get_cell_content(min_row_no, col)), str(excel.get_cell_content(row, col))) with open( os.path.join( res_path, config_param.meta_path, config_param.special_file_name + config_param.meta_ext), 'w') as f: meta_doc.writexml(f, indent="\n", addindent="\t", encoding='utf-8') # open meta define file cpp_file = open(os.path.join(res_path, config_param.cpp_meta_file), 'a') cpp_file.write(u"\t//////////////////////////////////\n\t//Entity meta\n") cs_file = open(os.path.join(res_path, config_param.cs_meta_file), 'a') cs_file.write(u"\t//////////////////////////////////\n\t//Entity meta\n") # class field_name type entity_dict = {} # entity parent class entity_parent_dict = {} for row in range(min_row_no + config_param.entity_form_head_row, max_row_no): meta_class = excel.get_cell_content(row, min_col_no) if meta_class == None: continue meta_class_name = str(meta_class) if not entity_dict.has_key(meta_class_name): entity_dict[meta_class_name] = [] # filed type sub_class for col in [ config_param.entity_field_name_col, config_param.entity_field_type_col, config_param.entity_field_sub_class_col ]: cell_content = excel.get_cell_content(row, col) if cell_content == None: entity_dict[meta_class_name].append("") else: entity_dict[meta_class_name].append(str(cell_content)) # field type parent_class parent_class_cell = excel.get_cell_content( row, config_param.entity_parent_class_col) if (parent_class_cell != None): if (not entity_parent_dict.has_key(meta_class_name)): entity_parent_dict[meta_class_name] = [] parent_class_name = str(parent_class_cell) if parent_class_name not in entity_parent_dict[meta_class_name]: entity_parent_dict[meta_class_name].append(parent_class_name) # print entity_dict # print entity_parent_dict # first of all, add parent entity classes for i in entity_parent_dict: parent_list = entity_parent_dict.get(i) if parent_list == None: continue for j in xrange(0, len(parent_list)): parent = parent_list[j] if not entity_dict.has_key(parent): continue write_entity(cpp_file, parent, entity_dict, True) write_entity(cs_file, parent, entity_dict, False) # then other entity classes for k in entity_dict: # jump over parent meta classes find = False for p in entity_parent_dict: parent_list = entity_parent_dict.get(p) if parent_list == None: continue if k in parent_list: find = True break if find == True: continue write_entity_cpp_head(cpp_file, k) write_entity_cs_head(cs_file, k) # parent class members parent_list = entity_parent_dict.get(k) if parent_list != None: cpp_file.write("\t\t//parent entity class\n") for parent in parent_list: write_entity_all_member(cpp_file, parent, entity_dict, True) write_entity_all_member(cs_file, parent, entity_dict, False) cpp_file.write("\n\t\t//self entity class\n") write_entity_all_member(cpp_file, k, entity_dict, True) write_entity_all_member(cs_file, k, entity_dict, False) write_entity_cpp_end(cpp_file) write_entity_cs_end(cs_file) # sperate line cpp_file.write(u"\t//////////////////////////////////\n\t//Config meta\n") cs_file.write(u"\t//////////////////////////////////\n\t//Config meta\n") # close meta define files cpp_file.close() cs_file.close() print "generate [%s] finished" % entity_filepath
def genrate_single_config(res_path, filepath, filename): excel = config_excel.my_excel(filepath) excel.set_sheet_by_index(0) # set default sheet min_row_no = excel.get_min_row_no() max_row_no = excel.get_max_row_no() min_col_no = excel.get_min_col_no() max_col_no = excel.get_max_col_no() '''config meta xml''' meta_doc = Document() meta_root_node = meta_doc.createElement('metas') meta_doc.appendChild(meta_root_node) for col in range(min_col_no, max_col_no): data_node = meta_doc.createElement('meta') for row in range(min_row_no, min_row_no + config_param.config_form_head_row): if config_param.config_form_head_list[row - min_row_no + 1] == 'target': target_value = str.upper(str(excel.get_cell_content(row, col))) # S(erver) or A(all) will be generate for server side if target_value != config_param.target_all or target_value != config_param.target_server: continue data_node.setAttribute( config_param.config_form_head_list[row - min_row_no + 1], excel.get_cell_content(row, col)) if data_node.hasAttributes(): meta_root_node.appendChild(data_node) with open( os.path.join(res_path, config_param.meta_path, filename + config_param.meta_ext), 'w') as f: meta_doc.writexml(f, indent="\n", addindent="\t", encoding='utf-8') '''config data xml''' config_doc = Document() config_root_node = config_doc.createElement('data') config_doc.appendChild(config_root_node) for row in range(min_row_no + config_param.config_form_head_row, max_row_no): data_node = config_doc.createElement('data') config_root_node.appendChild(data_node) for col in range(min_col_no, max_col_no): # ignore empty cell in first row if excel.get_cell_content(min_row_no, col) == None: continue data_node.setAttribute( str(excel.get_cell_content(min_row_no, col)), str(excel.get_cell_content(row, col))) with open( os.path.join(res_path, config_param.server_res_path, filename + config_param.config_ext), 'w') as f: config_doc.writexml(f, indent="\n", addindent="\t", encoding='utf-8') # open meta define file cpp_file = open(os.path.join(res_path, config_param.cpp_meta_file), 'a') cpp_file.write(u"\tclass AFConfigMeta" + filename.capitalize() + "\n\t{\n\tpublic:\n") cpp_file.write( u'''\t\tstatic const std::string& self_name() { static const std::string meta_%s_ = "%s"; return meta_%s_; }\n\n''' % (filename, filename, filename)) cs_file = open(os.path.join(res_path, config_param.cs_meta_file), 'a') cs_file.write(u"\tpublic class AFConfigMeta" + filename.capitalize() + "\n\t{\n") cs_file.write( u'''\t\tpublic static readonly String self_name = "%s";\n\n''' % filename) for col in range(min_col_no, max_col_no): filed_name = str(excel.get_cell_content(min_row_no, col)) type_name = str(excel.get_cell_content(min_row_no + 1, col)) cpp_file.write( u'''\t\tstatic const std::string& %s() { static const std::string %s_ = "%s"; return %s_; } // %s\n''' % (filed_name, filed_name, filed_name, filed_name, type_name)) cs_file.write( u'''\t\tpublic static readonly String %s = "%s"; // %s\n''' % (filed_name, filed_name, type_name)) cpp_file.write(u"\t};\n\n") cs_file.write(u"\t}\n\n") # close meta define files cpp_file.close() cs_file.close() return True
def genrate_single_config(res_path, excel_list, classname): path = excel_list[0] excel = config_excel.my_excel(path) excel.set_sheet_by_index(0) # set default sheet min_row_no = excel.get_min_row_no() max_row_no = excel.get_max_row_no() + 1 min_col_no = excel.get_min_col_no() max_col_no = excel.get_max_col_no() + 1 '''config meta xml''' meta_doc = Document() meta_root_node = meta_doc.createElement('metas') meta_doc.appendChild(meta_root_node) for col in range(min_col_no, max_col_no): data_node = meta_doc.createElement('meta') for row in range(min_row_no, min_row_no + config_param.config_form_head_row): if config_param.config_form_head_list[row - min_row_no + 1] == 'target': target_value = str.upper(str(excel.get_cell_content(row, col))) # S(erver) or A(all) will be generate for server side if target_value != config_param.target_all or target_value != config_param.target_server: continue data_node.setAttribute( config_param.config_form_head_list[row - min_row_no + 1], excel.get_cell_content(row, col)) # data_node.setAttribute(config_param.field_index,filename+"."+excel.get_cell_content(min_row_no, col)) if data_node.hasAttributes(): meta_root_node.appendChild(data_node) with open(os.path.join(res_path, config_param.meta_path, classname + config_param.meta_ext), 'w', encoding='utf-8') as f: meta_doc.writexml(f, indent="\n", addindent="\t", encoding='utf-8') '''config data xml''' config_doc = Document() config_root_node = config_doc.createElement('data') config_doc.appendChild(config_root_node) for filepath in excel_list: excel = config_excel.my_excel(filepath) excel.set_sheet_by_index(0) # set default sheet min_row_no = excel.get_min_row_no() max_row_no = excel.get_max_row_no() + 1 min_col_no = excel.get_min_col_no() max_col_no = excel.get_max_col_no() + 1 for row in range(min_row_no + config_param.config_form_head_row, max_row_no): data_node = config_doc.createElement('data') config_root_node.appendChild(data_node) for col in range(min_col_no, max_col_no): # ignore empty cell in first row if excel.get_cell_content(min_row_no, col) == None: continue data_node.setAttribute( str(excel.get_cell_content(min_row_no, col)), str(excel.get_cell_content(row, col))) with open(os.path.join(res_path, config_param.server_res_path, classname + config_param.config_ext), 'w', encoding='utf-8') as f: config_doc.writexml(f, indent="\n", addindent="\t", encoding='utf-8') # open meta define file cpp_file = open(os.path.join(res_path, config_param.cpp_meta_file), 'a', encoding='utf-8') cs_file = open(os.path.join(res_path, config_param.cs_meta_file), 'a', encoding='utf-8') cpp_file.write(u"\tenum class %s : std::uint32_t\n\t{\n\t\tmeta_empty,\n" % (classname)) cs_file.write(u"\tenum %s\n\t{\n\t\tmeta_empty,\n" % (classname)) excel = config_excel.my_excel(excel_list[0]) excel.set_sheet_by_index(0) # set default sheet min_row_no = excel.get_min_row_no() max_row_no = excel.get_max_row_no() + 1 min_col_no = excel.get_min_col_no() max_col_no = excel.get_max_col_no() + 1 for col in range(min_col_no, max_col_no): field_name = str(excel.get_cell_content(min_row_no, col)) cpp_file.write(u"\t\t%s,\n" % (field_name)) cs_file.write(u"\t\t%s,\n" % (field_name)) cpp_file.write(u"\t};\n\n") cpp_file.write(u"\tusing " + classname + "_rep_type = std::underlying_type<" + classname + ">::type;\n\n") cs_file.write(u"\t}\n") cpp_file.write(u"\tclass AFConfigMeta" + classname.capitalize() + "\n\t{\n\tpublic:\n") cpp_file.write( u'''\t\tstatic const std::string& self_name() { static const std::string meta_%s_ = "%s"; return meta_%s_; }\n\n''' % (classname, classname, classname)) cs_file.write(u"\tpublic class AFConfigMeta" + classname.capitalize() + "\n\t{\n") cs_file.write( u'''\t\tpublic static readonly String self_name = "%s";\n\n''' % classname) if (not entity_dict.__contains__(classname)): entity_dict[classname] = [] for col in range(min_col_no, max_col_no): field_name = str(excel.get_cell_content(min_row_no, col)) type_name = str(excel.get_cell_content(min_row_no + 1, col)) entity_dict[classname].append(field_name) entity_dict[classname].append(type_name) entity_dict[classname].append("") cpp_file.write( u'''\t\tstatic const std::string& %s() { static const std::string %s_ = "%s"; return %s_; } // %s\n''' % (field_name, field_name, field_name, field_name, type_name)) cpp_file.write( u'''\t\tstatic uint32_t %s_index() { static const int %s_index_ = static_cast<%s_rep_type>(%s::%s); return %s_index_; } // %s\n''' % (field_name, field_name, classname, classname, field_name, field_name, field_name)) cs_file.write( u'''\t\tpublic static readonly String %s = "%s"; // %s\n''' % (field_name, field_name, type_name)) cs_file.write( u'''\t\tpublic static UInt32 %s_index = (UInt32)%s.%s; // %s\n''' % (field_name, classname, field_name, field_name)) cpp_file.write(u"\t};\n\n") cs_file.write(u"\t}\n\n") # close meta define files cpp_file.close() cs_file.close() return True