Merge pull request 'feat: 单项 Value 策略与迁移工程多 Excel BOM 导入' (#21) from codex/item-value-bom-import into main
Core regression / Python core and release configuration (push) Successful in 18s
Core regression / JavaScript syntax (push) Successful in 3s

This commit was merged in pull request #21.
This commit is contained in:
abaishu0730 committed 2026-11-05 14:08:00 +08:00
commit 08ddf4cd6a
41 files changed
+1997 -83

No files matched your search

+3 -1
View File
@@ -24,7 +24,7 @@ jobs:
- name: Install runner Python and test dependencies
run: |
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends python3 python3-pytest
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends python3 python3-pytest python3-openpyxl
python3 --version
- name: Compile application and build helpers
run: python3 -m compileall -q src packaging tools
@@ -39,6 +39,8 @@ jobs:
tests/packaging/test_dsh_runtime_staging.py
tests/packaging/test_release_configuration.py
tests/packaging/test_self_extracting_release.py
tests/trial_task_center/test_bom_template_contract.py
tests/trial_task_center/test_bom_import_review.py
--junitxml=build/ci-results.xml --tb=short
- name: Windows release scope
run: |
@@ -0,0 +1,34 @@
# Item Value and Migrated BOM Import
Date: 2026-11-05
## Approved scope
- Task-level Value policy remains selectable. `inherit` follows the task setting (`part_label` when enabled, otherwise `preserve`).
- Every target row can explicitly choose `preserve`, `part_label`, `part_number`, or `custom`; explicit choices override the task default.
- BOM import accepts multiple Excel files. Existing exact project paths take precedence; stale paths can resolve through an exact, unique project name. Ambiguous names block import.
- Preserve source workbook, original Excel row, original project path, current path, and match reason.
- Verify each function independently using generated fixtures.
## Implementation checklist
- [x] Add and validate persisted item Value mode; preserve legacy custom Value inputs.
- [x] Resolve inherited modes using the task setting during execution, context snapshots, BOM validation, and new-round checks.
- [x] Group rules by concrete item mode and freeze each mode/custom text into execution commands.
- [x] Preserve the existing recovery comparison of queue policy, custom text, and target identities.
- [x] Add optional BOM Value mode and plan export/import support; accept legacy 11-column BOM and 13-column target plans.
- [x] Parse all selected workbooks before inspection and preserve original source coordinates.
- [x] Route exact paths and uniquely matching migrated names; isolate projects with different names even when erroneous old paths are identical.
- [x] Retain cross-file duplicate blocking and atomic plan saving.
- [x] Add item dropdown selection and synchronize legacy custom checkboxes; commit normally and cancel with Esc.
- [x] Switch the BOM picker and its UI tests to multi-selection.
- [x] Reuse supervised inspection, cancellation, phase reporting, and cleanup failure handling.
- [x] Keep editable mapping-rule IDs separate from compiled execution rules.
- [x] Preserve explicit item policies when batch custom defaults change; disabling a matching seeded custom default restores inheritance.
- [x] Add an integration fixture covering two workbooks, migration routing, four Value modes, save, database reopen, frozen commands, and recovery readback/mismatch rejection.
## Verification
See `docs/testing/2026-11-05-item-value-bom-import.md` for generated samples, independent acceptance cases, commands, results, and the real-CAD boundary.
Existing unrelated DSH work in this checkout is retained. No commit, remote branch deletion, merge, or deployment is part of this implementation.
@@ -0,0 +1,70 @@
# 单项 Value 与多文件 BOM 导入验收
日期:2026-11-05。范围:用户确认的单项 Value 策略、迁移工程 BOM 导入、多选 Excel,以及各功能的独立模拟验证。
## 模拟样本
测试每次在 pytest 临时目录生成 Excel、SQLite 数据库和工程占位文件,不改真实工程。
- 两个工程:`BoardA/BoardA.prj`、`BoardB/BoardB.prj`,各包含 R1、R2,旧料号为 OLD,原 Value 为 10nF。
- 两份 Excel:BoardA.xlsx 使用继承任务默认与 Preserve;BoardB.xlsx 使用 Custom=47k 与 Part Number。
- Excel 中的旧工程路径为 `Z:/old/...`,当前工程路径位于临时目录;按工程名称精确路由。
- 负向样本:同名不同目录、两份文件重复 R1、公式、Excel 错误单元格、旧计划的文本 FALSE、自定义 Value 为空、相同错误旧路径但不同工程名称。
- 实际调用 BOM 解析、匹配、计划保存、数据库重开、队列生成和恢复解析;CAD 运行环境用测试对象替代。
## 独立验收项
以下各项均已通过对应的独立模拟用例;完整流程另有两工作簿的集成闭环验证。
| 功能 | 验证内容与预期 |
| --- | --- |
| 单项显式策略 | Preserve、Part Label、Part Number、Custom 分别进入对应规则和执行命令 |
| 默认值与覆盖 | 继承项随任务默认使用 Part Label;显式 Preserve 保持原 Value |
| 批量规则传播 | 修改批量自定义默认值时保留单项显式 Preserve、Part Label、Part Number;关闭匹配的旧自定义默认值后恢复继承 |
| 自定义一致性 | 旧自定义输入保留;取消勾选回到继承;空自定义阻止生成执行规则 |
| 数据库存储 | 旧表增加字段后可读取;保存并重新打开数据库后策略仍正确 |
| 恢复绑定 | 读取冻结的单项策略和自定义内容;改变策略或自定义内容后旧队列不再匹配 |
| 多选 Excel | 两个工作簿一次预览;跨工程编辑后一次保存;未确认前不修改计划 |
| 迁移路径 | 路径失效且工程名称唯一一致时使用当前路径;不同工程名称不会因相同错误旧路径而混淆 |
| 名称歧义 | 相同名称对应多个工程时停止自动路由 |
| 重复阻塞 | 两份 Excel 声明同一位号时整批预览失败,已有计划保持不变 |
| 来源追踪 | 每条结果附原文件与行号;source-map.json 保留原路径、当前路径及匹配原因 |
| 旧格式兼容 | 11 列 BOM 的自定义内容继续生效;13 列计划的文本 FALSE 正确解析 |
| 输入校验 | 第二份文件含公式或错误料号时,在进入工程检查前阻止本次导入 |
| 编辑操作 | 下拉选择提交后生效;Esc 取消;Value 列提示可读取 |
| 进度与失败处理 | 多文件导入沿用监督进程、阶段通知、取消和资源清理保护;失败报告仍保留来源 |
| 原有规则编辑 | 单项覆盖不替换规则表中的可编辑规则 ID |
专项用例位于 `tests/trial_task_center/test_item_value_and_bom_sources.py`;界面用例位于 `tests/ui/test_value_options_layout.py` 及 `tests/v1_1/test_bom_batch.py`、`test_bom_import_report.py`、`test_ui_workspace.py`。
## 最终执行结果
| 检查 | 实际结果 |
| --- | --- |
| Trial Task Center 全目录 | 355 passed,1 项 pywinauto COM 线程模式警告 |
| 新增专项 24 项与原有批量规则 9 项 | 33 passed |
| BOM、计划、目标执行、新回合及相关 UI 回归 | 165 passed |
| 打包契约检查 | 3 passed |
| Python compileall、git diff --check | 退出码 0;Git 提示已有文件的换行转换警告 |
| DSH 运行时契约检查 | status=passed;不代表真实 CAD 操作 |
专项与批量规则的重跑命令:
```powershell
python -m pytest -q tests/trial_task_center/test_item_value_and_bom_sources.py tests/v1_1/test_batch_rules.py
python -m pytest -q tests/trial_task_center
python -m pytest -q tests/packaging/test_dx_part_replace_build.py
```
165 项回归的重跑命令(Qt 使用 offscreen):
```powershell
$env:QT_QPA_PLATFORM='offscreen'
python -m pytest -q tests/v1_1/test_bom_import_report.py tests/v1_1/test_bom_batch.py tests/v1_1/test_ui_workspace.py tests/v1_1/test_bom_plan.py tests/v1_1/test_bom_part_number_types.py tests/v1_1/test_spreadsheet_plan.py tests/v1_1/test_target_cell_rules.py tests/v1_1/test_target_execution.py tests/v1_1/test_backend_targets.py tests/v1_1/test_new_round.py tests/ui/test_value_options_layout.py tests/ui/test_ui_polish.py tests/ui/test_trial_task_center_workflow.py
```
独立只读复核复现了两项批量默认值传播问题:覆盖单项显式策略,以及关闭旧自定义默认值失败。4 项新增回归先复现失败,再验证修复;复核已确认两项问题解决。专项也补验了旧 BOM 自定义内容、文本 FALSE、错误单元格、Esc 取消、可编辑规则 ID 和来源行号偏移。
## 验证边界
本记录的模拟验收覆盖输入、规则、持久化、冻结队列、恢复读取和界面交互。后续已按用户要求补充真实 DxDesigner 替换、保存重开、核验恢复及独立磁盘读取,见 [真实替换实测](2026-11-05-item-value-bom-native.md);该实测未执行 PCB。全仓库测试与完整安装包构建没有作为本次通过结论。
@@ -0,0 +1,45 @@
# 单项 Value 与迁移 BOM 的真实替换实测
日期:2026-11-05。按用户要求只验证原理图替换、保存和回读,未执行 PCB、Packager 或前向标注。
## 结果
两个真实工程的新副本、两份旧路径 Excel、四个位号已完成实际 DxDesigner Replace Part。四项均有保存及关闭重开回读记录;最终另启全新 CAD 进程独立读取两份磁盘工程,取得以下结果。
全部由 `4100-CA0000-T7` 替换为 `4100-CA0000-T0`,目标 Cell 为 `RESC1608N_0603`。
| 工程 | 位号 | 单项策略 | 独立读回的 Value | Part Label |
| --- | --- | --- | --- | --- |
| Audio_D2128 | RV05 | 继承任务默认 Part Label | 0R | 0R |
| Audio_D2128 | RV11 | 显式 Preserve,覆盖任务默认 | 空,无 Value 属性 | 0R |
| Audio_T605A | RV05 | 显式 Part Number | 4100-CA0000-T0 | 0R |
| Audio_T605A | RV11 | 显式 Custom | 47k | 0R |
Preserve 沿用既有语义:保留目标器件原生 Value 状态,不复制旧器件的 Value。本库目标没有 Value 属性,因此本例读回为空。
两份 Excel 均使用 `Z:/migrated-old-location/...`,按唯一同名工程成功路由;4 行导入通过,来源文件和原行号均保留。每个目标 UID 未变,并独立取得两个引脚。
## 实测发现及处理
本轮并非一次无报错运行。首次执行在第一个工程两项保存回读成功后,资源状态文件原子替换遇到 WinError 5,队列安全停止。定位到 `batch_resources.write_record` 没有向已有的共享占用识别逻辑传入目标路径;补齐该参数,真实 Windows 读句柄占用复现用例由失败转为通过,永久权限错误仍不重试。
恢复原队列时只执行尚未开始的第二个工程。第二个工程两项也保存回读成功,但最后写 `project-session-result.tsv` 时,ADODB.Stream 遇到读占用,触发保护及资源清理核验。为这一特定回执文件增加最多 20 次、间隔 50ms 的文件写入重试;不重试其他 CAD 操作。真实 cscript + Windows 文件句柄用例验证短暂占用成功、持续占用失败。
之后通过已有资源清理及核验恢复接口核验磁盘结果,取得 `restored_ok / recovery_saved_verified`,两任务均完成。恢复记录的 `native_replacement_called=false`;两份任务原生日志合计恰好 4 次替换,没有重做已完成步骤。最后由全新 CAD 进程独立读取上表结果。
修复后没有从头再替换四个位号;两个文件占用修复以对应的真实文件操作复现验证,原生业务结果通过核验恢复和独立磁盘读取收尾。
## 证据
证据根:`build/item-value-native-20261105-103328/`。
- 最终报告:`reconciliation-report-105332.json`,`verified=true`。
- 首次及续行失败记录:`report.json`、`continuation-report.json`,保留原始故障。
- 独立磁盘读取:`Audio_D2128-saved.json`、`Audio_T605A-saved.json`,每工程 57 个器件。
- 恢复证据:`data/tasks/53a0e391-6fc2-4685-90bf-66e44000e2fa/quick-run-history/recovery-f4264066ce8c4cf8b4b36d91d790205f/recovery.json`。
- 原工程目录哈希、原中央库 LMC 哈希均未变;测试副本 PCB 文件哈希未变,`pcb_execution=false`。
- 自有 CAD 均退出,四个私有桌面均释放;未抢占用户输入桌面。
实测工具:`tools/probe_item_value_bom_live.py`,复用现有生产服务、原生脚本、私有 CAD 会话与独立读取器,没有使用模拟 CAD。
本轮仅额外运行与两个现场文件占用故障直接相关的 4 个小用例,没有重复运行整套回归。
@@ -0,0 +1,32 @@
# PR #21 审查修复与独立验证
审查来源:[评论 217](https://git.simgreek.com/Skyworth_Digital_Hardware/dxdesigner-part-replacement/pulls/21#issuecomment-217)。本轮修复基于 `8b5509d`,覆盖 1 项 P2、5 项 P3,以及 CI 覆盖建议。保留 PR,未执行合并。
## 修复与验收对应
| 审查项 | 最终行为 | 独立验证 |
| --- | --- | --- |
| 任务显示名与工程文件名不一致 | 精确路径优先;接受唯一的任务显示名或工程文件名;显示名冲突仍阻断。单工程路径留空时保留旧的唯一工程绑定行为,显式错误路径不能任意兜底 | 改名“音频板”后导出、修改为旧路径、真实 XLSX 重新导入;显示名重复与精确路径优先;单工程任意显示名空路径 |
| 跨文件重复提示行号矛盾 | 报告提供 `source_file`、`source_row`;重复原因同时列出全部原文件与原行;界面显示原始行号 | 第一份第 2 行与第二份第 5 行重复,报告两行均失败且无预览;UI 格式验证显示第二份第 5 行 |
| 来源证据无引用、无导出、无限累积 | 来源映射存于锚点任务的 `quick-run-history/bom-import-<uuid>/source-map.json`;报告和任务事件引用;诊断包只纳入 JSON 元数据并沿用脱敏;复用现有历史数量、大小、时间限制;临时规范化 Excel 在 `finally` 删除 | 真实服务导入后检查报告、事件、诊断 ZIP 与 Excel 删除;两次导入后限制保留 1 份,旧映射删除、新映射保留 |
| 匹配原因失真与非原子写入 | 区分精确路径、任务显示名、工程名称、迁移名称、唯一工程兜底;相对路径在规范化前拒绝;映射使用现有原子 `write_record` 和共享占用处理 | 各匹配原因断言;相对路径失败;模拟替换 JSON 失败后原内容完整、无遗留临时 JSON |
| 用户模板缺少 Value 来源列 | 有任务/无任务模板均导出第 12 列,提供全部五种策略下拉、样式与批注;底层旧写入接口默认保持 11 列 | 真实 openpyxl 检查两个模板、五种策略读入和编译、旧 11 列自定义值推断;空任务 UI 下载 |
| 两个操作之间释放占用,读源无进度 | `preview_bom_sources` 在任何本地源读取前进入监督进程;本地读取、路由、检查全程持有一个快速执行租约;读取前通知 READING,并逐文件报告名称与序号 | 在读取与检查内部尝试竞争租约,均被拒;监督入口禁止本地读源;逐文件进度;真实监督子进程端到端返回来源元数据与预览 |
| CI 未覆盖功能 | Linux 工作流安装 openpyxl,加入模板/策略契约与本轮来源导入回归用例,无 Qt/CAD 依赖 | 本机定向执行;远端 CI 状态另由 PR 检查项记录,不用 Windows 本机结果冒充 Linux 结果 |
同时修正监督清理失败的一个边界:业务失败报告由 `primary_error` 继承时,也必须清除其可执行预览。覆盖业务成功/失败 × 清理待处理/异常退出四种组合;报告保留,预览均为空。
## 本轮验证记录
本轮仅验证新增修复及直接相关功能,不重跑整个仓库,也不重复真实 CAD/PCB。原始真实替换验收仍见 `2026-11-05-item-value-bom-native.md`。
- 修复前先运行新复现文件:`test_bom_import_review.py` 原有 9 项全部失败;修复后原有 9 项全部通过,并补齐连续占用、名称冲突和匹配原因用例。当前该文件共 **14 个用例**。
- `python -m pytest -q tests/trial_task_center/test_bom_import_review.py tests/trial_task_center/test_item_value_and_bom_sources.py tests/v1_1/test_bom_sources_supervision.py --tb=short`:**44 passed**,涵盖 14 项审查回归、24 项既有策略/多来源验证、7 项监督验证(测试集合存在重叠,不累加)。
- 模板、旧 BOM、料号类型、CBB 和两个模板导出入口的定向命令:**34 passed**,其中新增模板契约 8 项。
- 监督协议、结果处理和新监督入口:**22 passed**。与上面的 44 项存在 6 项重叠,不累加。
- 报告、诊断包和历史保留相邻用例首轮:**37 passed、1 failed、1 error**。第一项 UI 用例仍拦截旧的检查接口,首轮异步等待超时,未结束的任务影响了后一项的占用检查;单独定位还发现按钮尚未启用就点击的问题。两项现已通过真实的新入口、使用无 CAD 的运行时替身,并明确等待按钮可用。
- 仅重跑上述两项 UI 及新增原始来源格式用例:**3 passed**(`--basetemp=build/pr21-ui-review-confirm`);已通过的相邻用例没有重复跑。
- 最后新增真实监督进程完整导入测试:`test_real_supervised_sources_roundtrip_origin_metadata_and_preview`:**1 passed**(`--basetemp=build/pr21-supervised-import-confirm`)。父进程调用真实服务,子进程读取两份 XLSX 和预先保存的库存,返回完整预览、原文件/行号和 `Path` 类型的映射路径;未打开 CAD,未保存新计划,临时 Excel 已删除。
- 独立只读代码复核覆盖 service/workspace/report/supervisor/diagnostics/retention/CI,未发现阻塞问题。
各条记录属于不同范围,存在重叠,不能相加作为独立总数。未构建安装包,未重复真实 CAD 替换,未执行 PCB。
+17 -2
View File
@@ -3371,14 +3371,29 @@ Function QPReadContinuation(ByVal Path)
End Function
Sub QPWriteUtf8(ByVal Path, ByVal Text)
Dim Stream
Dim Stream, Attempt, Limit, Number, Source, Description
Set Stream = CreateObject("ADODB.Stream")
Stream.Type = 2
Stream.Charset = "utf-8"
Stream.Open
Stream.WriteText CStr(Text)
Stream.SaveToFile CStr(Path), 2
Limit = 1
' The session driver polls this receipt; retry only its file write, never CAD.
If LCase(QP_Fso.GetFileName(CStr(Path))) = "project-session-result.tsv" Then Limit = 20
For Attempt = 1 To Limit
On Error Resume Next
Err.Clear
Stream.SaveToFile CStr(Path), 2
Number = Err.Number
Source = Err.Source
Description = Err.Description
Err.Clear
On Error GoTo 0
If Number = 0 Then Exit For
If Attempt < Limit Then WScript.Sleep 50
Next
Stream.Close
If Number <> 0 Then Err.Raise Number, Source, Description
End Sub
Sub QPAppendAscii(ByVal Path, ByVal Text)
@@ -35,7 +35,7 @@ def write_record(path, record):
json.dump(record, stream, ensure_ascii=False, indent=2)
stream.flush()
os.fsync(stream.fileno())
retry_sharing_violation(lambda: os.replace(temporary, path))
retry_sharing_violation(lambda: os.replace(temporary, path), target=path)
finally:
temporary.unlink(missing_ok=True)
@@ -82,15 +82,18 @@ def _propagate(store, task_id, before, after):
for field in ('new_part_number', 'sync_nc'):
if getattr(row, field) == getattr(prior, field):
changes[field] = getattr(rule, field)
if (row.custom_value_enabled, row.custom_value) == (prior.custom_value_enabled, prior.custom_value):
changes.update(custom_value_enabled=rule.custom_value_enabled, custom_value=rule.custom_value)
if (row.value_mode in {'inherit', 'custom'} and
(row.custom_value_enabled, row.custom_value) == (prior.custom_value_enabled, prior.custom_value)):
changes.update(custom_value_enabled=rule.custom_value_enabled, custom_value=rule.custom_value,
value_mode='custom' if rule.custom_value_enabled else 'inherit')
elif not row.new_part_number:
changes['new_part_number'] = rule.new_part_number
changes['target_cell'] = rule.target_cell
if row.sync_nc:
changes['sync_nc'] = rule.sync_nc
if not row.custom_value_enabled and not row.custom_value:
changes.update(custom_value_enabled=rule.custom_value_enabled, custom_value=rule.custom_value)
if row.value_mode == 'inherit' and not row.custom_value_enabled and not row.custom_value:
changes.update(custom_value_enabled=rule.custom_value_enabled, custom_value=rule.custom_value,
value_mode='custom' if rule.custom_value_enabled else 'inherit')
if (not changes.get('target_cell', row.target_cell)
and changes.get('new_part_number', row.new_part_number) == rule.new_part_number):
# Saved BOM/manual mappings can predate their explicit rule Cell.
@@ -89,7 +89,7 @@ def run(request_path):
from .stop_control import StopControl
stop = StopControl() if request.get('interactive_stop', False) else threading.Event()
finished, output_lock = threading.Event(), threading.Lock()
kind = {'inspect_bom_batch': 'bom_import', 'preview_bom_batch': 'bom_import',
kind = {'inspect_bom_batch': 'bom_import', 'preview_bom_batch': 'bom_import', 'preview_bom_sources': 'bom_import',
'execute_task_queue': 'task_queue', 'execute_quick_run_queue': 'quick_run_queue'}.get(request['operation'], request['operation'])
runtime = BatchCadRuntime(root, kind, stop, session_id=batch_id)
sequence = 0
@@ -156,9 +156,9 @@ def run(request_path):
elif operation == 'execute_task_queue':
from .task_queue import execute_task_queue
result = execute_task_queue(service, **arguments, stop_event=stop, runtime=runtime)
elif operation in {'inspect_bom_batch', 'preview_bom_batch'}:
elif operation in {'inspect_bom_batch', 'preview_bom_batch', 'preview_bom_sources'}:
controls = {'stop_event': stop, 'on_progress': progress, 'runtime': runtime}
if operation == 'inspect_bom_batch':
if operation in {'inspect_bom_batch', 'preview_bom_sources'}:
controls['on_phase'] = phase
result = getattr(service, operation)(**arguments, **controls)
elif operation == 'collect_target_inventory':
@@ -174,7 +174,7 @@ def run(request_path):
response.update(status='ok', business_succeeded=True, result=encode(result))
write_record(directory / 'business-result.json', {'version': VERSION, 'batch_id': batch_id,
'attempt_id': attempt_id, 'generation': max(1, runtime.generation), 'payload': response})
if operation == 'inspect_bom_batch':
if operation in {'inspect_bom_batch', 'preview_bom_sources'}:
from .bom_import_state import BomImportPhase
phase(BomImportPhase.RELEASING, '本批位号核验结束,正在释放后台资源…')
except Exception as error:
@@ -45,7 +45,7 @@ def _identity(scope):
def export_template(store, task_id, path):
records = () if task_id is None else tuple({'project_name': task.name, 'project_path': str(project.path),
'enabled': True, 'sync_nc': True} for task, project in _scope(store, task_id))
return write_bom_template(path, records)
return write_bom_template(path, records, include_value_mode=True)
def _assert_editable(service, task_id):
@@ -23,6 +23,9 @@ class BomRowResult:
project_name: str
status: str # passed, failed, unchecked
reason: str = ''
source_file: str = ''
source_row: int | None = None
duplicate_rows: tuple[int, ...] = ()
@dataclass(frozen=True)
@@ -39,6 +42,7 @@ class BomImportReport:
projects: tuple[BomProjectResult, ...]
preview: BomBatchPreview | None = None
errors: tuple[str, ...] = ()
source_map_path: Path | None = None
@property
def total_rows(self):
@@ -120,7 +124,8 @@ def _check_record_conflicts(rows, records, indices):
reason = '位号在 BOM 中重复(Excel 行 ' + ', '.join(
str(rows[item].excel_row) for item in dict.fromkeys(claimed)) + ')'
for index in claimed:
rows[index] = replace(rows[index], status='failed', reason=reason)
rows[index] = replace(rows[index], status='failed', reason=reason,
duplicate_rows=tuple(rows[item].excel_row for item in dict.fromkeys(claimed)))
def inspect_batch(service, task_id, path, *, stop_event=None, on_progress=None, on_phase=None, runtime=None):
@@ -253,6 +258,7 @@ def inspect_batch(service, task_id, path, *, stop_event=None, on_progress=None,
for position, (key, indices) in enumerate(grouped.items(), 1):
check_cancelled()
task, project = owners[key]
task_default_value_mode = 'part_label' if task.copy_part_number_to_value else 'preserve'
def progress(message, phase=None):
notify(f'工程 {position}/{len(grouped)} · {message}', phase)
progress(f'正在核验「{task.name}」的位号库存…', BomImportPhase.INVENTORY)
@@ -304,7 +310,7 @@ def inspect_batch(service, task_id, path, *, stop_event=None, on_progress=None,
if matcher is None:
raise TaskCenterError('target_inventory', '未采集到可匹配的工程位号')
matched = matcher.match_record(records[index])
compile_target_rules(tuple(matched.values()))
compile_target_rules(tuple(matched.values()), default_value_mode=task_default_value_mode)
for target_key, row in matched.items():
touched.setdefault(target_key, []).append(index)
matched_rows[target_key] = row
@@ -315,10 +321,11 @@ def inspect_batch(service, task_id, path, *, stop_event=None, on_progress=None,
if len(indices_for_target) > 1:
for index in indices_for_target:
rows[index] = replace(rows[index], status='failed', reason='位号在 BOM 中重复(Excel 行 ' +
', '.join(str(rows[item].excel_row) for item in indices_for_target) + ')')
', '.join(str(rows[item].excel_row) for item in indices_for_target) + ')',
duplicate_rows=tuple(rows[item].excel_row for item in indices_for_target))
combined = tuple(matched_rows.get(key, row) for key, row in matcher.by_key.items()) if matcher else ()
try:
compile_target_rules(combined)
compile_target_rules(combined, default_value_mode=task_default_value_mode)
except TaskCenterError as error:
for index in indices:
if rows[index].status == 'passed':
@@ -29,6 +29,8 @@ _HEADER_HELP = (
'是否替换:TRUE/FALSE 或 是/否;留空默认替换。未列出的位号保留。',
'自定义 Value,可选;留空沿用工具原有 Value 策略。',
'同步 NC:TRUE/FALSE 或 是/否;留空默认开启。',
'Value 来源:inherit 沿用任务策略,preserve 保留原值,part_label 使用 Part Label,'
'part_number 使用新料号,custom 使用自定义 Value 列;留空时有自定义值则使用 custom,否则 inherit。',
)
_REFDES_SPLIT = re.compile(r'[,,;;、\s]+')
_ALIASES = {
@@ -41,16 +43,19 @@ _ALIASES = {
'uid': 'uid', '替换': 'enabled', '是否替换': 'enabled', 'enabled': 'enabled',
'新value': 'custom_value', '自定义value': 'custom_value', 'custom_value': 'custom_value',
'同步nc': 'sync_nc', '同步_nc': 'sync_nc', 'sync_nc': 'sync_nc',
'value_mode': 'value_mode', 'value来源': 'value_mode', 'value策略': 'value_mode',
}
def write_bom_template(path: Path, records=()) -> Path:
def write_bom_template(path: Path, records=(), *, include_value_mode=False) -> Path:
records = tuple(records)
headers = BOM_HEADERS + (('value_mode',) if include_value_mode or any('value_mode' in record for record in records) else ())
workbook = Workbook()
sheet = workbook.active
sheet.title = 'BOM Replacement'
sheet.append(BOM_HEADERS)
sheet.append(headers)
for record in records:
sheet.append(tuple(record.get(header, '') for header in BOM_HEADERS))
sheet.append(tuple(record.get(header, '') for header in headers))
wrapped = Alignment(wrap_text=True, vertical='top')
for row in sheet.iter_rows():
for cell in row:
@@ -66,8 +71,12 @@ def write_bom_template(path: Path, records=()) -> Path:
validation = DataValidation(type='list', formula1='"TRUE,FALSE"', allow_blank=False)
sheet.add_data_validation(validation)
validation.add(f'{column}2:{column}{max(2, sheet.max_row + 500)}')
if 'value_mode' in headers:
validation = DataValidation(type='list', formula1='"inherit,preserve,part_label,part_number,custom"', allow_blank=False)
sheet.add_data_validation(validation)
validation.add(f'L2:L{max(2, sheet.max_row + 500)}')
for column, width in {'A': 30, 'B': 64, 'C': 22, 'D': 22, 'E': 28, 'F': 18, 'G': 14,
'H': 28, 'I': 12, 'J': 24, 'K': 12}.items():
'H': 28, 'I': 12, 'J': 24, 'K': 12, 'L': 16}.items():
sheet.column_dimensions[column].width = width
sheet.column_dimensions[column].alignment = wrapped
writable_last_row = max(2, sheet.max_row)
@@ -97,6 +106,7 @@ class BomRecord:
enabled: bool
custom: str
sync_nc: bool
value_mode: str = 'inherit'
def read_bom_records(path: Path) -> tuple[BomRecord, ...]:
@@ -151,13 +161,18 @@ def parse_bom_row(cells, positions):
enabled = _bool(_value(values, positions, 'enabled', True))
sync_nc = _bool(_value(values, positions, 'sync_nc', True))
custom = _text(values, positions, 'custom_value')
value_mode = _text(values, positions, 'value_mode') or ('custom' if custom else 'inherit')
if value_mode not in {'inherit', 'preserve', 'part_label', 'part_number', 'custom'}:
raise ValueError('Value 来源只能是 inherit、preserve、part_label、part_number 或 custom')
if value_mode == 'custom' and not custom:
raise ValueError('Value 来源为 custom 时必须填写自定义 Value')
schematic = _text(values, positions, 'schematic_name')
sheet_number = _text(values, positions, 'sheet_number')
uid = _text(values, positions, 'uid')
project = _text(values, positions, 'project_path')
if project and not Path(project).is_absolute():
raise ValueError('工程路径必须使用模板中的完整路径')
return BomRecord(project, old, new, refs, schematic, sheet_number, uid, enabled, custom, sync_nc)
return BomRecord(project, old, new, refs, schematic, sheet_number, uid, enabled, custom, sync_nc, value_mode)
class BomMatcher:
@@ -196,9 +211,10 @@ class BomMatcher:
if key in matched:
raise TaskCenterError('bom_import', f'位号 {refdes} 在 BOM 中重复')
matched[key] = TargetPlanRow(target, record.enabled, record.new,
bool(record.custom), record.custom, record.sync_nc,
record.value_mode == 'custom', record.custom, record.sync_nc,
(self.by_key[key].target_cell if self.by_key[key].new_part_number == record.new else '')
or self.cell_defaults.get((target.old_part_number.casefold(), record.new), ''))
or self.cell_defaults.get((target.old_part_number.casefold(), record.new), ''),
record.value_mode)
return matched
@@ -52,6 +52,9 @@ def _scrub_json(value):
def _allowed(parts):
if (len(parts) == 5 and parts[0] == 'tasks' and parts[2] == 'quick-run-history'
and re.fullmatch(r'bom-import-[A-Za-z0-9-]+', parts[3])):
return parts[-1] == 'source-map.json'
if len(parts) == 2 and parts[0] == 'logs':
return bool(re.fullmatch(r'[^/]+\.log(?:\.\d+)?', parts[-1]))
if len(parts) == 3 and parts[0] == 'batch-resources':
@@ -46,7 +46,7 @@ _TABLES={
'selected','available','status','summary')),
'target_plan':(('task_id','project_path','schematic_name','sheet_number','uid'),
('task_id','project_path','schematic_name','sheet_number','uid','refdes','old_part_number','old_value',
'enabled','new_part_number','custom_value_enabled','custom_value','sync_nc','target_cell')),
'enabled','new_part_number','custom_value_enabled','custom_value','sync_nc','target_cell','value_mode')),
}
_CUTOFF='history_import_cutoff_v1'
_IMPORTED='history_import_done_v1'
@@ -81,7 +81,9 @@ def _stage_new_round(service, task_id, transition):
for member in members:
facade = service._project_task_service(member.task_id)
root = facade._quick_run_preparer.data_root / 'quick-run'
rules = (compile_target_rules(store.list_target_plan(member.task_id))
rules = (compile_target_rules(
store.list_target_plan(member.task_id),
default_value_mode=('part_label' if member.copy_part_number_to_value else 'preserve'))
if member.target_plan_mode else store.list_rules(member.task_id))
snapshot = facade._quick_run_preparer.read_snapshot(member.task_id,
store.list_projects(member.task_id), rules,
@@ -1999,7 +1999,10 @@ def _rule_command_options(rule: RuleRecord, base_value_mode: str) -> tuple[str,
sync_nc = bool(getattr(rule, "sync_nc", True))
if bool(getattr(rule, "custom_value_enabled", False)):
return "custom", str(getattr(rule, "custom_value", "")), sync_nc, base_value_mode
return base_value_mode, "", sync_nc, base_value_mode
item_mode = getattr(rule, "value_mode", base_value_mode)
if item_mode not in {"preserve", "part_label", "part_number"}:
raise TaskCenterError("value_option", "queue Value mode is invalid")
return item_mode, "", sync_nc, base_value_mode
def _command_text(
@@ -16,7 +16,7 @@ HISTORY_MAX_AGE_DAYS = 30
HISTORY_MAX_ENTRIES = 100
HISTORY_MAX_BYTES = 5 * 1024 * 1024 * 1024
HISTORY_MIN_ENTRIES = 10
_HISTORY_NAME = re.compile(r"(?:batch|recovery|sheet-discovery|obsolete)-[A-Za-z0-9-]+\Z")
_HISTORY_NAME = re.compile(r"(?:batch|recovery|sheet-discovery|obsolete|bom-import)-[A-Za-z0-9-]+\Z")
@dataclass(frozen=True, slots=True)
@@ -5,6 +5,7 @@ from __future__ import annotations
from collections.abc import Callable
from contextlib import contextmanager, ExitStack
from datetime import datetime
from dataclasses import replace
import os
from pathlib import Path
import threading
@@ -194,6 +195,83 @@ class TaskCenterService:
lambda: preview_batch(self, task_id, path, **control_options(stop_event, on_progress),
**({'runtime': runtime} if runtime is not None else {})))
def preview_bom_sources(self, task_id: str, sources, *, stop_event=None, on_progress=None,
on_phase=None, runtime=None):
"""Normalize several BOM files and exact-name migrated project paths before CAD reads."""
sources = tuple(Path(source) for source in sources)
if runtime is None and self._supervised:
return self._supervise('preview_bom_sources', {'task_id': task_id, 'sources': sources},
stop_event=stop_event, on_progress=on_progress, on_phase=on_phase)
from .bom_batch import _scope
from .bom_import_report import BomImportReport, inspect_batch
from .bom_import_state import BomImportPhase
from .retention import maintain_quick_run_storage
from .workspace_bom import load_sources, route_sources, write_routed_bom
from uuid import uuid4
def preview():
message = '正在读取 BOM 来源表格…'
if on_phase is not None:
on_phase(BomImportPhase.READING, message)
if on_progress is not None:
on_progress(message)
rows = load_sources(sources, stop_event=stop_event, on_progress=on_progress)
scope = _scope(self._store, task_id)
paths = tuple(project.path for _, project in scope)
names = {project.path: task.name for task, project in scope}
routes = route_sources(rows, paths, single=len(paths) == 1, project_names=names)
unresolved = [(row.source.name, row.excel_row, row.project_name or row.project_path)
for row in rows if routes.get(row.project_key) is None]
if unresolved:
details = '; '.join(f'{name} 第 {number} 行:{key}' for name, number, key in unresolved[:20])
raise TaskCenterError('project_binding', f'无法唯一匹配 BOM 工程,请确认迁移工程对应关系:{details}')
task_root = self._logging.root / 'tasks' / task_id
maintain_quick_run_storage(task_root)
root = task_root / 'quick-run-history' / ('bom-import-' + uuid4().hex)
normalized = root / 'normalized-bom.xlsx'
mapping = root / 'source-map.json'
def annotate(report):
if report is None:
return report
annotated = []
for row in report.rows:
if not 2 <= row.excel_row <= len(rows) + 1:
annotated.append(row)
continue
source = rows[row.excel_row - 2]
reason = row.reason
if row.duplicate_rows:
locations = (f'{rows[number - 2].source.name} 第 {rows[number - 2].excel_row} 行'
for number in row.duplicate_rows)
reason = '位号在 BOM 中重复(' + ';'.join(locations) + ')'
annotated.append(replace(row, source_file=str(source.source), source_row=source.excel_row,
reason=reason + f';来源:{source.source} 第 {source.excel_row} 行'))
return replace(report, rows=tuple(annotated), source_map_path=mapping)
def inspection_phase(phase, message):
if on_phase is not None and phase is not BomImportPhase.READING:
on_phase(phase, message)
try:
write_routed_bom(normalized, rows, routes, project_names=names)
append_task_event(self._logging, level='INFO', task_id=task_id,
operation='preview_bom_sources', message='BOM 导入来源记录已保存',
details={'source_map_path': str(mapping), 'source_count': len({row.source for row in rows})})
report = inspect_batch(self, task_id, normalized, stop_event=stop_event,
on_progress=on_progress, on_phase=inspection_phase, runtime=runtime)
return annotate(report)
except TaskCenterError as error:
if getattr(error, 'bom_report', None) is not None:
error.bom_report = annotate(error.bom_report)
if isinstance(getattr(error, 'completed_result', None), BomImportReport):
error.completed_result = annotate(error.completed_result)
raise
finally:
normalized.unlink(missing_ok=True)
return self._idle_quick_run_operation('preview_bom_sources', task_id, preview)
def save_bom_batch(self, preview, rows_by_task):
from .bom_batch import save_batch
return self._idle_quick_run_operation('save_bom_batch', preview.anchor_task_id,
@@ -298,7 +376,7 @@ class TaskCenterService:
raise TaskCenterError("project_binding", "bound task must own exactly one project")
rules, targets = (bundle.batch_rules if bundle.batch_rules is not None else bundle.rules), bundle.targets
facade = self._facade_for_task_record(task)
effective_rules = compile_target_rules(tuple(targets)) if task.target_plan_mode else rules
effective_rules = (compile_target_rules(tuple(targets), default_value_mode=("part_label" if task.copy_part_number_to_value else "preserve")) if task.target_plan_mode else rules)
snapshot = facade._quick_run_preparer.read_snapshot(task_id, projects, effective_rules,
copy_part_number_to_value=task.copy_part_number_to_value, read_control=read_control)
progress = snapshot.progress
@@ -350,7 +428,7 @@ class TaskCenterService:
task = self._store.get_task(task_id)
if task.target_plan_mode:
rows = self._store.list_target_plan(task_id)
rules = compile_target_rules(tuple(rows))
rules = compile_target_rules(tuple(rows), default_value_mode=("part_label" if task.copy_part_number_to_value else "preserve"))
if not rules:
raise TaskCenterError("enabled_rule_required", "target plan has no enabled targets")
return rules
@@ -14,7 +14,8 @@ from .target_plan import TargetPlanRow, compile_target_rules
HEADERS = ("project_path", "project_name", "schematic_name", "sheet_number", "uid",
"refdes", "old_part_number", "old_value", "enabled", "new_part_number",
"custom_value_enabled", "custom_value", "sync_nc")
"custom_value_enabled", "custom_value", "sync_nc", "value_mode")
LEGACY_HEADERS = HEADERS[:-1]
def export_target_plan(store, task_id: str, path: Path) -> Path:
@@ -28,7 +29,7 @@ def export_target_plan(store, task_id: str, path: Path) -> Path:
values = (str(target.project_path), target.project_path.name, target.schematic_name,
target.sheet_number, target.uid, target.refdes, target.old_part_number,
target.old_value, row.enabled, row.new_part_number, row.custom_value_enabled,
row.custom_value, row.sync_nc)
row.custom_value, row.sync_nc, row.value_mode)
sheet.append(values)
for cell in sheet[sheet.max_row]:
if isinstance(cell.value, str):
@@ -44,7 +45,7 @@ def export_target_plan(store, task_id: str, path: Path) -> Path:
check.add(f"{column}2:{column}{max(sheet.max_row, 2)}")
for column, width in {"A": 45, "B": 22, "C": 20, "D": 12, "E": 26,
"F": 14, "G": 20, "H": 20, "I": 12, "J": 20,
"K": 22, "L": 20, "M": 12}.items():
"K": 22, "L": 20, "M": 12, "N": 16}.items():
sheet.column_dimensions[column].width = width
path = Path(path)
workbook.save(path)
@@ -66,14 +67,16 @@ def read_target_plan(store, task_id: str, path: Path) -> tuple[TargetPlanRow, ..
if len(workbook.worksheets) != 1:
raise ValueError("workbook must have exactly one plan sheet")
records = list(workbook.active.iter_rows(values_only=False))
if not records or tuple(cell.value for cell in records[0]) != HEADERS:
header = tuple(cell.value for cell in records[0]) if records else ()
if header not in {HEADERS, LEGACY_HEADERS}:
raise ValueError("plan columns changed")
legacy = header == LEGACY_HEADERS
by_key: dict[tuple[str, str, str, str], TargetPlanRow] = {}
for cells in records[1:]:
values = tuple(cell.value for cell in cells)
if all(value is None for value in values):
continue
if len(values) != len(HEADERS) or any(cell.data_type == "f" for cell in cells):
if len(values) != len(header) or any(cell.data_type == "f" for cell in cells):
raise ValueError("invalid or formula plan row")
identity = tuple("" if value is None else str(value) for value in values[:8])
key = (str(Path(identity[0]).resolve(strict=False)).casefold(),
@@ -90,13 +93,15 @@ def read_target_plan(store, task_id: str, path: Path) -> tuple[TargetPlanRow, ..
enabled = _excel_bool(enabled)
custom_enabled = _excel_bool(custom_enabled)
sync_nc = _excel_bool(sync_nc)
value_mode = 'custom' if custom_enabled else ('inherit' if legacy else str(values[13] or 'inherit'))
if not isinstance(new_part, str) and new_part is not None:
raise ValueError("new Part Number must be text")
if not isinstance(custom_value, str) and custom_value is not None:
raise ValueError("custom Value must be text")
by_key[key] = TargetPlanRow(target, enabled, new_part or "", custom_enabled,
custom_value or "", sync_nc,
original.target_cell if original.new_part_number == (new_part or '') else '')
original.target_cell if original.new_part_number == (new_part or '') else '',
value_mode)
if set(by_key) != set(expected):
raise ValueError("plan rows are missing")
result = tuple(by_key.values())
@@ -84,7 +84,7 @@ def read_task_bundle(store, task_id: str, read_control=None) -> TaskBundle:
row['uid'], row['refdes'], row['old_part_number'], row['old_value']),
bool(row['enabled']), row['new_part_number'], bool(row['custom_value_enabled']),
row['custom_value'], bool(row['sync_nc']),
row['target_cell'],
row['target_cell'], value_mode=row['value_mode'],
)
targets = records(
"SELECT * FROM target_plan WHERE task_id = ? ORDER BY project_path, schematic_name, sheet_number, refdes, uid",
@@ -238,6 +238,8 @@ class StoreSchema:
columns = {row['name'] for row in self._store._connection.execute('PRAGMA table_info(target_plan)')}
if 'target_cell' not in columns:
self._store._connection.execute("ALTER TABLE target_plan ADD COLUMN target_cell TEXT NOT NULL DEFAULT ''")
if 'value_mode' not in columns:
self._store._connection.execute("ALTER TABLE target_plan ADD COLUMN value_mode TEXT NOT NULL DEFAULT 'inherit'")
def _ensure_rule_columns(self) -> None:
columns = {row["name"] for row in self._store._connection.execute("PRAGMA table_info(rules)")}
@@ -86,11 +86,11 @@ class TargetPlanRepository:
self._store._connection.execute("DELETE FROM target_plan WHERE task_id = ?", (task_id,))
for row in rows:
target = row.target
self._store._connection.execute("""INSERT INTO target_plan VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
self._store._connection.execute("""INSERT INTO target_plan VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
(task_id, identities.project(target.project_path), target.schematic_name,
target.sheet_number, target.uid, target.refdes, target.old_part_number, target.old_value,
int(row.enabled), row.new_part_number, int(row.custom_value_enabled), row.custom_value,
int(row.sync_nc), row.target_cell))
int(row.sync_nc), row.target_cell, row.value_mode))
self._store._connection.execute("UPDATE tasks SET target_plan_mode = 1 WHERE task_id = ?", (task_id,))
return self._store.list_target_plan(task_id)
@@ -115,7 +115,7 @@ class TargetPlanRepository:
raise TaskCenterError("target_identity", "inventory project is not in task")
for row in rows:
target = row.target
self._store._connection.execute("""INSERT INTO target_plan VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, '', 0, '', 1, '')""",
self._store._connection.execute("""INSERT INTO target_plan VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, '', 0, '', 1, '', 'inherit')""",
(task_id, identities.project(target.project_path), target.schematic_name,
target.sheet_number, target.uid, target.refdes, target.old_part_number, target.old_value))
self._store._connection.execute("UPDATE tasks SET target_plan_mode = 1 WHERE task_id = ?", (task_id,))
@@ -130,6 +130,6 @@ class TargetPlanRepository:
ComponentTarget(Path(row["project_path"]), row["schematic_name"], row["sheet_number"],
row["uid"], row["refdes"], row["old_part_number"], row["old_value"]),
bool(row["enabled"]), row["new_part_number"], bool(row["custom_value_enabled"]),
row["custom_value"], bool(row["sync_nc"]), row['target_cell']) for row in rows)
row["custom_value"], bool(row["sync_nc"]), row['target_cell'], row['value_mode']) for row in rows)
@@ -11,7 +11,7 @@ import re
from .errors import TaskCenterError
VERSION = 1
OPERATIONS = frozenset({'probe', 'runtime_smoke', 'inspect_bom_batch', 'preview_bom_batch',
OPERATIONS = frozenset({'probe', 'runtime_smoke', 'inspect_bom_batch', 'preview_bom_batch', 'preview_bom_sources',
'collect_target_inventory', 'execute_task_queue', 'execute_quick_run_queue', 'execute_quick_run',
'prepare_quick_run', 'prepare_failed_quick_run', 'recover_quick_run', 'cleanup_batch_resources',
'target_cell_choices'})
@@ -13,7 +13,8 @@ def validated_outcome(operation, body):
or body.get('status') not in {'ok', 'error'}):
raise ValueError('Invalid supervisor business outcome')
result = decode(body['result']) if body['business_succeeded'] else None
expected = {'inspect_bom_batch': BomImportReport, 'preview_bom_batch': BomBatchPreview,
expected = {'inspect_bom_batch': BomImportReport, 'preview_bom_sources': BomImportReport,
'preview_bom_batch': BomBatchPreview,
'execute_task_queue': TaskQueueResult, 'execute_quick_run_queue': BatchRunResult,
'execute_quick_run': QuickRunStepResult, 'recover_quick_run': QuickRunStepResult,
'prepare_quick_run': QuickRunResult, 'prepare_failed_quick_run': QuickRunProgress,
@@ -43,10 +44,11 @@ def resource_pending_error(operation, result, error, primary_error=None):
result = replace(result, preview=None)
failure = BatchResourceCleanupError('后台资源仍待清理;报告和已保存核验结果保留。' + (str(error) if error else ''),
completed_result=result, primary_error=primary_error, cleanup_error=error)
if isinstance(result, BomImportReport):
failure.bom_report = result
elif getattr(error, 'bom_report', None) is not None:
failure.bom_report = replace(error.bom_report, preview=None)
for candidate in (result, getattr(error, 'bom_report', None),
getattr(primary_error, 'bom_report', None)):
if isinstance(candidate, BomImportReport):
failure.bom_report = replace(candidate, preview=None)
break
return failure
@@ -54,13 +54,20 @@ class TargetPlanRow:
custom_value: str = ""
sync_nc: bool = True
target_cell: str = ""
value_mode: str = "inherit"
def __post_init__(self) -> None:
if any(type(value) is not bool for value in (self.enabled, self.custom_value_enabled, self.sync_nc)):
raise TaskCenterError("target_plan", "plan switches must be Boolean")
if any(not isinstance(value, str) for value in (self.new_part_number, self.custom_value, self.target_cell)):
if any(not isinstance(value, str) for value in (self.new_part_number, self.custom_value, self.target_cell, self.value_mode)):
raise TaskCenterError("target_plan", "Part Number and Value must be text")
if any(character in value for value in (self.new_part_number, self.custom_value, self.target_cell)
if self.value_mode not in {"inherit", "preserve", "part_label", "part_number", "custom"}:
raise TaskCenterError("value_option", "Value mode must be inherit, preserve, part_label, part_number, or custom")
if self.custom_value_enabled and self.value_mode != "custom":
object.__setattr__(self, "value_mode", "custom")
if self.value_mode == "custom":
object.__setattr__(self, "custom_value_enabled", True)
if any(character in value for value in (self.new_part_number, self.custom_value, self.target_cell, self.value_mode)
for character in "\t\r\n"):
raise TaskCenterError("target_plan", "Part Number and Value cannot contain TSV controls")
@@ -79,16 +86,21 @@ class TargetRule:
position: int = 0
scope: str = "targets"
target_cell: str = ""
value_mode: str = "preserve"
def __post_init__(self) -> None:
if not self.targets:
raise TaskCenterError("empty_target_scope", "target rule needs at least one component")
if any(target.old_part_number.casefold() != self.old_part_number.casefold() for target in self.targets):
raise TaskCenterError("target_identity", "target old Part Number differs from rule")
if self.value_mode not in {"preserve", "part_label", "part_number", "custom"}:
raise TaskCenterError("value_option", "compiled Value mode must be concrete")
def compile_target_rules(rows: tuple[TargetPlanRow, ...]) -> tuple[TargetRule, ...]:
def compile_target_rules(rows: tuple[TargetPlanRow, ...], *, default_value_mode: str = "preserve") -> tuple[TargetRule, ...]:
"""Group selected rows by sheet and replacement options, retaining UID scope."""
if default_value_mode not in {"preserve", "part_label", "part_number"}:
raise TaskCenterError("value_option", "task default Value mode is invalid")
seen: set[tuple[str, str, str, str]] = set()
grouped: dict[tuple, list[ComponentTarget]] = {}
old_parts: set[str] = set()
@@ -109,15 +121,21 @@ def compile_target_rules(rows: tuple[TargetPlanRow, ...]) -> tuple[TargetRule, .
old = target.old_part_number.strip()
if not new or old.casefold() == new.casefold():
raise TaskCenterError("mapping_rule", "source and target Part Numbers must differ")
if row.custom_value_enabled and not row.custom_value:
value_mode = "custom" if row.custom_value_enabled else row.value_mode
if value_mode == "inherit":
value_mode = default_value_mode
if value_mode not in {"preserve", "part_label", "part_number", "custom"}:
raise TaskCenterError("value_option", "Value mode is invalid")
if value_mode == "custom" and not row.custom_value:
raise TaskCenterError("value_option", "custom Value is empty")
old_parts.add(old.casefold())
new_parts.add(new.casefold())
key = (target_key[0], target.schematic_name,
target.sheet_number, old, new, row.custom_value_enabled,
row.custom_value if row.custom_value_enabled else "", row.sync_nc, row.target_cell)
target.sheet_number, old, new, value_mode,
row.custom_value if value_mode == "custom" else "", row.sync_nc, row.target_cell)
grouped.setdefault(key, []).append(target)
if old_parts & new_parts:
raise TaskCenterError("mapping_chain", "chain or cycle replacement is not allowed")
return tuple(TargetRule(key[3], key[4], tuple(targets), key[5], key[6], key[7],
position=index, target_cell=key[8]) for index, (key, targets) in enumerate(grouped.items()))
return tuple(TargetRule(key[3], key[4], tuple(targets), key[5] == "custom", key[6], key[7],
position=index, target_cell=key[8], value_mode=key[5])
for index, (key, targets) in enumerate(grouped.items()))
@@ -637,12 +637,12 @@ class ProjectPlanPanel(QWidget):
def import_bom(self):
if not self.task_id:
return
path, _ = QFileDialog.getOpenFileName(self, '导入 BOM 模板', '', 'BOM Excel (*.xlsx)')
if path:
paths, _ = QFileDialog.getOpenFileNames(self, '导入 BOM 模板(可多选)', '', 'BOM Excel (*.xlsx)')
if paths:
task_id = self.task_id
scope = self._report_scope(task_id)
self._dispatch_cancellable(lambda stop, progress, phase: self.window.service.inspect_bom_batch(
task_id, Path(path), stop_event=stop, on_progress=progress, on_phase=phase),
self._dispatch_cancellable(lambda stop, progress, phase: self.window.service.preview_bom_sources(
task_id, tuple(Path(path) for path in paths), stop_event=stop, on_progress=progress, on_phase=phase),
lambda report: self._received_bom_report(report, scope=scope), self.import_bom_button,
bom_scope=scope)
@@ -697,7 +697,10 @@ class ProjectPlanPanel(QWidget):
f'共 {report.total_projects} 个工程,通过 {report.passed_projects} 个、'
f'失败 {report.failed_projects} 个、未检查 {report.unchecked_projects} 个。')
details = [summary, *report.errors]
details.extend(f'Excel 第 {row.excel_row} 行 · {row.project_name or "未指定工程"} '
if report.source_map_path is not None:
details.append(f'导入来源记录:{report.source_map_path}')
details.extend(f'{Path(row.source_file).name + " · " if row.source_file else ""}'
f'Excel 第 {row.source_row if row.source_row is not None else row.excel_row} 行 · {row.project_name or "未指定工程"} '
f'({row.project_path or "无工程路径"})· '
f'{ {"passed": "通过", "failed": "失败", "unchecked": "未检查"}[row.status]}:{row.reason}'
for row in report.rows)
@@ -1,11 +1,13 @@
"""Model-based editable target plan; filtering never changes saved identities."""
from dataclasses import replace
from PySide6.QtCore import QAbstractTableModel, QModelIndex, QSortFilterProxyModel, Qt, Signal,QPersistentModelIndex
from PySide6.QtWidgets import QAbstractItemView, QHeaderView, QTableView,QLabel,QStyledItemDelegate,QLineEdit,QAbstractItemDelegate
from PySide6.QtWidgets import QAbstractItemView, QHeaderView, QTableView,QLabel,QStyledItemDelegate,QLineEdit,QAbstractItemDelegate,QComboBox
HEADERS = ('替换', '原理图', '图页', '位号', '旧料号', '原 Value', '新料号', '自定义 Value', '自定义内容', '同步 NC')
HEADERS = ('替换', '原理图', '图页', '位号', '旧料号', '原 Value', '新料号', '自定义 Value', '自定义内容', '同步 NC', 'Value 来源')
CHECK_FIELDS = {0: 'enabled', 7: 'custom_value_enabled', 9: 'sync_nc'}
TEXT_FIELDS = {6: 'new_part_number', 8: 'custom_value'}
VALUE_MODE_COLUMN = 10
VALUE_MODES = ('inherit', 'preserve', 'part_label', 'part_number', 'custom')
class TargetPlanModel(QAbstractTableModel):
changed = Signal()
@@ -38,7 +40,8 @@ class TargetPlanModel(QAbstractTableModel):
'原理图名称,只读。','图页编号,只读。','器件位号,只读;完整 UID 在单元格提示中。',
'当前工程的原料号,只读。','原始 Value,只读。','可编辑的新料号;修改后需要保存计划。',
'启用本行自定义 Value,优先于工程级 Value 策略。',
'本行自定义 Value 内容。','按本行配置是否同步 NC。')[section]
'本行自定义 Value 内容。','按本行配置是否同步 NC。',
'inherit 跟随任务默认;其余选项显式指定本行的 Value 来源。')[section]
def data(self, index, role=Qt.ItemDataRole.DisplayRole):
if not index.isValid():
return None
@@ -49,7 +52,7 @@ class TargetPlanModel(QAbstractTableModel):
return Qt.CheckState.Checked if getattr(row, CHECK_FIELDS[col]) else Qt.CheckState.Unchecked
if role in (Qt.ItemDataRole.DisplayRole, Qt.ItemDataRole.EditRole):
return ('', t.schematic_name, t.sheet_number, t.refdes, t.old_part_number,
t.old_value, row.new_part_number, '', row.custom_value, '')[col]
t.old_value, row.new_part_number, '', row.custom_value, '', row.value_mode)[col]
if role == Qt.ItemDataRole.ToolTipRole:
content=self.data(index,Qt.ItemDataRole.DisplayRole)
if col in CHECK_FIELDS:content='开启'if getattr(row,CHECK_FIELDS[col])else'关闭'
@@ -62,7 +65,7 @@ class TargetPlanModel(QAbstractTableModel):
if self.editable:
if index.column() in CHECK_FIELDS:
flags |= Qt.ItemFlag.ItemIsUserCheckable
elif index.column() in TEXT_FIELDS:
elif index.column() in TEXT_FIELDS or index.column() == VALUE_MODE_COLUMN:
flags |= Qt.ItemFlag.ItemIsEditable
return flags
def setData(self, index, value, role=Qt.ItemDataRole.EditRole):
@@ -71,8 +74,13 @@ class TargetPlanModel(QAbstractTableModel):
col = index.column()
if role == Qt.ItemDataRole.CheckStateRole and col in CHECK_FIELDS:
change = {CHECK_FIELDS[col]: value in (Qt.CheckState.Checked, Qt.CheckState.Checked.value)}
if col == 7:
change['value_mode'] = 'custom' if change['custom_value_enabled'] else 'inherit'
elif role == Qt.ItemDataRole.EditRole and col in TEXT_FIELDS:
change = {TEXT_FIELDS[col]: str(value).strip()}
elif role == Qt.ItemDataRole.EditRole and col == VALUE_MODE_COLUMN:
mode = str(value).strip()
change = {'value_mode': mode, 'custom_value_enabled': mode == 'custom'}
else:
return False
number = index.row()
@@ -84,7 +92,11 @@ class TargetPlanModel(QAbstractTableModel):
self.enabled_count += int(updated.enabled) - int(self.rows[number].enabled)
self.rows[number] = updated
self.search_text[number] = self.searchable(updated)
self.dataChanged.emit(index, index, [role, Qt.ItemDataRole.DisplayRole])
roles = [role, Qt.ItemDataRole.DisplayRole]
if 'custom_value_enabled' in change:
roles.append(Qt.ItemDataRole.CheckStateRole)
first, last = (self.index(number, 7), self.index(number, VALUE_MODE_COLUMN)) if 'value_mode' in change else (index, index)
self.dataChanged.emit(first, last, roles)
self.changed.emit()
return True
@@ -92,10 +104,14 @@ class TargetPlanModel(QAbstractTableModel):
"""Notify the view and controller once for a bulk operation."""
if not self.editable:
return
field = CHECK_FIELDS[column] if role == Qt.ItemDataRole.CheckStateRole else TEXT_FIELDS[column]
field = CHECK_FIELDS[column] if role == Qt.ItemDataRole.CheckStateRole else ('value_mode' if column == VALUE_MODE_COLUMN else TEXT_FIELDS[column])
changed = []
for number in numbers:
changes = {field: value}
if field == 'value_mode':
changes['custom_value_enabled'] = str(value).strip() == 'custom'
elif field == 'custom_value_enabled':
changes['value_mode'] = 'custom' if value else 'inherit'
if field == 'new_part_number' and value != self.rows[number].new_part_number:
changes['target_cell'] = ''
updated = replace(self.rows[number], **changes)
@@ -105,8 +121,11 @@ class TargetPlanModel(QAbstractTableModel):
self.search_text[number] = self.searchable(updated)
changed.append(number)
if changed:
self.dataChanged.emit(self.index(min(changed), column), self.index(max(changed), column),
[role, Qt.ItemDataRole.DisplayRole])
roles = [role, Qt.ItemDataRole.DisplayRole]
if field in {'value_mode', 'custom_value_enabled'}:
roles.append(Qt.ItemDataRole.CheckStateRole)
first_col, last_col = (7, VALUE_MODE_COLUMN) if field in {'value_mode', 'custom_value_enabled'} else (column, column)
self.dataChanged.emit(self.index(min(changed), first_col), self.index(max(changed), last_col), roles)
self.changed.emit()
class TargetFilter(QSortFilterProxyModel):
@@ -121,6 +140,13 @@ class TargetFilter(QSortFilterProxyModel):
class PlanEditorDelegate(QStyledItemDelegate):
"""Expose uncommitted editor text without changing Escape/cancel semantics."""
def createEditor(self,parent,option,index):
if index.column() == VALUE_MODE_COLUMN:
editor = QComboBox(parent)
editor.addItems(VALUE_MODES)
view = self.parent()
view._active_editor = (editor, QPersistentModelIndex(index))
editor.currentTextChanged.connect(lambda _: view.editor_activity.emit())
return editor
editor=super().createEditor(parent,option,index)
if isinstance(editor,QLineEdit):
view=self.parent()
@@ -169,7 +195,7 @@ class TargetTable(QTableView):
header.setStretchLastSection(False)
header.setMinimumSectionSize(48)
header.setDefaultAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
for col, width in enumerate((55, 90, 50, 70, 145, 115, 145, 90, 125, 75)):
for col, width in enumerate((55, 90, 50, 70, 145, 115, 145, 90, 125, 75, 105)):
self.setColumnWidth(col, width)
self.dirty = False
self._next_match = -1
@@ -191,7 +217,8 @@ class TargetTable(QTableView):
def has_pending_editor(self):
if not self._active_editor:return False
editor,index=self._active_editor
return index.isValid()and editor.isVisible()and editor.text()!=str(index.data(Qt.ItemDataRole.EditRole)or'')
value = editor.currentText() if isinstance(editor, QComboBox) else editor.text()
return index.isValid()and editor.isVisible()and value!=str(index.data(Qt.ItemDataRole.EditRole)or'')
def cancel_editor(self):
if self._active_editor:
self.itemDelegate().closeEditor.emit(self._active_editor[0],QAbstractItemDelegate.EndEditHint.RevertModelCache)
@@ -0,0 +1,204 @@
"""Safe multi-file BOM loading and project routing for workspace imports."""
from dataclasses import dataclass
from pathlib import Path, PureWindowsPath
from openpyxl import load_workbook
from .bom_plan import BOM_HEADERS, _normal_headers, _part_number_text, write_bom_template
from .errors import TaskCenterError
def _path_key(value) -> str:
return str(value or '').replace('\\', '/').strip().rstrip('/').casefold()
def _project_name(value) -> str:
raw = _path_key(value)
return raw.rsplit('/', 1)[-1]
@dataclass(frozen=True)
class SourceRow:
source: Path
excel_row: int
values: tuple
value_mode: str = 'inherit'
@property
def project_name(self):
return str(self.values[0] or '').strip()
@property
def project_path(self):
return str(self.values[1] or '').strip()
@property
def project_key(self):
return (_path_key(self.project_path or self.source), _path_key(self.project_name))
@property
def refdes(self):
return str(self.values[4] or '').strip()
def _selected_xlsx(sources):
paths = set()
for source in sources:
source = Path(source)
candidates = source.rglob('*.xlsx') if source.is_dir() else (source,)
for path in candidates:
if path.suffix.casefold() == '.xlsx' and not path.name.startswith('~$'):
paths.add(path.resolve())
return tuple(sorted(paths, key=lambda path: str(path).casefold()))
def load_sources(sources, *, stop_event=None, on_progress=None):
"""Read every selected workbook before any CAD operation starts."""
from .operation_control import check_cancelled
paths = _selected_xlsx(sources)
if not paths:
raise TaskCenterError('bom_import', '未找到可导入的 .xlsx 替换清单')
result = []
for number, path in enumerate(paths, 1):
check_cancelled(stop_event)
if on_progress is not None:
on_progress(f'正在读取 BOM {number}/{len(paths)}:{path.name}')
workbook = None
try:
workbook = load_workbook(path, read_only=True, data_only=False)
if len(workbook.worksheets) != 1:
raise ValueError('BOM 只能包含一个工作表')
rows = iter(workbook.active.iter_rows())
header_cells = next(rows, ())
headers = _normal_headers(tuple(cell.value for cell in header_cells))
names = tuple(name for name in headers if name)
if len(names) != len(set(names)):
raise ValueError('BOM 列名重复')
required = {'old_part_number', 'new_part_number', 'refdes'}
if not required <= set(names):
raise ValueError('BOM 必须包含旧料号、新料号和位号列')
if any(cell.data_type == 'f' for cell in header_cells):
raise ValueError('BOM 表头不接受公式')
positions = {name: index for index, name in enumerate(headers) if name}
for excel_row, cells in enumerate(rows, 2):
check_cancelled(stop_event)
if any(cell.data_type == 'f' for cell in cells):
raise ValueError(f'Excel 第 {excel_row} 行不接受公式')
values = tuple(
cells[positions[name]].value if name in positions and positions[name] < len(cells) else ''
for name in BOM_HEADERS
)
if not any(values[index] not in (None, '') for index in (2, 3, 4, 9)):
continue
project_path = str(values[1] or '').strip()
if project_path and not (Path(project_path).is_absolute() or PureWindowsPath(project_path).is_absolute()):
raise ValueError(f'Excel 第 {excel_row} 行工程路径必须是完整路径')
try:
_part_number_text(cells, positions, 'old_part_number', '旧料号')
_part_number_text(cells, positions, 'new_part_number', '新料号')
except ValueError as error:
raise ValueError(f'Excel 第 {excel_row} 行:{error}') from error
default_mode = 'custom' if str(values[9] or '').strip() else 'inherit'
value_mode = str(cells[positions['value_mode']].value or default_mode).strip() if 'value_mode' in positions else default_mode
if value_mode not in {'inherit', 'preserve', 'part_label', 'part_number', 'custom'}:
raise ValueError(f'Excel 第 {excel_row} 行 Value 来源无效')
result.append(SourceRow(path, excel_row, values, value_mode))
except TaskCenterError:
raise
except Exception as error:
raise TaskCenterError('bom_import', f'{path.name}:{error}') from error
finally:
if workbook is not None:
workbook.close()
if not result:
raise TaskCenterError('bom_import', '替换清单尚未填写旧料号、新料号和位号')
return tuple(result)
def _exact_name_matches(project_name, paths):
name = _project_name(project_name)
if not name:
return ()
return tuple(path for path in paths if _project_name(path) == name)
def resolve_project(project_path, project_name, paths, *, project_names=None):
"""Resolve exact path first, then an exact unique migrated project name."""
paths = tuple(Path(path) for path in paths)
raw = _path_key(project_path)
exact_path = tuple(path for path in paths if _path_key(path.resolve()) == raw) if raw else ()
if len(exact_path) == 1:
return exact_path[0]
if project_name:
aliases = tuple(path for path in paths
if _path_key((project_names or {}).get(path)) == _path_key(project_name))
# A project_name containing directories is stronger than a basename.
name_key = _path_key(project_name)
suffix = tuple(path for path in paths if _path_key(path).endswith('/' + name_key.lstrip('/')))
matches = tuple(dict.fromkeys((*aliases, *suffix)))
if len(matches) == 1:
return matches[0]
if len(matches) > 1:
return None
matches = _exact_name_matches(project_name or project_path, paths)
return matches[0] if len(matches) == 1 else None
def route_sources(rows, paths, *, manual=None, single=False, project_names=None):
"""Route source rows; stale paths are allowed only on exact unique names."""
paths, manual = tuple(Path(path) for path in paths), manual or {}
routes = {}
for row in rows:
key = row.project_key
if key in routes:
continue
chosen = manual.get(key)
if chosen is not None:
chosen = Path(chosen)
if chosen not in paths:
raise TaskCenterError('project_binding', '人工选择的工程不在本次工程列表中')
else:
chosen = resolve_project(row.project_path, row.project_name, paths, project_names=project_names)
if chosen is None and single and len(paths) == 1 and not row.project_path:
chosen = paths[0]
routes[key] = chosen
return routes
def write_routed_bom(destination, rows, routes, *, project_names=None):
"""Write canonical project paths while preserving row order and values."""
records = []
source_map = []
for row in rows:
target = routes.get(row.project_key)
if target is None:
raise TaskCenterError('project_binding', f'请先确认工程匹配:{row.project_key}')
record = dict(zip(BOM_HEADERS, row.values))
record['project_path'] = str(Path(target).resolve())
record['value_mode'] = row.value_mode
records.append(record)
if _path_key(row.project_path) == _path_key(target.resolve()):
reason = 'exact_path'
elif row.project_name and _path_key(row.project_name) == _path_key((project_names or {}).get(target)):
reason = 'migrated_task_name' if row.project_path else 'task_name'
elif row.project_name and _path_key(target).endswith('/' + _path_key(row.project_name).lstrip('/')):
reason = 'migrated_exact_name' if row.project_path else 'exact_name'
elif row.project_name and _project_name(row.project_name) == _project_name(target):
reason = 'migrated_basename' if row.project_path else 'basename'
elif row.project_path:
reason = 'migrated_basename'
else:
reason = 'single_project'
source_map.append({'source_file': str(row.source), 'source_row': row.excel_row,
'original_project_path': row.project_path,
'project_name': row.project_name,
'matched_project_path': str(Path(target).resolve()),
'match_reason': reason})
destination = Path(destination)
destination.parent.mkdir(parents=True, exist_ok=True)
result = write_bom_template(destination, records)
from .batch_resources import write_record
write_record(destination.with_name('source-map.json'), source_map)
return result
@@ -0,0 +1,225 @@
"""PR21 routing, source evidence and operation-boundary regressions; no CAD."""
import json
from pathlib import Path
from types import SimpleNamespace
import zipfile
from openpyxl import Workbook, load_workbook
import pytest
from dx_productivity.trial_task_center.bom_import_report import BomImportReport
from dx_productivity.trial_task_center.bom_plan import BOM_HEADERS
from dx_productivity.trial_task_center.errors import TaskCenterError
from dx_productivity.trial_task_center.logging_setup import configure_logging
from dx_productivity.trial_task_center.service import TaskCenterService
from dx_productivity.trial_task_center.store import TaskStore
from dx_productivity.trial_task_center.target_plan import ComponentTarget
from dx_productivity.trial_task_center.workspace_bom import load_sources, route_sources, write_routed_bom
@pytest.fixture
def case(tmp_path):
root = tmp_path / 'projects'
for name in ('BoardA', 'BoardB'):
project = root / name / (name + '.prj')
project.parent.mkdir(parents=True)
project.write_text('KEY FrontEndSnapshot "DxD"\nKEY iCDBDir "database"\n')
store = TaskStore(tmp_path / 'tasks.db')
service = TaskCenterService(store, configure_logging(tmp_path / 'data'))
tasks = service.create_project_tasks(root)
paths = tuple(service.projects(t.task_id)[0].path for t in tasks)
for task, path in zip(tasks, paths):
store.record_target_inventory(task.task_id, (
ComponentTarget(path, 'MAIN', '1', 'u1', 'R1', 'OLD', '10nF'),))
yield SimpleNamespace(service=service, store=store, tasks=tasks, paths=paths,
runtime=SimpleNamespace(bind_logs=lambda *_: None), root=tmp_path / 'data')
store.close()
def source(path, *, name='BoardA.prj', project='Z:/old/BoardA.prj', row=2):
book = Workbook()
sheet = book.active
sheet.append(BOM_HEADERS)
for _ in range(2, row):
sheet.append([None] * len(BOM_HEADERS))
sheet.append([name, project, 'OLD', 'NEW', 'R1', '', '', '', True, '', True])
book.save(path)
book.close()
return path
def test_renamed_task_template_routes_stale_path_in_multi_project_batch(case, tmp_path):
task = case.tasks[0]
case.service.update_task(task.task_id, name='音频板', note='')
path = case.service.export_bom_template(task.task_id, tmp_path / 'renamed.xlsx')
book = load_workbook(path)
sheet = book.active
sheet['B2'] = 'Z:/moved/BoardA.prj'
sheet['C2'], sheet['D2'], sheet['E2'] = 'OLD', 'NEW', 'R1'
book.save(path)
book.close()
report = case.service.preview_bom_sources(task.task_id, (path,), runtime=case.runtime)
assert report.passed_rows == 1 and report.preview
assert report.preview.projects[0].project_path == case.paths[0]
assert json.loads(report.source_map_path.read_text(encoding='utf-8'))[0]['match_reason'] == 'migrated_task_name'
def test_single_project_blank_path_keeps_legacy_display_name_binding(case, tmp_path):
task_id = case.tasks[0].task_id
case.store._connection.execute('UPDATE tasks SET group_id = NULL WHERE task_id = ?', (task_id,))
path = source(tmp_path / 'blank-path.xlsx', name='display name from another machine', project='')
report = case.service.preview_bom_sources(task_id, (path,), runtime=case.runtime)
assert report.passed_rows == 1 and report.preview
assert json.loads(report.source_map_path.read_text(encoding='utf-8'))[0]['match_reason'] == 'single_project'
def test_duplicate_messages_use_both_original_workbook_coordinates(case, tmp_path):
first = source(tmp_path / 'first.xlsx')
second = source(tmp_path / 'second.xlsx', row=5)
report = case.service.preview_bom_sources(case.tasks[0].task_id, (first, second), runtime=case.runtime)
assert report.preview is None and report.failed_rows == 2
assert [row.source_row for row in report.rows] == [2, 5]
for row in report.rows:
assert 'first.xlsx 第 2 行' in row.reason
assert 'second.xlsx 第 5 行' in row.reason
assert 'Excel 行 2, 3' not in row.reason
def test_import_evidence_is_referenced_exportable_and_discards_bom_copy(case, tmp_path):
from dx_productivity.trial_task_center.diagnostics_bundle import export_diagnostic_bundle
path = source(tmp_path / 'original.xlsx')
report = case.service.preview_bom_sources(case.tasks[0].task_id, (path,), runtime=case.runtime)
mapping = report.source_map_path
assert mapping.is_file()
assert not mapping.with_name('normalized-bom.xlsx').exists()
task_events = case.root / 'tasks' / case.tasks[0].task_id / 'events.jsonl'
assert any(json.loads(line).get('details', {}).get('source_map_path') == str(mapping)
for line in task_events.read_text(encoding='utf-8').splitlines())
bundle = export_diagnostic_bundle(case.root, tmp_path / 'diagnostics.zip')
with zipfile.ZipFile(bundle) as archive:
assert mapping.relative_to(case.root).as_posix() in archive.namelist()
assert not any(name.endswith('.xlsx') for name in archive.namelist())
def test_source_map_has_bounded_existing_history_retention(case, tmp_path):
from dx_productivity.trial_task_center.retention import maintain_quick_run_storage
path = source(tmp_path / 'source.xlsx')
first = case.service.preview_bom_sources(case.tasks[0].task_id, (path,), runtime=case.runtime)
second = case.service.preview_bom_sources(case.tasks[0].task_id, (path,), runtime=case.runtime)
task_root = case.root / 'tasks' / case.tasks[0].task_id
maintain_quick_run_storage(task_root, max_entries=1, min_entries=0)
assert not first.source_map_path.exists()
assert second.source_map_path.is_file()
def test_relative_project_path_is_rejected_before_normalization(tmp_path):
path = source(tmp_path / 'relative.xlsx', project='sub/BoardA.prj')
with pytest.raises(TaskCenterError, match='完整路径'):
load_sources((path,))
def test_failed_source_map_replace_preserves_previous_valid_json(tmp_path, monkeypatch):
import dx_productivity.trial_task_center.batch_resources as resources
path = source(tmp_path / 'source.xlsx')
rows = load_sources((path,))
routes = route_sources(rows, (tmp_path / 'BoardA.prj',))
mapping = tmp_path / 'source-map.json'
mapping.write_text('[{"previous":true}]', encoding='utf-8')
real_replace = resources.os.replace
def deny(source_path, destination_path):
if Path(destination_path).name == 'source-map.json':
raise OSError('controlled disk write failure')
return real_replace(source_path, destination_path)
monkeypatch.setattr(resources.os, 'replace', deny)
with pytest.raises(OSError, match='controlled disk'):
write_routed_bom(tmp_path / 'normalized.xlsx', rows, routes)
assert json.loads(mapping.read_text(encoding='utf-8')) == [{'previous': True}]
assert not list(tmp_path.glob('source-map.json.*.tmp'))
def test_supervised_import_dispatches_before_reading_any_source(case, tmp_path, monkeypatch):
import dx_productivity.trial_task_center.workspace_bom as workspace
case.service._supervised = True
path = source(tmp_path / 'source.xlsx')
monkeypatch.setattr(workspace, 'load_sources', lambda *_a, **_kw: pytest.fail('source read escaped supervisor'))
def supervise(operation, arguments, **controls):
assert operation == 'preview_bom_sources'
assert arguments['sources'] == (path,)
return BomImportReport((), ())
monkeypatch.setattr(case.service, '_supervise', supervise)
assert case.service.preview_bom_sources(case.tasks[0].task_id, (path,)).total_rows == 0
def test_each_source_reports_read_progress_before_inspection(case, tmp_path, monkeypatch):
import dx_productivity.trial_task_center.bom_import_report as inspection
from dx_productivity.trial_task_center.bom_import_state import BomImportPhase
paths = (source(tmp_path / 'first.xlsx'), source(tmp_path / 'second.xlsx', name='BoardB.prj', project='Z:/old/BoardB.prj'))
progress, phases = [], []
original = inspection.inspect_batch
def inspect(*args, **kwargs):
assert any('first.xlsx' in message for message in progress)
assert any('second.xlsx' in message for message in progress)
assert phases[0] is BomImportPhase.READING
return original(*args, **kwargs)
monkeypatch.setattr(inspection, 'inspect_batch', inspect)
report = case.service.preview_bom_sources(case.tasks[0].task_id, paths, runtime=case.runtime,
on_progress=progress.append, on_phase=lambda phase, _: phases.append(phase))
assert report.passed_rows == 2
def test_single_lease_covers_source_read_and_inspection(case, tmp_path, monkeypatch):
import dx_productivity.trial_task_center.workspace_bom as workspace
import dx_productivity.trial_task_center.bom_import_report as inspection
from dx_productivity.trial_task_center.service import _quick_run_lease
checked = []
operations = []
def assert_busy():
with pytest.raises(TaskCenterError) as failure:
with _quick_run_lease(case.root):
pytest.fail('import operation released its lease')
assert failure.value.code == 'quick_run_busy'
original_load, original_inspect = workspace.load_sources, inspection.inspect_batch
def load(*args, **kwargs):
assert_busy()
checked.append('read')
return original_load(*args, **kwargs)
def inspect(*args, **kwargs):
assert_busy()
checked.append('inspect')
return original_inspect(*args, **kwargs)
monkeypatch.setattr(workspace, 'load_sources', load)
monkeypatch.setattr(inspection, 'inspect_batch', inspect)
original_operation = case.service._idle_quick_run_operation
def operation(name, task_id, action, **kwargs):
operations.append(name)
return original_operation(name, task_id, action, **kwargs)
monkeypatch.setattr(case.service, '_idle_quick_run_operation', operation)
report = case.service.preview_bom_sources(case.tasks[0].task_id,
(source(tmp_path / 'source.xlsx'),), runtime=case.runtime)
assert report.passed_rows == 1 and checked == ['read', 'inspect']
assert operations == ['preview_bom_sources']
with _quick_run_lease(case.root):
pass
def test_duplicate_task_alias_blocks_but_exact_path_wins(case, tmp_path):
from dx_productivity.trial_task_center.workspace_bom import resolve_project
names = dict.fromkeys(case.paths, 'same display name')
assert resolve_project('Z:/old/BoardA.prj', 'same display name', case.paths, project_names=names) is None
assert resolve_project(str(case.paths[0]), 'same display name', case.paths, project_names=names) == case.paths[0]
@pytest.mark.parametrize('name,project,reason', [
('BoardA.prj', '', 'exact_name'),
('', '', 'single_project'),
('BoardA.prj', 'Z:/old/BoardA.prj', 'migrated_exact_name'),
])
def test_source_map_distinguishes_name_migration_from_blank_fallback(case, tmp_path, name, project, reason):
task_id = case.tasks[0].task_id
case.store._connection.execute('UPDATE tasks SET group_id = NULL WHERE task_id = ?', (task_id,))
path = source(tmp_path / 'source.xlsx', name=name, project=project)
report = case.service.preview_bom_sources(task_id, (path,), runtime=case.runtime)
assert report.passed_rows == 1
assert json.loads(report.source_map_path.read_text(encoding='utf-8'))[0]['match_reason'] == reason
@@ -0,0 +1,81 @@
"""Portable XLSX and item Value contracts; no CAD, Qt, or Windows runtime."""
import pytest
from openpyxl import load_workbook
from dx_productivity.trial_task_center.bom_batch import export_template
from dx_productivity.trial_task_center.bom_plan import (
BOM_HEADERS, match_bom_records, read_bom_records, write_bom_template,
)
from dx_productivity.trial_task_center.store import TaskStore
from dx_productivity.trial_task_center.target_plan import ComponentTarget, TargetPlanRow, compile_target_rules
@pytest.mark.parametrize('with_task', [False, True])
def test_user_template_exposes_editable_value_source_without_inventory(tmp_path, with_task):
store = TaskStore(tmp_path / 'tasks.db')
try:
task_id = None
if with_task:
task_id = store.create_task('Board').task_id
store.sync_projects(task_id, tmp_path, (tmp_path / 'Board.prj',))
path = export_template(store, task_id, tmp_path / 'template.xlsx')
book = load_workbook(path)
try:
sheet = book.active
assert tuple(cell.value for cell in sheet[1]) == BOM_HEADERS + ('value_mode',)
header = sheet['L1']
assert header.font.bold and header.font.color.rgb == '00FFFFFF'
assert header.fill.patternType == 'solid' and header.fill.fgColor.rgb == '001D3557'
assert header.comment and all(mode in header.comment.text for mode in (
'inherit', 'preserve', 'part_label', 'part_number', 'custom'))
assert any(validation.formula1 == '"inherit,preserve,part_label,part_number,custom"'
and 'L2' in validation for validation in sheet.data_validations.dataValidation)
assert sheet['C2'].number_format == sheet['D2'].number_format == '@'
if with_task:
assert sheet['B2'].value == str(tmp_path / 'Board.prj')
assert not store.list_target_plan(task_id)
finally:
book.close()
assert read_bom_records(path) == ()
finally:
store.close()
def test_legacy_writer_keeps_eleven_columns_and_custom_value_inference(tmp_path):
path = write_bom_template(tmp_path / 'legacy.xlsx', ({
'old_part_number': '001', 'new_part_number': '002', 'refdes': 'R1', 'custom_value': '47k',
},))
book = load_workbook(path)
try:
assert len(BOM_HEADERS) == 11
assert tuple(cell.value for cell in book.active[1]) == BOM_HEADERS
finally:
book.close()
record, = read_bom_records(path)
assert (record.old, record.new, record.value_mode, record.custom) == ('001', '002', 'custom', '47k')
@pytest.mark.parametrize('mode,custom,expected', [
('inherit', '', 'part_label'),
('preserve', '', 'preserve'),
('part_label', '', 'part_label'),
('part_number', '', 'part_number'),
('custom', '47k', 'custom'),
])
def test_record_value_source_roundtrips_and_compiles_with_task_default(tmp_path, mode, custom, expected):
path = write_bom_template(tmp_path / 'values.xlsx', ({
'old_part_number': '001', 'new_part_number': '002', 'refdes': 'R1',
'value_mode': mode, 'custom_value': custom,
},))
book = load_workbook(path)
try:
assert book.active['L1'].comment is not None
assert book.active['L1'].font.bold
finally:
book.close()
target = ComponentTarget(tmp_path / 'Board.prj', 'MAIN', '1', 'u1', 'R1', '001', '10nF')
records = read_bom_records(path)
rows = match_bom_records((TargetPlanRow(target),), records)
rule, = compile_target_rules(rows, default_value_mode='part_label')
assert (rows[0].value_mode, rule.value_mode, rule.custom_value) == (mode, expected, custom)
@@ -0,0 +1,481 @@
from pathlib import Path
import pytest
from openpyxl import Workbook
from dx_productivity.trial_task_center.errors import TaskCenterError
from dx_productivity.trial_task_center.target_plan import (
ComponentTarget,
TargetPlanRow,
compile_target_rules,
)
from dx_productivity.trial_task_center.models import ProjectRecord, ProjectStatus
from dx_productivity.trial_task_center.quick_run import QuickRunPreparer
def _target(tmp_path, uid, refdes):
return ComponentTarget(tmp_path / 'migrated.prj', 'MAIN', '1', uid, refdes, 'OLD', '10nF')
def test_each_enabled_item_freezes_its_own_value_mode(tmp_path):
rows = (
TargetPlanRow(_target(tmp_path, 'u1', 'R1'), True, 'NEW', value_mode='preserve'),
TargetPlanRow(_target(tmp_path, 'u2', 'R2'), True, 'NEW', value_mode='part_label'),
TargetPlanRow(_target(tmp_path, 'u3', 'R3'), True, 'NEW', value_mode='custom', custom_value='47k'),
)
rules = compile_target_rules(rows)
assert rows[2].custom_value_enabled
assert {(rule.targets[0].refdes, rule.value_mode, rule.custom_value) for rule in rules} == {
('R1', 'preserve', ''), ('R2', 'part_label', ''), ('R3', 'custom', '47k')
}
def test_inherit_item_uses_task_default_but_explicit_item_override_wins(tmp_path):
rows = (
TargetPlanRow(_target(tmp_path, 'u1', 'R1'), True, 'NEW', value_mode='inherit'),
TargetPlanRow(_target(tmp_path, 'u2', 'R2'), True, 'NEW', value_mode='preserve'),
)
rules = compile_target_rules(rows, default_value_mode='part_label')
assert [(rule.targets[0].refdes, rule.value_mode) for rule in rules] == [
('R1', 'part_label'), ('R2', 'preserve')
]
def test_service_execution_rules_resolve_task_default_for_inherited_items(tmp_path):
from dx_productivity.trial_task_center.logging_setup import configure_logging
from dx_productivity.trial_task_center.service import TaskCenterService
from dx_productivity.trial_task_center.store import TaskStore
store = TaskStore(tmp_path / 'execution-default.db')
service = TaskCenterService(store, configure_logging(tmp_path / 'data'))
task = service.create_task('default policy')
project = tmp_path / 'board.prj'
store.sync_projects(task.task_id, tmp_path, (project,))
targets = (
ComponentTarget(project, 'MAIN', '1', 'u1', 'R1', 'OLD1', '10nF'),
ComponentTarget(project, 'MAIN', '1', 'u2', 'R2', 'OLD2', '10nF'),
)
store.record_target_inventory(task.task_id, targets)
store.replace_target_plan(task.task_id, (
TargetPlanRow(targets[0], True, 'NEW1', value_mode='inherit'),
TargetPlanRow(targets[1], True, 'NEW2', value_mode='preserve'),
))
service.set_copy_part_number_to_value(task.task_id, True)
try:
rules = service._execution_rules(task.task_id)
assert [(rule.targets[0].refdes, rule.value_mode) for rule in rules] == [
('R1', 'part_label'), ('R2', 'preserve')
]
finally:
store.close()
def test_execution_command_preserves_item_value_modes(tmp_path):
projects = (ProjectRecord('p1', 'task', 'Board', tmp_path / 'migrated.prj', tmp_path,
True, True, ProjectStatus.PENDING, 0),)
rows = (
TargetPlanRow(_target(tmp_path, 'u1', 'R1'), True, 'NEW', value_mode='preserve'),
TargetPlanRow(_target(tmp_path, 'u2', 'R2'), True, 'NEW', value_mode='part_label'),
)
rules = compile_target_rules(rows)
command = QuickRunPreparer(tmp_path / 'data', script_source=Path(__file__)).prepare(
'task', projects, rules, copy_part_number_to_value=False,
sheet_targets={str(projects[0].path).casefold(): (('MAIN', '1'),)},
).command_path.read_text(encoding='utf-8')
header, first, second = command.splitlines()
mode = header.split('\t').index('value_mode')
assert [first.split('\t')[mode], second.split('\t')[mode]] == ['preserve', 'part_label']
def test_invalid_item_value_options_are_rejected(tmp_path):
with pytest.raises(TaskCenterError, match='Value'):
TargetPlanRow(_target(tmp_path, 'u1', 'R1'), True, 'NEW', value_mode='automatic')
with pytest.raises(TaskCenterError, match='custom Value is empty'):
compile_target_rules((TargetPlanRow(_target(tmp_path, 'u1', 'R1'), True, 'NEW',
value_mode='custom'),))
def test_legacy_bom_custom_value_remains_custom_after_normalization(tmp_path):
from dx_productivity.trial_task_center.bom_plan import read_bom_records, write_bom_template
from dx_productivity.trial_task_center.workspace_bom import load_sources, route_sources, write_routed_bom
source = write_bom_template(tmp_path / 'legacy.xlsx', ({
'project_name': 'Board.prj', 'project_path': r'Z:\old\Board.prj',
'old_part_number': 'OLD', 'new_part_number': 'NEW', 'refdes': 'R1',
'custom_value': '47k',
},))
rows = load_sources((source,))
normalized = write_routed_bom(tmp_path / 'normalized.xlsx', rows,
route_sources(rows, (tmp_path / 'Board.prj',)))
record, = read_bom_records(normalized)
assert record.value_mode == 'custom' and record.custom == '47k'
def test_target_context_keeps_editable_mapping_rules_after_item_overrides(tmp_path):
from dx_productivity.trial_task_center.logging_setup import configure_logging
from dx_productivity.trial_task_center.service import TaskCenterService
from dx_productivity.trial_task_center.store import TaskStore
store = TaskStore(tmp_path / 'context.db')
service = TaskCenterService(store, configure_logging(tmp_path / 'data'))
project = tmp_path / 'board.prj'
project.write_text('KEY FrontEndSnapshot "DxD"\nKEY iCDBDir "database"\n')
task, = service.create_project_tasks(tmp_path, selected_paths=(project,))
original = service.add_rule(task.task_id, 'OLD', 'NEW')
target = ComponentTarget(project, 'MAIN', '1', 'u1', 'R1', 'OLD', '10nF')
store.record_target_inventory(task.task_id, (target,))
store.replace_target_plan(task.task_id, (TargetPlanRow(target, True, 'OTHER', value_mode='part_label'),))
try:
context = service.task_context(task.task_id)
assert [(r.rule_id, r.new_part_number) for r in context.rules] == [(original.rule_id, 'NEW')]
assert service._execution_rules(task.task_id)[0].new_part_number == 'OTHER'
finally:
store.close()
def _write_bom(path, project_name, project_path, refdes):
book = Workbook()
sheet = book.active
sheet.append(['project_name', 'project_path', 'old_part_number', 'new_part_number', 'refdes', 'value_mode'])
sheet.append([project_name, project_path, 'OLD', 'NEW', refdes, 'part_label'])
book.save(path)
def test_multi_file_bom_load_preserves_sources_and_migrates_exact_project_name(tmp_path):
from dx_productivity.trial_task_center.workspace_bom import load_sources, route_sources
first = tmp_path / 'first.xlsx'
second = tmp_path / 'second.xlsx'
_write_bom(first, 'Board.prj', r'Z:\old\Board.prj', 'R1')
_write_bom(second, 'Board.prj', r'Z:\old\Board.prj', 'R2')
current = tmp_path / 'Board.prj'
rows = load_sources((first, second))
routes = route_sources(rows, (current,))
assert [row.source.name for row in rows] == ['first.xlsx', 'second.xlsx']
assert [row.excel_row for row in rows] == [2, 2]
assert routes[rows[0].project_key] == current
def test_migrated_name_match_is_ambiguous_without_manual_route(tmp_path):
from dx_productivity.trial_task_center.workspace_bom import load_sources, route_sources
source = tmp_path / 'one.xlsx'
_write_bom(source, 'Board.prj', r'Z:\old\Board.prj', 'R1')
candidates = (tmp_path / 'a' / 'Board.prj', tmp_path / 'b' / 'Board.prj')
for candidate in candidates:
candidate.parent.mkdir()
candidate.touch()
rows = load_sources((source,))
routes = route_sources(rows, candidates)
assert routes[rows[0].project_key] is None
def test_distinct_project_names_route_independently_when_stale_paths_are_identical(tmp_path):
from dx_productivity.trial_task_center.workspace_bom import load_sources, route_sources
sources = (tmp_path / 'first.xlsx', tmp_path / 'second.xlsx')
_write_bom(sources[0], 'BoardA.prj', r'Z:\old\wrong.prj', 'R1')
_write_bom(sources[1], 'BoardB.prj', r'Z:\old\wrong.prj', 'R1')
paths = (tmp_path / 'BoardA.prj', tmp_path / 'BoardB.prj')
rows = load_sources(sources)
routes = route_sources(rows, paths)
assert tuple(routes[row.project_key] for row in rows) == paths
def test_routed_bom_writes_source_map_for_closed_loop_diagnostics(tmp_path):
from dx_productivity.trial_task_center.workspace_bom import load_sources, route_sources, write_routed_bom
source = tmp_path / 'one.xlsx'
_write_bom(source, 'Board.prj', r'Z:\old\Board.prj', 'R1')
current = tmp_path / 'Board.prj'
rows = load_sources((source,))
routes = route_sources(rows, (current,))
destination = tmp_path / 'normalized' / 'bom.xlsx'
write_routed_bom(destination, rows, routes)
source_map = destination.with_name('source-map.json').read_text(encoding='utf-8')
assert 'migrated_exact_name' in source_map
assert 'one.xlsx' in source_map and '"source_row": 2' in source_map
def test_target_plan_schema_migrates_value_mode_for_existing_inventory(tmp_path):
from dx_productivity.trial_task_center.store import TaskStore
path = tmp_path / 'tasks.db'
store = TaskStore(path)
task = store.create_task('migration')
project = tmp_path / 'board.prj'
store.sync_projects(task.task_id, tmp_path, (project,))
store.record_target_inventory(task.task_id, (ComponentTarget(project, 'MAIN', '1', 'u1', 'R1', 'OLD', '10nF'),))
store.close()
import sqlite3
with sqlite3.connect(path) as connection:
connection.execute('ALTER TABLE target_plan DROP COLUMN value_mode')
store = TaskStore(path)
try:
# Existing inventory must inherit the task policy after migration.
assert store.list_target_plan(task.task_id)[0].value_mode == 'inherit'
finally:
store.close()
def test_closed_loop_persists_plan_reopens_and_freezes_command_modes(tmp_path):
from dx_productivity.trial_task_center.models import ProjectStatus, ProjectRecord
from dx_productivity.trial_task_center.store import TaskStore
database_path = tmp_path / 'closed-loop.db'
project = tmp_path / 'board.prj'
store = TaskStore(database_path)
task = store.create_task('closed-loop')
store.sync_projects(task.task_id, tmp_path, (project,))
targets = (_target(tmp_path, 'u1', 'R1'), _target(tmp_path, 'u2', 'R2'))
targets = tuple(ComponentTarget(project, t.schematic_name, t.sheet_number, t.uid, t.refdes,
t.old_part_number, t.old_value) for t in targets)
store.record_target_inventory(task.task_id, targets)
rows = (TargetPlanRow(targets[0], True, 'NEW', value_mode='preserve'),
TargetPlanRow(targets[1], True, 'NEW', value_mode='part_label'))
store.replace_target_plan(task.task_id, rows)
store.close()
reopened = TaskStore(database_path)
try:
frozen = reopened.list_target_plan(task.task_id)
rules = compile_target_rules(frozen)
command = QuickRunPreparer(tmp_path / 'evidence', script_source=Path(__file__)).prepare(
task.task_id,
(ProjectRecord('p1', task.task_id, 'Board', project, tmp_path, True, True,
ProjectStatus.PENDING, 0),),
rules,
copy_part_number_to_value=False,
sheet_targets={str(project).casefold(): (('MAIN', '1'),)},
).command_path.read_text(encoding='utf-8')
header, *command_rows = command.splitlines()
mode_index = header.split('\t').index('value_mode')
assert [row.split('\t')[mode_index] for row in command_rows] == ['preserve', 'part_label']
finally:
reopened.close()
def test_service_multi_file_preview_normalizes_migrated_routes_before_cad(tmp_path, monkeypatch):
from dx_productivity.trial_task_center.logging_setup import configure_logging
from dx_productivity.trial_task_center.service import TaskCenterService
from dx_productivity.trial_task_center.store import TaskStore
import dx_productivity.trial_task_center.bom_import_report as report_module
source = tmp_path / 'migrated.xlsx'
_write_bom(source, 'Board.prj', r'Z:\old\Board.prj', 'R1')
current = tmp_path / 'Board.prj'
store = TaskStore(tmp_path / 'service.db')
service = TaskCenterService(store, configure_logging(tmp_path / 'data'))
task = service.create_task('BOM')
store.sync_projects(task.task_id, tmp_path, (current,))
seen = {}
def fake_inspect(_service, _task_id, path, **_options):
seen['path'] = Path(path)
assert seen['path'].is_file()
return report_module.BomImportReport(
rows=(report_module.BomRowResult(2, str(current), 'Board', 'passed', 'validated'),),
projects=(report_module.BomProjectResult(str(current), 'Board', 'passed', (2,)),),
)
monkeypatch.setattr(report_module, 'inspect_batch', fake_inspect)
try:
report = service.preview_bom_sources(task.task_id, (source,))
assert report.rows[0].reason.startswith('validated;来源:')
assert 'migrated.xlsx' in report.rows[0].reason and '第 2 行' in report.rows[0].reason
assert not seen['path'].exists()
assert report.source_map_path == seen['path'].parent / 'source-map.json'
assert (seen['path'].parent / 'source-map.json').is_file()
finally:
store.close()
@pytest.fixture
def simulated_bom_workspace(tmp_path):
from types import SimpleNamespace
from dx_productivity.trial_task_center.logging_setup import configure_logging
from dx_productivity.trial_task_center.service import TaskCenterService
from dx_productivity.trial_task_center.store import TaskStore
root = tmp_path / 'projects'
for name in ('BoardA', 'BoardB'):
project = root / name / (name + '.prj')
project.parent.mkdir(parents=True)
project.write_text('KEY FrontEndSnapshot "DxD"\nKEY iCDBDir "database"\n')
store = TaskStore(tmp_path / 'simulation.db')
service = TaskCenterService(store, configure_logging(tmp_path / 'evidence'))
tasks = service.create_project_tasks(root)
for task in tasks:
project = service.projects(task.task_id)[0].path
targets = tuple(ComponentTarget(project, 'MAIN', '1', f'u{i}', f'R{i}', 'OLD', '10nF')
for i in (1, 2))
store.record_target_inventory(task.task_id, targets)
runtime = SimpleNamespace(bind_logs=lambda *_: None)
yield service, store, tasks, runtime
store.close()
def test_two_excel_imports_save_reopen_freeze_and_recovery_readback(simulated_bom_workspace, tmp_path):
from dataclasses import replace
from dx_productivity.trial_task_center.bom_plan import write_bom_template
from dx_productivity.trial_task_center.bom_import_state import BomImportPhase as Phase
from dx_productivity.trial_task_center.quick_run import _read_command_options, _read_commands, _queue_matches_inputs
from dx_productivity.trial_task_center.store import TaskStore
service, store, tasks, runtime = simulated_bom_workspace
service.set_copy_part_number_to_value(tasks[0].task_id, True)
sources = []
for task, modes in zip(tasks, (('inherit', 'preserve'), ('custom', 'part_number'))):
project = service.projects(task.task_id)[0].path
sources.append(write_bom_template(tmp_path / (project.stem + '.xlsx'), tuple({
'project_name': task.name, 'project_path': 'Z:/old/' + project.name,
'old_part_number': 'OLD', 'new_part_number': 'NEW', 'refdes': f'R{i}',
'value_mode': mode, 'custom_value': '47k' if mode == 'custom' else '',
} for i, mode in enumerate(modes, 1))))
phases = []
report = service.preview_bom_sources(tasks[0].task_id, sources, runtime=runtime,
on_phase=lambda phase, _: phases.append(phase))
assert report.passed_rows == 4 and report.preview is not None
assert phases == [Phase.READING, Phase.VALIDATING, Phase.INVENTORY, Phase.MATCHING,
Phase.INVENTORY, Phase.MATCHING]
assert all(not row.enabled for task in tasks for row in store.list_target_plan(task.task_id))
for row, filename, number in zip(report.rows, ('BoardA.xlsx', 'BoardA.xlsx', 'BoardB.xlsx', 'BoardB.xlsx'), (2, 3, 2, 3)):
assert filename in row.reason and f'第 {number} 行' in row.reason
service.save_bom_batch(report.preview, {p.task_id: p.rows for p in report.preview.projects})
reopened = TaskStore(store.path)
try:
for task, expected in zip(tasks, ((('part_label', ''), ('preserve', '')),
(('custom', '47k'), ('part_number', '')))):
policy = reopened.get_task(task.task_id).copy_part_number_to_value
rules = compile_target_rules(reopened.list_target_plan(task.task_id),
default_value_mode='part_label' if policy else 'preserve')
projects = reopened.list_projects(task.task_id)
command = QuickRunPreparer(tmp_path / task.task_id, script_source=Path(__file__)).prepare(
task.task_id, projects, rules, copy_part_number_to_value=policy,
sheet_targets={str(projects[0].path).casefold(): (('MAIN', '1'),)},
).command_path
options = _read_command_options(command)
assert tuple((mode, custom) for mode, custom, _, _ in options.values()) == expected
commands = _read_commands(command)
assert _queue_matches_inputs(command, commands, projects, rules)
changed = (replace(rules[0], custom_value='changed') if rules[0].value_mode == 'custom'
else replace(rules[0], value_mode='part_number'))
assert not _queue_matches_inputs(command, commands, projects, (changed, *rules[1:]))
finally:
reopened.close()
def test_cross_file_duplicate_blocks_every_plan_with_original_sources(simulated_bom_workspace, tmp_path):
service, store, tasks, runtime = simulated_bom_workspace
sources = (tmp_path / 'first.xlsx', tmp_path / 'second.xlsx')
for source in sources:
_write_bom(source, 'BoardA.prj', r'Z:\old\BoardA.prj', 'R1')
report = service.preview_bom_sources(tasks[0].task_id, sources, runtime=runtime)
assert report.preview is None and report.failed_rows == 2
assert 'first.xlsx' in report.rows[0].reason and 'second.xlsx' in report.rows[1].reason
assert all(not row.enabled for task in tasks for row in store.list_target_plan(task.task_id))
def test_multi_source_import_reuses_supervised_inspection(tmp_path, monkeypatch):
from dx_productivity.trial_task_center.logging_setup import configure_logging
from dx_productivity.trial_task_center.service import TaskCenterService
from dx_productivity.trial_task_center.store import TaskStore
from dx_productivity.trial_task_center.bom_import_report import BomImportReport
store = TaskStore(tmp_path / 'supervised.db')
service = TaskCenterService(store, configure_logging(tmp_path / 'data'), supervised=True)
task = store.create_task('board')
store.sync_projects(task.task_id, tmp_path, (tmp_path / 'Board.prj',))
source = tmp_path / 'one.xlsx'
_write_bom(source, 'Board.prj', r'Z:\old\Board.prj', 'R1')
phases = []
def supervised(operation, arguments, **controls):
assert operation == 'preview_bom_sources'
assert arguments['sources'] == (source,)
controls['on_phase']('validating', 'normalized')
return BomImportReport((), ())
monkeypatch.setattr(service, '_supervise', supervised)
try:
service.preview_bom_sources(task.task_id, (source,), on_phase=lambda phase, _: phases.append(phase))
assert phases == ['validating']
finally:
store.close()
def test_legacy_plan_text_false_inherits_without_enabling_custom(simulated_bom_workspace, tmp_path):
from openpyxl import load_workbook
from dx_productivity.trial_task_center.spreadsheet_plan import export_target_plan, read_target_plan
_, store, tasks, _ = simulated_bom_workspace
task_id = tasks[0].task_id
path = export_target_plan(store, task_id, tmp_path / 'legacy-plan.xlsx')
book = load_workbook(path)
book.active.delete_cols(14)
book.active['I2'] = True
book.active['J2'] = 'NEW'
book.active['K2'] = 'FALSE'
book.save(path)
book.close()
imported = read_target_plan(store, task_id, path)
assert imported[0].enabled and imported[0].value_mode == 'inherit'
assert not imported[0].custom_value_enabled
@pytest.mark.parametrize('invalid', ['=1+1', '#N/A'])
def test_malformed_second_excel_is_blocked_before_inspection(simulated_bom_workspace, tmp_path, monkeypatch, invalid):
from openpyxl import load_workbook
service, store, tasks, _ = simulated_bom_workspace
first, second = tmp_path / 'first.xlsx', tmp_path / 'second.xlsx'
for source in (first, second):
_write_bom(source, 'BoardA.prj', r'Z:\old\BoardA.prj', 'R1')
book = load_workbook(second)
book.active['D2'] = invalid
book.save(second)
book.close()
monkeypatch.setattr(service, 'inspect_bom_batch', lambda *_a, **_k: pytest.fail('invalid source reached inspection'))
with pytest.raises(TaskCenterError, match='second.xlsx.*2'):
service.preview_bom_sources(tasks[0].task_id, (first, second))
assert all(not row.enabled for task in tasks for row in store.list_target_plan(task.task_id))
@pytest.mark.parametrize('mode', ['preserve', 'part_label', 'part_number'])
def test_batch_custom_default_does_not_overwrite_explicit_item_mode(simulated_bom_workspace, mode):
from dataclasses import replace
service, store, tasks, _ = simulated_bom_workspace
task_id = tasks[0].task_id
rule = service.add_rule(task_id, 'OLD', 'NEW')
rows = store.list_target_plan(task_id)
store.replace_target_plan(task_id, (replace(rows[0], enabled=True, value_mode=mode), rows[1]))
service.update_rule(rule.rule_id, old_part_number='OLD', new_part_number='NEW', enabled=True,
custom_value_enabled=True, custom_value='47k')
row = store.list_target_plan(task_id)[0]
assert row.value_mode == mode and not row.custom_value_enabled
assert service._execution_rules(task_id)[0].value_mode == mode
def test_disabling_seeded_custom_default_restores_inherited_policy(simulated_bom_workspace):
from dataclasses import replace
service, store, tasks, _ = simulated_bom_workspace
task_id = tasks[0].task_id
rule = service.add_rule(task_id, 'OLD', 'NEW', custom_value_enabled=True, custom_value='47k')
rows = store.list_target_plan(task_id)
store.replace_target_plan(task_id, (replace(rows[0], enabled=True), rows[1]))
service.set_copy_part_number_to_value(task_id, True)
service.update_rule(rule.rule_id, old_part_number='OLD', new_part_number='NEW', enabled=True,
custom_value_enabled=False, custom_value='')
row = store.list_target_plan(task_id)[0]
assert row.value_mode == 'inherit' and not row.custom_value_enabled
assert service._execution_rules(task_id)[0].value_mode == 'part_label'
+51
View File
@@ -21,6 +21,57 @@ def test_mapping_dialog_exposes_custom_value_nc_and_optional_cell(qtbot):
assert dialog.target_cell=='' and '可选' in dialog.cell_combo.itemText(0)
def test_target_table_exposes_item_value_mode_selector(qtbot):
from pathlib import Path
from dx_productivity.trial_task_center.target_plan import ComponentTarget, TargetPlanRow
from dx_productivity.trial_task_center.ui.target_table import TargetTable
table = TargetTable()
qtbot.addWidget(table)
row = TargetPlanRow(ComponentTarget(Path('C:/board.prj'), 'MAIN', '1', 'u1', 'R1', 'OLD', '10nF'),
True, 'NEW')
table.populate((row,))
assert table.model().index(0, 10).data() == 'inherit'
assert table.model().setData(table.model().index(0, 10), 'part_label')
assert table.rows()[0].value_mode == 'part_label'
assert not table.rows()[0].custom_value_enabled
assert table.model().setData(table.model().index(0, 10), 'custom')
assert table.rows()[0].value_mode == 'custom' and table.rows()[0].custom_value_enabled
from PySide6.QtCore import Qt
assert table.model().setData(table.model().index(0, 7), Qt.CheckState.Unchecked,
Qt.ItemDataRole.CheckStateRole)
assert table.rows()[0].value_mode == 'inherit' and not table.rows()[0].custom_value_enabled
assert table.plan_model.headerData(10, Qt.Orientation.Horizontal, Qt.ItemDataRole.ToolTipRole)
def test_target_value_editor_commits_selection_and_escape_cancels(qtbot):
from pathlib import Path
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QComboBox
from dx_productivity.trial_task_center.target_plan import ComponentTarget, TargetPlanRow
from dx_productivity.trial_task_center.ui.target_table import TargetTable
table = TargetTable()
qtbot.addWidget(table)
table.resize(1400, 200)
table.populate((TargetPlanRow(ComponentTarget(Path('C:/board.prj'), 'MAIN', '1', 'u1',
'R1', 'OLD', '10nF'), True, 'NEW'),))
table.show()
index = table.model().index(0, 10)
table.edit(index)
editor = table.findChild(QComboBox)
assert editor.currentText() == 'inherit'
editor.setCurrentText('part_number')
qtbot.keyClick(editor, Qt.Key.Key_Tab)
assert table.rows()[0].value_mode == 'part_number'
table.edit(index)
editor = next(e for e in table.findChildren(QComboBox) if e.isVisible())
editor.setCurrentText('custom')
qtbot.keyClick(editor, Qt.Key.Key_Escape)
assert table.rows()[0].value_mode == 'part_number'
def test_small_window_keeps_splitters_and_opens_log_in_disposable_window(ui,qtbot):
w=ui.window;w.resize(980,700);qtbot.wait(30)
assert w.width()==980 and w.height()==700
+3 -2
View File
@@ -160,12 +160,13 @@ def test_ui_previews_each_project_and_confirms_the_batch_once(batch, tmp_path, q
from PySide6.QtWidgets import QFileDialog
from dx_productivity.trial_task_center.ui.main_window import TrialTaskCenterWindow
service, store, tasks, targets, calls = batch
path = write_bom_template(tmp_path / 'bom.xlsx', records_for(tasks, targets))
paths = [write_bom_template(tmp_path / f'bom-{i}.xlsx', (record,))
for i, record in enumerate(records_for(tasks, targets))]
window = TrialTaskCenterWindow(service)
qtbot.addWidget(window)
window.open_task(tasks[0].task_id)
qtbot.waitUntil(lambda: not window._loading and not window._context_query.busy)
monkeypatch.setattr(QFileDialog, 'getOpenFileName', lambda *args: (str(path), 'Excel (*.xlsx)'))
monkeypatch.setattr(QFileDialog, 'getOpenFileNames', lambda *args: ([str(path) for path in paths], 'Excel (*.xlsx)'))
window.plan_panel.import_bom_button.click()
qtbot.waitUntil(lambda: not window._workers, timeout=5000)
assert window.plan_panel.table.rows()[0].new_part_number == 'NEW-0'
+29 -4
View File
@@ -236,12 +236,14 @@ def test_cancellation_during_excel_parsing_keeps_operation_cancelled(batch, tmp_
def test_ui_report_is_persistent_and_failure_does_not_stage(batch, tmp_path, qtbot, monkeypatch):
from types import SimpleNamespace
from PySide6.QtWidgets import QFileDialog
from dx_productivity.trial_task_center.ui.main_window import TrialTaskCenterWindow
service, store, tasks, targets, calls = batch
# Exercise the real async import callback without any blocking message box.
monkeypatch.setattr(service, 'inspect_bom_batch', lambda *args, **kwargs: inspect_batch(
service, *args, **kwargs), raising=False)
preview = service.preview_bom_sources
monkeypatch.setattr(service, 'preview_bom_sources', lambda *args, **kwargs: preview(
*args, runtime=SimpleNamespace(bind_logs=lambda *_: None), **kwargs))
records = list(records_for(tasks, targets))
records[1]['new_part_number'] = ''
path = write_bom_template(tmp_path / 'bad-ui.xlsx', records)
@@ -249,7 +251,8 @@ def test_ui_report_is_persistent_and_failure_does_not_stage(batch, tmp_path, qtb
qtbot.addWidget(window)
window.open_task(tasks[0].task_id)
qtbot.waitUntil(lambda: not window._loading and not window._context_query.busy)
monkeypatch.setattr(QFileDialog, 'getOpenFileName', lambda *args: (str(path), 'Excel (*.xlsx)'))
qtbot.waitUntil(window.plan_panel.import_bom_button.isEnabled)
monkeypatch.setattr(QFileDialog, 'getOpenFileNames', lambda *args: ([str(path)], 'Excel (*.xlsx)'))
window.plan_panel.import_bom_button.click()
qtbot.waitUntil(lambda: not window._workers)
panel = window.plan_panel
@@ -264,10 +267,14 @@ def test_ui_report_is_persistent_and_failure_does_not_stage(batch, tmp_path, qtb
def test_ui_cleanup_failure_keeps_details_and_retry_clears_previous_result(batch, tmp_path, qtbot, monkeypatch):
from types import SimpleNamespace
from PySide6.QtWidgets import QFileDialog
from dx_productivity.trial_task_center.ui.main_window import TrialTaskCenterWindow
from dx_productivity.trial_task_center.bom_import_state import BomImportPhase
service, store, tasks, targets, calls = batch
preview = service.preview_bom_sources
monkeypatch.setattr(service, 'preview_bom_sources', lambda *args, **kwargs: preview(
*args, runtime=SimpleNamespace(bind_logs=lambda *_: None), **kwargs))
preparer = service.for_project_task(tasks[0].task_id)._quick_run_preparer
original_reader = preparer.collect_inventory
def failed_reader(*args, **kwargs):
@@ -278,7 +285,8 @@ def test_ui_cleanup_failure_keeps_details_and_retry_clears_previous_result(batch
qtbot.addWidget(window)
window.open_task(tasks[0].task_id)
qtbot.waitUntil(lambda: not window._loading and not window._context_query.busy)
monkeypatch.setattr(QFileDialog, 'getOpenFileName', lambda *args: (str(path), 'Excel (*.xlsx)'))
qtbot.waitUntil(window.plan_panel.import_bom_button.isEnabled)
monkeypatch.setattr(QFileDialog, 'getOpenFileNames', lambda *args: ([str(path)], 'Excel (*.xlsx)'))
panel = window.plan_panel
panel.import_bom_button.click()
qtbot.waitUntil(lambda: not window._workers)
@@ -287,6 +295,7 @@ def test_ui_cleanup_failure_keeps_details_and_retry_clears_previous_result(batch
assert 'UI-CLEANUP-1' in panel.report_text and panel.report_toggle.isVisibleTo(panel)
assert not panel._bom_batches and not calls
qtbot.waitUntil(lambda: not window._loading and not window._context_query.busy)
qtbot.waitUntil(panel.import_bom_button.isEnabled)
preparer.collect_inventory = original_reader
panel.import_bom_button.click()
assert panel.last_bom_report is None and not panel.report_toggle.isVisibleTo(panel)
@@ -296,3 +305,19 @@ def test_ui_cleanup_failure_keeps_details_and_retry_clears_previous_result(batch
assert all(not store.list_target_plan(task.task_id) for task in tasks)
panel.discard_all()
window.close()
def test_ui_report_uses_original_workbook_row_and_links_source_evidence(tmp_path):
from types import SimpleNamespace
from dx_productivity.trial_task_center.bom_import_report import BomImportReport, BomRowResult
from dx_productivity.trial_task_center.bom_import_state import BomImportPhase
from dx_productivity.trial_task_center.ui.project_plan import ProjectPlanPanel
report = BomImportReport((BomRowResult(3, 'board.prj', 'Board', 'failed', 'duplicate',
source_file=str(tmp_path / 'second.xlsx'), source_row=5),), (),
source_map_path=tmp_path / 'source-map.json')
state = SimpleNamespace(report=report, phase=BomImportPhase.FAILED, notice='')
_, text, _ = ProjectPlanPanel._format_bom_report(state)
assert 'second.xlsx · Excel 第 5 行' in text
assert 'Excel 第 3 行' not in text
assert str(report.source_map_path) in text
+126
View File
@@ -0,0 +1,126 @@
import json
import os
from contextlib import closing
import sys
import textwrap
import pytest
from test_bom_batch import batch
def test_real_supervised_sources_roundtrip_origin_metadata_and_preview(batch, tmp_path):
from dx_productivity.trial_task_center.bom_plan import write_bom_template
service, store, tasks, targets, calls = batch
for task in tasks:
store.record_target_inventory(task.task_id, targets[task.task_id])
sources = tuple(write_bom_template(tmp_path / f'source-{index}.xlsx', ({
'project_name': task.name, 'project_path': str(targets[task.task_id][0].project_path),
'old_part_number': 'OLD', 'new_part_number': 'NEW', 'refdes': 'R1', 'value_mode': 'part_number',
},)) for index, task in enumerate(tasks))
service._supervised = True
report = service.preview_bom_sources(tasks[0].task_id, sources)
assert report.passed_rows == 2 and report.preview is not None
assert tuple(row.source_file for row in report.rows) == tuple(str(path.resolve()) for path in sources)
assert all(row.source_row == 2 for row in report.rows)
assert report.source_map_path.is_file()
assert not report.source_map_path.with_name('normalized-bom.xlsx').exists()
assert all(project.rows[0].value_mode == 'part_number' for project in report.preview.projects)
assert not calls
assert all(not row.enabled for task in tasks for row in store.list_target_plan(task.task_id))
def test_bom_sources_run_in_one_worker_with_import_controls(tmp_path, monkeypatch):
"""Exercise real request admission and resource release, without opening CAD."""
from dx_productivity.trial_task_center import batch_supervisor
from dx_productivity.trial_task_center.bom_import_state import BomImportPhase
from dx_productivity.trial_task_center.logging_setup import configure_logging
from dx_productivity.trial_task_center.service import TaskCenterService
from dx_productivity.trial_task_center.store import TaskStore
bootstrap = textwrap.dedent('''
import os, sys
from pathlib import Path
from dx_productivity.trial_task_center.batch_worker import run
from dx_productivity.trial_task_center.bom_import_report import BomImportReport, BomRowResult
from dx_productivity.trial_task_center.bom_import_state import BomImportPhase
from dx_productivity.trial_task_center.service import TaskCenterService
def business(self, task_id, sources, *, runtime, stop_event, on_progress, on_phase):
assert task_id == 'task'
assert type(sources) is tuple and len(sources) == 2
assert all(isinstance(source, Path) for source in sources)
assert tuple(source.name for source in sources) == ('first.xlsx', 'second.xlsx')
assert runtime.kind == 'bom_import'
assert runtime.stop_event is stop_event
on_progress('read both sources')
on_phase(BomImportPhase.READING, 'reading sources')
return BomImportReport((BomRowResult(2, str(os.getpid()), 'Board', 'passed'),), ())
TaskCenterService.preview_bom_sources = business
run(Path(sys.argv[1]))
''')
monkeypatch.setattr(batch_supervisor, '_command',
lambda request: (sys.executable, '-c', bootstrap, str(request)))
progress, phases = [], []
with closing(TaskStore(tmp_path / 'task-center.db')) as store:
service = TaskCenterService(store, configure_logging(tmp_path))
report = batch_supervisor.run_supervised(service, 'preview_bom_sources',
{'task_id': 'task', 'sources': (tmp_path / 'first.xlsx', tmp_path / 'second.xlsx')},
on_progress=progress.append, on_phase=lambda phase, message: phases.append(phase))
assert report.rows[0].project_name == 'Board'
assert report.rows[0].project_path != str(os.getpid())
assert progress == ['read both sources']
assert phases == [BomImportPhase.READING, BomImportPhase.RELEASING]
directory = next((tmp_path / 'batch-resources').iterdir())
journal = json.loads((directory / 'supervisor.json').read_text(encoding='utf-8'))
assert journal['state'] == 'finished'
assert journal['operation'] == 'preview_bom_sources'
assert journal['task_ids'] == ['task']
batch_supervisor.assert_supervisor_idle(tmp_path)
def test_bom_sources_supervisor_rejects_wrong_result_type():
from dx_productivity.trial_task_center.supervisor_protocol import encode
from dx_productivity.trial_task_center.supervisor_result import validated_outcome
with pytest.raises(ValueError, match='result type'):
validated_outcome('preview_bom_sources',
{'business_succeeded': True, 'status': 'ok', 'result': encode(())})
@pytest.mark.parametrize('shutdown_failed', [False, True])
@pytest.mark.parametrize('business_succeeded', [False, True])
def test_bom_sources_cleanup_preserves_rows_without_preview(
tmp_path, monkeypatch, shutdown_failed, business_succeeded):
from dx_productivity.trial_task_center import batch_resources
from dx_productivity.trial_task_center.batch_supervisor import _finish_response
from dx_productivity.trial_task_center.bom_batch import BomBatchPreview
from dx_productivity.trial_task_center.bom_import_report import BomImportReport, BomRowResult
from dx_productivity.trial_task_center.errors import TaskCenterError
from dx_productivity.trial_task_center.logging_setup import configure_logging
from dx_productivity.trial_task_center.supervisor_protocol import encode, encode_error
report = BomImportReport((BomRowResult(4, 'board.prj', 'Board', 'passed'),), (),
BomBatchPreview('task', (), ()))
journal = {'operation': 'preview_bom_sources', 'batch_id': 'a' * 32, 'state': 'running'}
directory = tmp_path / 'batch-resources' / journal['batch_id']
if shutdown_failed:
batch_resources.write_record(directory / 'session.json',
{'cad_shutdown': {'orderly_shutdown': False}})
pending = () if shutdown_failed else ({'session_id': journal['batch_id']},)
monkeypatch.setattr(batch_resources, 'cleanup_pending_resources', lambda *args, **kwargs: pending)
response = {'business_succeeded': business_succeeded, 'status': 'error',
'error': encode_error(TaskCenterError('batch_resources_pending', 'Quit failed'))}
if business_succeeded:
response['result'] = encode(report)
else:
primary = TaskCenterError('inventory_cleanup', 'Reader failed')
primary.bom_report = report
response['operation_error'] = encode_error(primary)
with pytest.raises(TaskCenterError) as caught:
_finish_response(tmp_path, journal, response, configure_logging(tmp_path))
assert caught.value.code == ('application_shutdown_failed' if shutdown_failed else 'batch_resources_pending')
assert caught.value.bom_report.rows == report.rows
assert caught.value.bom_report.preview is None
if business_succeeded:
assert caught.value.completed_result.preview is None
else:
assert caught.value.primary_error.code == 'inventory_cleanup'
@@ -116,6 +116,21 @@ def test_value_policy_records_actual_change_and_ignores_repeated_value(case):
assert case.history.page().rows==before
def test_item_value_only_change_is_recorded_in_history(case):
from dataclasses import replace
task = bound_task(case)
case.service.collect_target_inventory(task.task_id, ('OLD',), runtime=object())
row, = case.service.target_plan(task.task_id)
before = len(events(case, 'replace_target_plan'))
case.service.replace_target_plan(task.task_id, (replace(row, value_mode='part_label'),))
rows = events(case, 'replace_target_plan')
assert len(rows) == before + 1
change, = rows[0]['details']['changes']
assert change['table'] == 'target_plan'
assert change['before'] == {'value_mode': 'inherit'}
assert change['after'] == {'value_mode': 'part_label'}
@pytest.mark.parametrize('operation',['create_assistant_project_tasks','collect_target_inventory','refresh_bound_project','set_value_policy'])
def test_history_write_failure_rolls_back_each_real_service_mutation(case,monkeypatch,operation):
task=None if operation=='create_assistant_project_tasks' else bound_task(case)
+38
View File
@@ -68,3 +68,41 @@ def test_native_short_path_never_passes_extended_prefix_to_wsh(tmp_path):
if result is None:
pytest.skip('filesystem does not provide short paths')
assert not result.startswith(chr(92) * 2 + '?')
def test_batch_resource_record_retries_reader_denied_atomic_replace(tmp_path, monkeypatch):
import json
import os
from threading import Timer
if os.name != 'nt':
pytest.skip('Windows delete-sharing boundary')
import win32con
import win32file
from dx_productivity.trial_task_center import batch_resources
path = tmp_path / 'session.json'
path.write_text('{"stage": "before"}', encoding='utf-8')
reader = win32file.CreateFile(str(path), win32con.GENERIC_READ,
win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE, None,
win32con.OPEN_EXISTING, 0, None)
real_replace = os.replace
denials, timers = [], []
def replace(source, target):
try:
return real_replace(source, target)
except PermissionError as error:
denials.append(error.winerror)
timer = Timer(.02, reader.Close)
timers.append(timer)
timer.start()
raise
monkeypatch.setattr(batch_resources.os, 'replace', replace)
try:
batch_resources.write_record(path, {'stage': 'saved'})
assert denials == [5]
assert json.loads(path.read_text(encoding='utf-8')) == {'stage': 'saved'}
assert not list(tmp_path.glob('*.tmp'))
finally:
for timer in timers:
timer.join()
reader.Close()
+45
View File
@@ -134,3 +134,48 @@ def test_completed_failure_is_delivered_and_next_step_starts_fresh_worker(tmp_pa
assert scope.run_step(project, command, 3).returncode == 0
assert scope.worker is not previous
assert (tmp_path / 'closed.txt').exists()
@pytest.mark.parametrize('release_reader', [True, False])
def test_real_vbs_session_result_write_handles_reader_contention(tmp_path, release_reader):
import os
import re
from threading import Timer
if os.name != 'nt':
pytest.skip('Windows ADODB.Stream sharing boundary')
import win32con
import win32file
source = Path(__file__).resolve().parents[2] / 'integration/dxdesigner795/QuickPartReplace.vbs'
writer = re.search(r'(?ms)^Sub QPWriteUtf8\(.*?^End Sub', source.read_text(encoding='utf-8')).group()
script = tmp_path / 'write-result.vbs'
script.write_text('Set QP_Fso = CreateObject("Scripting.FileSystemObject")\n'
'WScript.Echo "ready"\nOn Error Resume Next\nQPWriteUtf8 WScript.Arguments(0), "verified"\n'
'If Err.Number <> 0 Then\nWScript.StdErr.WriteLine Err.Description\nWScript.Quit 1\nEnd If\n'
'WScript.Quit 0\n' + writer,
encoding='ascii')
path = tmp_path / 'project-session-result.tsv'
path.write_text('before', encoding='utf-8')
reader = win32file.CreateFile(str(path), win32con.GENERIC_READ, win32con.FILE_SHARE_READ,
None, win32con.OPEN_EXISTING, 0, None)
timer = None
process = subprocess.Popen((r'C:\Windows\SysWOW64\cscript.exe', '//nologo', str(script), str(path)),
stdout=subprocess.PIPE, stderr=subprocess.PIPE, creationflags=subprocess.CREATE_NO_WINDOW)
try:
assert process.stdout.readline().strip() == b'ready'
if release_reader:
timer = Timer(.15, reader.Close)
timer.start()
_, stderr = process.communicate(timeout=5)
if release_reader:
assert process.returncode == 0, stderr
assert path.read_text(encoding='utf-8-sig') == 'verified'
else:
assert process.returncode != 0
assert path.read_text(encoding='utf-8') == 'before'
finally:
if timer:
timer.join()
reader.Close()
if process.poll() is None:
process.kill()
process.wait()
+1 -1
View File
@@ -152,7 +152,7 @@ def test_blank_bom_can_be_downloaded_without_task_or_inventory(qtbot, tmp_path,
qtbot.waitUntil(lambda: not window._workers and output.exists())
workbook = load_workbook(output)
try:
assert tuple(cell.value for cell in workbook.active[1]) == BOM_HEADERS
assert tuple(cell.value for cell in workbook.active[1]) == BOM_HEADERS + ('value_mode',)
# The first editing row is explicitly text-formatted to preserve zeroes.
assert workbook.active.max_row == 2
assert all(cell.value is None for cell in workbook.active[2])
+5 -5
View File
@@ -104,8 +104,8 @@ def test_bom_retry_replaces_old_report_with_live_state(window, qtbot, monkeypatc
if outcome == 'error':
raise TaskCenterError('bom_import', '本次文件无法读取')
return new
monkeypatch.setattr(window.service, 'inspect_bom_batch', inspect)
monkeypatch.setattr(QFileDialog, 'getOpenFileName', lambda *args: ('retry.xlsx', ''))
monkeypatch.setattr(window.service, 'preview_bom_sources', inspect)
monkeypatch.setattr(QFileDialog, 'getOpenFileNames', lambda *args: (['retry.xlsx'], ''))
panel.import_bom()
assert 'Excel' in panel.summary.text()
try:
@@ -145,7 +145,7 @@ def test_cancelling_bom_file_picker_keeps_previous_report(window, qtbot, monkeyp
panel = window.plan_panel
report = BomImportReport((), (), errors=('原有报告',))
panel._received_bom_report(report)
monkeypatch.setattr(QFileDialog, 'getOpenFileName', lambda *args: ('', ''))
monkeypatch.setattr(QFileDialog, 'getOpenFileNames', lambda *args: ([], ''))
panel.import_bom()
assert panel.last_bom_report is report
assert '原有报告' in panel.report_text
@@ -168,8 +168,8 @@ def test_import_phases_drive_ui_and_cancelling_cannot_be_overwritten(window, qtb
assert release.wait(5)
phase(BomImportPhase.INVENTORY, 'late next project')
return BomImportReport((), (), errors=('late report',))
monkeypatch.setattr(window.service, 'inspect_bom_batch', inspect)
monkeypatch.setattr(QFileDialog, 'getOpenFileName', lambda *args: ('phased.xlsx', ''))
monkeypatch.setattr(window.service, 'preview_bom_sources', inspect)
monkeypatch.setattr(QFileDialog, 'getOpenFileNames', lambda *args: (['phased.xlsx'], ''))
panel.import_bom()
try:
qtbot.waitUntil(lambda: panel.bom_import_state.phase is BomImportPhase.MATCHING)
+255
View File
@@ -0,0 +1,255 @@
"""One real schematic-only acceptance run on new copies; no PCB execution."""
from __future__ import annotations
import argparse
from dataclasses import asdict
from datetime import datetime
import hashlib
import json
from pathlib import Path
import re
import shutil
import sys
from threading import Event
import time
import traceback
REPO = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(REPO / 'src'))
from probe_batch_runtime_live import hashes, inspect_native
OLD, NEW = '4100-CA0000-T7', '4100-CA0000-T0'
CASES = {'Audio_D2128': (('RV05', 'inherit', ''), ('RV11', 'preserve', '')),
'Audio_T605A': (('RV05', 'part_number', ''), ('RV11', 'custom', '47k'))}
def save(path, value):
path.write_text(json.dumps(value, ensure_ascii=False, indent=2, default=str), encoding='utf-8')
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--inspect-batch', type=Path)
parser.add_argument('--continue-run', type=Path, help='Continue only pending entries of the saved queue')
parser.add_argument('--reconcile-saved', type=Path, help='Verify saved uncertain result without native replacement')
args = parser.parse_args()
if args.inspect_batch:
for spec in json.loads(args.inspect_batch.read_text(encoding='utf-8')):
inspect_native(Path(spec))
return 0
from dx_productivity.trial_task_center.batch_runtime import BatchCadRuntime
from dx_productivity.trial_task_center.bom_plan import write_bom_template
from dx_productivity.trial_task_center.isolated_cad import IsolatedCadSession
from dx_productivity.trial_task_center.legacy_paths import cleanup_worker_aliases
from dx_productivity.trial_task_center.logging_setup import configure_logging
from dx_productivity.trial_task_center.service import TaskCenterService
from dx_productivity.trial_task_center.store import TaskStore
from dx_productivity.trial_task_center.task_queue import execute_task_queue
from private_desktop_observer import PrivateDesktopObserver
assert not (args.continue_run and args.reconcile_saved)
reconciling = args.reconcile_saved is not None
continuing = args.continue_run is not None or reconciling
output = ((args.continue_run or args.reconcile_saved).resolve(strict=True) if continuing else
REPO / 'build' / ('item-value-native-' + datetime.now().strftime('%Y%m%d-%H%M%S')))
assert output.is_relative_to(REPO / 'build')
if not continuing:
output.mkdir(exist_ok=False)
source_root = REPO / 'build/cbb-bom-import-live-20261015/projects'
library = Path('F:/Sync Tool Skyworth/Work Maps/Skyworth_CBB_Lib/Mentor_lib_EE7.9/mentor_lib.lmc')
previous = json.loads((output / 'report.json').read_text(encoding='utf-8')) if continuing else None
source_hashes = previous['source_hashes'] if continuing else {name: hashes(source_root / name) for name in CASES}
library_hash = hashlib.sha256(library.read_bytes()).hexdigest()
report = {'output': str(output), 'verified': False, 'pcb_execution': False,
'steps': [], 'source_hashes': source_hashes}
report_path = output / ('reconciliation-report.json' if reconciling else
'continuation-report.json' if continuing else 'report.json')
if reconciling and report_path.exists():
report_path = output / ('reconciliation-report-' + datetime.now().strftime('%H%M%S') + '.json')
if continuing:
assert not report_path.exists(), 'do not replay a previous continuation'
report['initial_failure'] = previous.get('error')
observation_copy = output / ('desktop-observation-before-reconciliation.json' if reconciling else 'desktop-observation-first-attempt.json')
if not observation_copy.exists():
shutil.copy2(output / 'desktop-observation.json', observation_copy)
if reconciling:
report['continuation_failure'] = json.loads((output / 'continuation-report.json').read_text(encoding='utf-8')).get('error')
save(report_path, report)
print('EVIDENCE=' + str(output), flush=True)
started_all = time.monotonic()
def record(stage, started=None, **details):
entry = {'stage': stage, **details}
if started is not None:
entry['seconds'] = round(time.monotonic() - started, 3)
report['steps'].append(entry)
save(report_path, report)
print(json.dumps(entry, ensure_ascii=False, default=str), flush=True)
projects = []
for name in CASES:
destination = output / 'projects' / name
if not continuing:
shutil.copytree(source_root / name, destination)
project = destination / (name + '.prj')
text, count = re.subn(r'(?m)^KEY CentralLibrary .*$',
lambda _: f'KEY CentralLibrary "{library.as_posix()}"', project.read_text(encoding='utf-8-sig'))
assert count == 1
if not continuing:
project.write_text(text, encoding='utf-8')
projects.append(project)
pcb_before = {str(p.relative_to(output)): hashlib.sha256(p.read_bytes()).hexdigest()
for project in projects for p in project.parent.rglob('*')
if p.is_file() and ('PCB' in p.relative_to(project.parent).parts or p.suffix.casefold() == '.pcb')}
store = TaskStore(output / 'tasks.db')
service = TaskCenterService(store, configure_logging(output / 'data'))
stop = Event()
try:
queue_id = None
completed_statuses = {}
if continuing:
queue_path, = tuple((output / 'data/task-queues').glob('*.json'))
queue_id = queue_path.stem
queue = json.loads(queue_path.read_text(encoding='utf-8'))
tasks = tuple(service.get_task(task_id) for task_id in queue['task_ids'])
if reconciling:
assert service.cleanup_batch_resources() == (), 'owned resources still need cleanup'
for task in tasks:
progress = service.quick_run_progress(task.task_id)
if progress.queue_complete and not service.has_interrupted_quick_run(task.task_id):
assert not progress.failed_items
status = service.for_project_task(task.task_id)._quick_run_preparer.data_root / 'quick-run/status.tsv'
completed_statuses[status] = status.read_bytes()
assert len(completed_statuses) == 1, 'continuation expects one completed and one pending project'
record('completed_project_will_not_be_replayed', queue_id=queue_id,
completed_status_paths=list(map(str, completed_statuses)))
else:
tasks = service.create_project_tasks(output / 'projects', selected_paths=tuple(projects))
assert len(tasks) == 2
for task in tasks:
if not continuing:
service.set_copy_part_number_to_value(task.task_id, True)
cache = REPO / 'build/explicit-cell-final-bom-20261019/data/cell-catalog'
if not continuing and cache.is_dir():
shutil.copytree(cache, output / 'data/cell-catalog')
if not continuing:
started = time.monotonic()
choices = service.target_cell_choices(tasks[0].task_id, NEW)
assert 'RESC1608N_0603' in choices, choices
service.add_rule(tasks[0].task_id, OLD, NEW, target_cell='RESC1608N_0603')
record('validated_target_cell_choices', started, choices=choices)
sources = []
for task in tasks:
project = service.projects(task.task_id)[0].path
records = tuple({'project_name': project.name,
'project_path': 'Z:/migrated-old-location/' + project.name,
'old_part_number': OLD, 'new_part_number': NEW, 'refdes': ref,
'enabled': True, 'sync_nc': True, 'value_mode': mode, 'custom_value': custom}
for ref, mode, custom in CASES[project.stem])
if not continuing:
sources.append(write_bom_template(output / (project.stem + '.xlsx'), records))
with PrivateDesktopObserver(output):
with BatchCadRuntime(output / 'data', 'item_value_schematic_live', stop) as runtime:
if reconciling:
uncertain = tuple(task for task in tasks if service.has_interrupted_quick_run(task.task_id))
assert len(uncertain) == 1
started = time.monotonic()
recovered = service.recover_quick_run(uncertain[0].task_id, confirmed=True, runtime=runtime)
assert recovered.state == 'restored_ok', recovered
record('saved_result_reconciled_without_native_replacement', started,
task_id=uncertain[0].task_id, result=asdict(recovered))
if not continuing:
started = time.monotonic()
imported = service.preview_bom_sources(tasks[0].task_id, sources, runtime=runtime,
stop_event=stop, on_progress=lambda message: print(message, flush=True))
assert imported.preview is not None and imported.passed_rows == 4 and not imported.failed_rows, imported
assert all('.xlsx' in row.reason for row in imported.rows)
service.save_bom_batch(imported.preview, {p.task_id: p.rows for p in imported.preview.projects})
record('two_migrated_excel_files_imported_and_saved', started, rows=[asdict(row) for row in imported.rows])
before = {task.task_id: service.target_plan(task.task_id) for task in tasks}
assert all(sum(row.enabled for row in rows) == 2 for rows in before.values())
started = time.monotonic()
result = execute_task_queue(service, tuple(task.task_id for task in tasks), stop_event=stop,
runtime=runtime, queue_id=queue_id)
runtime.result = result
record('real_schematic_replacement_saved_and_reopened', started, result=asdict(result))
assert not result.paused and all(entry.state == 'completed' for entry in result.entries), result
assert all(path.read_bytes() == content for path, content in completed_statuses.items()), 'completed steps were replayed'
for task in tasks:
facade = service.for_project_task(task.task_id)
command = facade._quick_run_preparer.data_root / 'quick-run/command.tsv'
lines = command.read_text(encoding='utf-8-sig').splitlines()
commands = [dict(zip(lines[0].split('\t'), line.split('\t'))) for line in lines[1:]]
record('frozen_item_commands', task_id=task.task_id, commands=commands)
# A fresh CAD process independently reads the saved projects once.
specs, destinations = [], {}
evidence = output / 'independent-read/quick-run-history/sheet-discovery-item-values'
evidence.mkdir(parents=True)
for task in tasks:
project = service.projects(task.task_id)[0].path
spec, destination = evidence / (project.stem + '-spec.json'), output / (project.stem + '-saved.json')
sheets = sorted({(row.target.schematic_name, row.target.sheet_number) for row in before[task.task_id]})
save(spec, {'project': str(project), 'sheets': sheets, 'destination': str(destination)})
specs.append(str(spec))
destinations[task.task_id] = destination
spec_list = evidence / 'specs.json'
save(spec_list, specs)
started = time.monotonic()
with IsolatedCadSession(evidence) as cad:
process = cad.start_worker((sys.executable, '-X', 'utf8', str(Path(__file__).resolve()),
'--inspect-batch', str(spec_list)), stdout=-1, stderr=-1)
try:
code = process.wait(timeout=180)
(evidence / 'worker-output.txt').write_bytes(process.stdout.read() + b'\n' + process.stderr.read())
assert code == 0, (evidence / 'worker-output.txt').read_text(encoding='utf-8', errors='replace')
finally:
process.close()
verified = []
for task in tasks:
actual = json.loads(destinations[task.task_id].read_text(encoding='utf-8'))
selected = {row['refdes']: row for row in actual.values() if row['refdes'] in {'RV05', 'RV11'}}
assert len(selected) == 2
for row in before[task.task_id]:
if not row.enabled:
continue
current = selected[row.target.refdes]
assert current['uid'] == row.target.uid and current['part_number'] == NEW, current
mode = 'part_label' if row.value_mode == 'inherit' else row.value_mode
expected = {'part_label': current['part_label'], 'part_number': NEW,
'custom': row.custom_value, 'preserve': ''}[mode]
assert current['value'] == expected, (mode, current)
if mode == 'preserve':
assert 'Value' not in current['attributes'], current
assert current['part_label'] == '0R' and current['pins']
verified.append({'project': task.name, 'refdes': row.target.refdes, 'uid': current['uid'],
'mode': mode, 'part_number': current['part_number'], 'value': current['value'],
'part_label': current['part_label'], 'pins': len(current['pins'])})
record('fresh_cad_independent_saved_readback', started, targets=verified)
observation = json.loads((output / 'desktop-observation.json').read_text(encoding='utf-8'))
assert not observation['observer_errors'] and not observation['owned_cad_became_foreground']
assert not observation['owned_cad_on_input_desktop']
assert all(observation['cad_exit_confirmed'].values()) and all(observation['desktop_absent'].values())
report['verified'] = True
except BaseException as error:
report['error'] = f'{type(error).__name__}: {error}'
report['traceback'] = traceback.format_exc()
traceback.print_exc()
finally:
report['source_projects_unchanged'] = all(hashes(source_root / name) == source_hashes[name] for name in CASES)
report['library_lmc_unchanged'] = hashlib.sha256(library.read_bytes()).hexdigest() == library_hash
report['pcb_files_unchanged'] = all((output / name).is_file() and hashlib.sha256((output / name).read_bytes()).hexdigest() == digest
for name, digest in pcb_before.items())
store.close()
report['alias_cleanup_errors'] = list(map(str, cleanup_worker_aliases()))
report['elapsed_seconds'] = round(time.monotonic() - started_all, 3)
save(report_path, report)
print(json.dumps({key: report.get(key) for key in ('verified', 'error', 'source_projects_unchanged',
'library_lmc_unchanged', 'pcb_execution', 'pcb_files_unchanged', 'elapsed_seconds')}, ensure_ascii=False), flush=True)
return 0 if (report['verified'] and report['source_projects_unchanged'] and report['library_lmc_unchanged']
and report['pcb_files_unchanged'] and not report['alias_cleanup_errors']) else 1
if __name__ == '__main__':
raise SystemExit(main())