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 not code_path.exists(): os.makedirs(code_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 def to_upper_camel_case(name): items = name.split("_") new_name = "" for item in items: new_name += item[0].upper() + item[1:] return new_name 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].replace(" ", '') col_type = col_type.replace(col_type.split('(')[0], replace_dic[col_type.split('(')[0]]) comment = cols[3] comment_str = f', comment="{comment}"' if comment.replace(" ", '') 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].replace(" ", '') 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(table_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) upper_camel_case_name = to_upper_camel_case(table_name) code_make("Utils/CodeMake/CrudTemplate", {"EditItemType": upper_camel_case_name, "edit_item_type": table_name, "修改项类型": chinese_name, '"""model_content"""': model_content, '"""info_content"""': info_content, '"""add_info_content"""': add_info_content, } , f"{path}/{upper_camel_case_name}")