feat: 新增银行卡检查插件及相关依赖与配置文件
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""XQ0033 插件 - 积分工作簿目标文件选择。
|
||||
|
||||
文件名 = 前缀 + 积分日期段(如 1009 单日 / 1011-12 跨日)。
|
||||
优先复用文件名含 T-2 的文件(续用追加);否则复制修改时间最新的文件另存为
|
||||
「前缀{T-2 MMDD}.xlsx」作为新批(积分导入/删除表由调用方清空重写)。
|
||||
"""
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
from datetime import date, timedelta
|
||||
|
||||
from .. import config
|
||||
|
||||
|
||||
def parse_name_dates(filename, year=None):
|
||||
"""从文件名解析积分日期段集合 → {date}(跨日按起止展开为逐日)。"""
|
||||
stem = os.path.splitext(filename)[0]
|
||||
if stem.startswith(config.FILE_PREFIX):
|
||||
stem = stem[len(config.FILE_PREFIX):]
|
||||
year = year or date.today().year
|
||||
dates = set()
|
||||
first_month = None
|
||||
for seg in stem.split("-"):
|
||||
seg = seg.strip()
|
||||
if re.fullmatch(r"\d{4}", seg):
|
||||
m, d = int(seg[:2]), int(seg[2:])
|
||||
if 1 <= m <= 12 and 1 <= d <= 31:
|
||||
first_month = m
|
||||
dates.add(date(year, m, d))
|
||||
elif re.fullmatch(r"\d{2}", seg) and first_month is not None:
|
||||
d = int(seg)
|
||||
if 1 <= d <= 31:
|
||||
dates.add(date(year, first_month, d))
|
||||
if len(dates) > 1:
|
||||
lo, hi = min(dates), max(dates)
|
||||
span, cur = set(), lo
|
||||
while cur <= hi:
|
||||
span.add(cur)
|
||||
cur += timedelta(days=1)
|
||||
dates = span
|
||||
return dates
|
||||
|
||||
|
||||
def pick_latest_file(save_dir, force_file=None):
|
||||
"""仅推送时使用:取目录内修改时间最新的批次文件(不新建批次)。"""
|
||||
if force_file:
|
||||
if not os.path.isfile(force_file):
|
||||
raise FileNotFoundError(f"指定的工作簿不存在:{force_file}")
|
||||
return os.path.abspath(force_file)
|
||||
if not os.path.isdir(save_dir):
|
||||
raise FileNotFoundError(f"工作簿目录不存在:{save_dir}")
|
||||
files = [f for f in os.listdir(save_dir)
|
||||
if f.startswith(config.FILE_PREFIX) and f.endswith(".xlsx")
|
||||
and not f.startswith("~$")]
|
||||
if not files:
|
||||
raise FileNotFoundError(f"目录 {save_dir} 未找到「{config.FILE_PREFIX}*.xlsx」文件")
|
||||
return os.path.join(save_dir, max(files, key=lambda f: os.path.getmtime(os.path.join(save_dir, f))))
|
||||
|
||||
|
||||
def pick_target_file(today, save_dir, force_file=None):
|
||||
"""选择本轮写入的积分工作簿 → (绝对路径, 是否新建批次文件, 上轮已处理的积分日期)。
|
||||
|
||||
第三个返回值取「被复制批次文件名里的最大日期」,用于识别两次运行之间漏处理的日期;
|
||||
指定工作簿时来源不可信,返回 None(不做补漏推断)。
|
||||
"""
|
||||
if force_file:
|
||||
if not os.path.isfile(force_file):
|
||||
raise FileNotFoundError(f"指定的工作簿不存在:{force_file}")
|
||||
return os.path.abspath(force_file), False, None
|
||||
|
||||
if not os.path.isdir(save_dir):
|
||||
raise FileNotFoundError(f"工作簿目录不存在:{save_dir}")
|
||||
|
||||
locks = [f for f in os.listdir(save_dir)
|
||||
if f.startswith("~$") and f.endswith(".xlsx")]
|
||||
if locks:
|
||||
raise RuntimeError(
|
||||
"检测到 WPS/Excel 锁文件(%s),目标文件正被打开,请先关闭后重试" % "、".join(locks))
|
||||
|
||||
files = [f for f in os.listdir(save_dir)
|
||||
if f.startswith(config.FILE_PREFIX) and f.endswith(".xlsx")
|
||||
and not f.startswith("~$") and parse_name_dates(f, today.year)]
|
||||
if not files:
|
||||
raise RuntimeError(
|
||||
f"目录 {save_dir} 下未找到「{config.FILE_PREFIX}MMDD.xlsx」格式数据文件,"
|
||||
"请先放入模板文件")
|
||||
|
||||
t2 = today - timedelta(days=2)
|
||||
for f in files:
|
||||
if t2 in parse_name_dates(f, today.year):
|
||||
path = os.path.join(save_dir, f)
|
||||
config.logger.info("命中文件名含 T-2(%s) 的数据文件:%s(续用追加模式)",
|
||||
t2.strftime("%m%d"), f)
|
||||
return path, False, t2
|
||||
|
||||
newest = max(files, key=lambda f: os.path.getmtime(os.path.join(save_dir, f)))
|
||||
src_dates = parse_name_dates(newest, today.year)
|
||||
covered_until = max(src_dates) if src_dates else None
|
||||
new_name = f"{config.FILE_PREFIX}{t2.strftime('%m%d')}.xlsx"
|
||||
new_path = os.path.join(save_dir, new_name)
|
||||
if os.path.exists(new_path):
|
||||
config.logger.warning("新批文件 %s 已存在但文件名未匹配 T-2,直接复用", new_name)
|
||||
return new_path, False, covered_until
|
||||
shutil.copy2(os.path.join(save_dir, newest), new_path)
|
||||
config.logger.info("未找到文件名含 T-2 的文件,已从最新文件 %s 复制新建批次文件:%s"
|
||||
"(上轮积分日期推断为 %s)",
|
||||
newest, new_name, covered_until or "未知")
|
||||
return new_path, True, covered_until
|
||||
Reference in New Issue
Block a user