urban-investment-research/Utils/CodeMake/CodeMakeUtils.py

33 lines
1.1 KiB
Python
Raw Normal View History

2023-03-27 14:42:20 +08:00
import shutil
from pathlib import Path
import os
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)
#
# code_make("CrudTemplate",
# {"EditItemType": "CompanyScore",
# "edit_item_type": "company_score",
# "修改项类型": "打分记录"}
# , "xxx/Mods/CompanyScore")