kit_path.py 1.6 KB

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