|
|
@@ -32,15 +32,26 @@ def print_sql_files():
|
|
|
|
|
|
return sql_files_dict
|
|
|
|
|
|
-def get_month_data():
|
|
|
- """获取并返回当前月份和次月等数据
|
|
|
+def get_month_data(month=None):
|
|
|
+ """获取并返回指定月份或当前月份的相关数据
|
|
|
+
|
|
|
+ Args:
|
|
|
+ month: 可选参数,格式为 YYYYMM (如 "202602"),如果不传则使用当前月份
|
|
|
|
|
|
返回格式:
|
|
|
["202602", "202603", "202601", "2026-02"]
|
|
|
- 分别对应:当前月份、次月、上月、当前月份(带分隔符)
|
|
|
+ 分别对应:指定月份、次月、上月、指定月份(带分隔符)
|
|
|
"""
|
|
|
- # 获取当前日期
|
|
|
- now = datetime.now()
|
|
|
+ # 获取日期
|
|
|
+ if month:
|
|
|
+ # 如果传入了月份,解析为日期
|
|
|
+ # 解析 YYYYMM 格式的月份
|
|
|
+ year = int(month[:4])
|
|
|
+ month_num = int(month[4:])
|
|
|
+ now = datetime(year, month_num, 1)
|
|
|
+ else:
|
|
|
+ # 如果没有传入月份,使用当前日期
|
|
|
+ now = datetime.now()
|
|
|
|
|
|
# 当前月份 (格式: YYYYMM)
|
|
|
current_month = now.strftime("%Y%m")
|
|
|
@@ -81,9 +92,12 @@ def ensure_directory(directory_path):
|
|
|
shutil.rmtree(item_path)
|
|
|
print(f"删除目录: {item_path}")
|
|
|
|
|
|
-def generate_sql_files():
|
|
|
+def generate_sql_files(month=None):
|
|
|
"""生成SQL文件
|
|
|
|
|
|
+ Args:
|
|
|
+ month: 可选参数,格式为 YYYYMM (如 "202602"),如果不传则使用当前月份
|
|
|
+
|
|
|
步骤:
|
|
|
1. 调用 ensure_directory 确保 SqlOut 目录存在且为空
|
|
|
2. 调用 get_month_data 读取参数列表
|
|
|
@@ -91,13 +105,26 @@ def generate_sql_files():
|
|
|
4. 遍历步骤3的返回值,每条把结果按照步骤2的返回值进行string替换其中{0}{1}{2}{3}的值
|
|
|
5. 按照key值输出到步骤1所说的目录里
|
|
|
"""
|
|
|
+ # 对传入的月份参数进行格式校验
|
|
|
+ if month:
|
|
|
+ # 检查长度是否为6位
|
|
|
+ if len(month) != 6:
|
|
|
+ raise ValueError(f"月份参数格式错误: {month},应为6位数字,格式为 YYYYMM")
|
|
|
+ # 检查是否为数字
|
|
|
+ if not month.isdigit():
|
|
|
+ raise ValueError(f"月份参数格式错误: {month},应为数字")
|
|
|
+ # 检查月份是否在有效范围内 (1-12)
|
|
|
+ month_num = int(month[4:])
|
|
|
+ if month_num < 1 or month_num > 12:
|
|
|
+ raise ValueError(f"月份参数格式错误: {month},月份应在 1-12 之间")
|
|
|
+
|
|
|
# 步骤1: 确保 SqlOut 目录存在且为空
|
|
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
|
|
sql_out_dir = os.path.join(current_dir, "SqlOut")
|
|
|
ensure_directory(sql_out_dir)
|
|
|
|
|
|
# 步骤2: 读取参数列表
|
|
|
- month_data = get_month_data()
|
|
|
+ month_data = get_month_data(month)
|
|
|
print(f"\n获取到的月份参数: {month_data}")
|
|
|
|
|
|
# 步骤3: 获得文件键值对
|
|
|
@@ -109,28 +136,8 @@ def generate_sql_files():
|
|
|
for file_name, content in sql_files.items():
|
|
|
# 替换内容中的 {0}{1}{2}{3}
|
|
|
try:
|
|
|
- # 先处理文件中可能存在的单独大括号,将它们转义
|
|
|
- # 然后使用format方法进行格式化
|
|
|
- # 注意:需要先将文件中的单独大括号转义为双大括号
|
|
|
- # 这样format方法就不会将它们视为占位符
|
|
|
- # 但要注意不要影响现有的{0}{1}{2}{3}占位符
|
|
|
-
|
|
|
- # 先保存现有的占位符
|
|
|
- temp_content = content
|
|
|
- placeholders = {}
|
|
|
- for i in range(4):
|
|
|
- placeholder = f"{{{i}}}"
|
|
|
- temp_content = temp_content.replace(placeholder, f"__PLACEHOLDER_{i}__")
|
|
|
-
|
|
|
- # 转义单独的大括号
|
|
|
- temp_content = temp_content.replace("{", "{{").replace("}", "}}")
|
|
|
-
|
|
|
- # 恢复占位符
|
|
|
- for i in range(4):
|
|
|
- temp_content = temp_content.replace(f"__PLACEHOLDER_{i}__", f"{{{i}}}")
|
|
|
-
|
|
|
- # 使用format方法进行格式化
|
|
|
- replaced_content = temp_content.format(*month_data)
|
|
|
+ # 使用 str.format 方法替换占位符
|
|
|
+ replaced_content = content.format(*month_data)
|
|
|
|
|
|
# 构建输出文件路径
|
|
|
output_path = os.path.join(sql_out_dir, file_name)
|
|
|
@@ -143,5 +150,16 @@ def generate_sql_files():
|
|
|
except Exception as e:
|
|
|
print(f"处理文件 {file_name} 时出错: {e}")
|
|
|
|
|
|
+import sys
|
|
|
+
|
|
|
if __name__ == "__main__":
|
|
|
- generate_sql_files()
|
|
|
+ # 检查命令行参数
|
|
|
+ if len(sys.argv) > 1:
|
|
|
+ # 获取传入的月份参数
|
|
|
+ month = sys.argv[1]
|
|
|
+ print(f"使用传入的月份参数: {month}")
|
|
|
+ generate_sql_files(month)
|
|
|
+ else:
|
|
|
+ # 没有传入参数,使用当前月份
|
|
|
+ print("没有传入月份参数,使用当前月份")
|
|
|
+ generate_sql_files()
|