import shutil from pathlib import Path import os import string def code_make(dir_path, replace_dic={}, save_path="temp"): """ 增删改查代码生成 @param dir_path: 代码模板 @param replace_dic: 需要替换的名称 @param save_path: 保存路径 """ code_path = Path(dir_path) if code_path.is_dir(): save_path = Path(save_path) shutil.copytree(code_path, save_path) for root, dirs, files in os.walk(save_path): for file in files: file_path = Path(os.path.join(root, file)) item_code_str = file_path.read_text(encoding="utf-8") new_code_str = item_code_str for replace_word, replaced_word in replace_dic.items(): new_code_str = new_code_str.replace(replace_word, replaced_word) with open(file_path, "w", encoding="utf-8") as f: f.write(new_code_str) def to_snake(name): new_name = "" for i in range(len(name)): k = name[i] if name[i] in string.ascii_letters: if name[i].isupper(): if i == 0: k = name[i].lower() else: k = '_' + name[i].lower() new_name += k return new_name test_str = """id int unsigned auto_increment company_id int 企业ID result char(5) 评级结果 rate_date date 评级日期 publish_date date 披露日期 outlook char(5) 展望 rate_agency varchar(50) 评级机构 rate_file text 评级报告文件地址链接""" def table_str_convert(table_str, key='id'): replace_dic = { 'varchar': 'String', 'char': 'String', 'date': 'Date', 'datetime': 'DateTime', 'text': 'Text', 'int': 'Integer', 'double': 'Double' } new_lines = [] for line in table_str.split("\n"): if line: cols = line.split("\t") col_name = cols[0].replace(' ', "").replace("-", '_') col_type = cols[1].relace(" ", '') col_type = col_type.replace(col_type.split('(')[0], replace_dic[col_type.split('(')[0]]) comment = col_type[3] comment_str = f',comment="{col_type[3]}"' if comment else "" primary_key_str = f',primary_key=True' if col_name == key else '' new_line = f'{col_name} = Column({col_type}{primary_key_str}{comment_str})' new_lines.append(new_line) new_content = "\n ".join(new_lines) return new_content def make_info_str(table_str, key='id'): replace_dic = { 'varchar': 'str', 'char': 'str', 'text': 'str', 'double': 'float' } info_lines = [] add_info_lines = [] for line in table_str.split("\n"): if line: cols = line.split("\t") col_name = cols[0].replace(' ', "").replace("-", '_') col_type = cols[1].relace(" ", '') if col_type.split('(')[0] in replace_dic: col_type = replace_dic[col_type.split('(')[0]] # comment = col_type[3] # comment_str = f',comment="{col_type[3]}"' if comment else "" # primary_key_str = f',primary_key=True' if col_name == key else '' new_line = f'{col_name} : Optional({col_type})' info_lines.append(new_line) if col_name != "id": add_info_lines.append(new_line) info_content = "\n ".join(info_lines) add_info_content = "\n ".join(add_info_lines) return info_content, add_info_content def crud_code_make(model_name: str, chinese_name, table_str, path: str): model_content = table_str_convert(table_str) info_content, add_info_content = make_info_str(table_str) code_make("CrudTemplate", {"EditItemType": model_name, "edit_item_type": to_snake(model_name), "修改项类型": chinese_name, '"""model_content"""': model_content, '"""info_content"""': info_content, '"""add_info_content"""': add_info_content, } , f"{path}/{to_snake(model_name)}") crud_code_make("Mods/OtherInformation2/Mods")