#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 定位训练校准工具包 xlsx(audit.py 与 rule_check.py 共用)。 assets 目录内只按「_v版本号.xlsx」后缀识别:中文文件名经部分平台上传、解压后会变成乱码, 前缀不可依赖。当前工作目录仍要求完整文件名,避免误取工作区里其他带版本号的表格。 """ import os import re NAME = "SKU审核专员-训练校准工具包" PATTERN = NAME + "_v*.xlsx" _VERSIONED = re.compile(r"_v(\d+(?:\.\d+)*)\.xlsx$", re.I) _SCRIPTS = os.path.dirname(os.path.abspath(__file__)) ASSET_DIRS = [os.path.join(os.path.dirname(_SCRIPTS), "assets"), os.path.join(_SCRIPTS, "assets")] def _best(folder, prefix=""): try: names = os.listdir(folder) except OSError: return None hits = [] for name in names: found = _VERSIONED.search(name) if found and name.startswith(prefix) and not name.startswith("~$"): hits.append(([int(x) for x in found.group(1).split(".")], name)) return os.path.join(folder, max(hits)[1]) if hits else None def find(asset_dirs=None, cwd=None): """返回版本号最高的工具包路径;找不到返回 None。""" for folder in (ASSET_DIRS if asset_dirs is None else asset_dirs): hit = _best(folder) if hit: return hit return _best(os.getcwd() if cwd is None else cwd, NAME) def display_name(path): """用于打印和写入报告的文件名;无法解码的字节转义显示,不让输出报错。""" return os.path.basename(path).encode("utf-8", "backslashreplace").decode("utf-8")