feat: support item value policies and migrated multi-file BOM import
Core regression / Python core and release configuration (push) Successful in 17s
Core regression / JavaScript syntax (push) Successful in 2s
Core regression / Python core and release configuration (pull_request) Successful in 14s
Core regression / JavaScript syntax (pull_request) Successful in 3s

This commit is contained in:
阿白叔 committed 2026-11-05 11:26:35 +08:00
1 parent 9316cf4fc4
commit 8b5509d64f
29 files changed
+1418 -63

No files matched your search

@@ -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 个小用例,没有重复运行整套回归。
+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.
@@ -253,6 +253,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 +305,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
@@ -318,7 +319,7 @@ def inspect_batch(service, task_id, path, *, stop_event=None, on_progress=None,
', '.join(str(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':
@@ -41,16 +41,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:
records = tuple(records)
headers = BOM_HEADERS + (('value_mode',) if 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 +69,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 +104,7 @@ class BomRecord:
enabled: bool
custom: str
sync_nc: bool
value_mode: str = 'inherit'
def read_bom_records(path: Path) -> tuple[BomRecord, ...]:
@@ -151,13 +159,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 +209,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
@@ -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(
@@ -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,52 @@ 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."""
from .bom_batch import _scope
from .bom_import_report import BomImportReport
from .workspace_bom import load_sources, route_sources, write_routed_bom
from uuid import uuid4
def normalize():
rows = load_sources(tuple(Path(source) for source in sources), stop_event=stop_event)
scope = _scope(self._store, task_id)
paths = tuple(project.path for _, project in scope)
routes = route_sources(rows, paths, single=len(paths) == 1)
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}')
root = self._logging.root / 'imports' / uuid4().hex
normalized = write_routed_bom(root / 'normalized-bom.xlsx', rows, routes)
return normalized, rows
normalized, sources_by_row = self._idle_quick_run_operation('preview_bom_sources', task_id, normalize)
def annotate(report):
if report is None:
return report
annotated = tuple(
replace(row, reason=(row.reason + ';来源:' +
f"{sources_by_row[row.excel_row - 2].source} 第 {sources_by_row[row.excel_row - 2].excel_row} 行")
if 2 <= row.excel_row <= len(sources_by_row) + 1 else row.reason)
for row in report.rows)
return replace(report, rows=annotated)
# Reuse the existing supervised reader and its cancellation/resource gates.
try:
report = self.inspect_bom_batch(task_id, normalized, stop_event=stop_event,
on_progress=on_progress, on_phase=on_phase, runtime=runtime)
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
return annotate(report)
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 +345,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 +397,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)
@@ -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)
@@ -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,185 @@
"""Safe multi-file BOM loading and project routing for workspace imports."""
from dataclasses import dataclass
import json
from pathlib import Path
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):
"""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 path in paths:
check_cancelled(stop_event)
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
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):
"""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:
# 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('/')))
if len(suffix) == 1:
return suffix[0]
if len(suffix) > 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):
"""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)
if chosen is None and single and len(paths) == 1 and not row.project_name and not row.project_path:
chosen = paths[0]
routes[key] = chosen
return routes
def write_routed_bom(destination, rows, routes):
"""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)
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': 'migrated_exact_name' if _path_key(row.project_path) != _path_key(target.resolve()) else 'exact_path'})
destination = Path(destination)
destination.parent.mkdir(parents=True, exist_ok=True)
result = write_bom_template(destination, records)
destination.with_name('source-map.json').write_text(
json.dumps(source_map, ensure_ascii=False, indent=2), encoding='utf-8')
return result
@@ -0,0 +1,479 @@
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)
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 seen['path'].is_file()
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 == 'inspect_bom_batch'
assert Path(arguments['path']).is_file()
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'
+2 -2
View File
@@ -249,7 +249,7 @@ 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)'))
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
@@ -278,7 +278,7 @@ 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)'))
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)
@@ -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()
+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())