Files
yiyezhqiu-refactored/tools/acceptance/native_read_tools.py
T

92 lines
4.6 KiB
Python

"""Exercise visible read controls and native Excel output on a dedicated copy."""
import argparse
import json
from pathlib import Path
import shlex
import time
from pywinauto import Application
import win32com.client
from packaged_menu import tools_running, wait_for
from toolbar_ui import ToolbarUI
def main():
parser = argparse.ArgumentParser()
parser.add_argument('executable', type=Path)
parser.add_argument('--project', required=True, type=Path)
parser.add_argument('--evidence', required=True, type=Path)
options = parser.parse_args()
exe, project, base = options.executable.resolve(), options.project.resolve(), options.evidence.resolve()
assert 'acceptance' in project.parts
base.mkdir(parents=True, exist_ok=True)
tool = wait_for(lambda: next((p for p in tools_running() if Path(p.executable).resolve() == exe
and '--request' in p.command_line), None))
args = shlex.split(tool.command_line, posix=False)
request = Path(args[args.index('--request') + 1].strip('"'))
snapshot = json.loads(request.read_text(encoding='utf-8-sig'))
assert Path(snapshot['design_path']).resolve() == project
ui = ToolbarUI(tool.pid)
if ui.panel.exists(timeout=0.2) and ui.panel.is_visible():
ui.collapse()
results = []
for page, checks in (('设计检查', ('孤立网络', '全部网络', 'Value 遗漏', 'Value1 遗漏')),
('网络工具', ('全部网络', '跨页查询'))):
ui.feature(page)
combo = ui.panel.child_window(title='检查类型', control_type='ComboBox')
for check in checks:
types = ('孤立网络', '全部网络', '跨页查询') if page == '网络工具' else (
'孤立网络', '全部网络', 'Value 遗漏', 'Value1 遗漏', '跨页查询')
ui.select_combo(combo, types.index(check), check)
if check == '跨页查询':
ui.panel.child_window(control_type='Edit').set_edit_text('GND')
ui.panel.child_window(title='运行检查', control_type='Button').invoke()
prefix = {'孤立网络': '孤立网络:', '全部网络': '网络总数:',
'Value 遗漏': 'Value 遗漏:', 'Value1 遗漏': 'Value1 遗漏:',
'跨页查询': 'GND 出现在'}[check]
summary = wait_for(lambda: next((text for text in ui.texts(ui.panel) if text.startswith(prefix)), None), 10)
results.append({'page': page, 'operation': check, 'result': summary})
print('PASS NATIVE READ TOOL', page, check, summary, flush=True)
ui.collapse()
ui.feature('BOM 工具')
wait_for(lambda: any(text.startswith('BOM 行数:') for text in ui.texts(ui.panel)), 10)
output = base / 'BOM-工具栏.xls'
if output.exists():
raise AssertionError('Use a fresh evidence directory to retain the old workbook')
ui.panel.child_window(title='导出 Excel…', control_type='Button').click_input()
dialog = ui.native_dialog('导出 BOM')
dialog.wait('visible', timeout=10)
filename = dialog.child_window(control_id=1001, class_name='Edit')
filename.set_edit_text(str(output))
save = dialog.child_window(control_id=1, class_name='Button')
save.click_input()
ui.collapse()
wait_for(lambda: output.is_file() and output.stat().st_size > 4096, 60)
status = request.with_name('bom-export-status.json')
wait_for(lambda: status.is_file(), 10)
excel = win32com.client.DispatchEx('Excel.Application')
excel.Visible = False
excel.DisplayAlerts = False
workbook = None
try:
workbook = excel.Workbooks.Open(str(output), ReadOnly=True)
values = workbook.Worksheets(1).UsedRange.Value
text = '\n'.join(str(value) for row in values for value in row if value is not None)
assert 'CE17' in text and 'LE03' in text, text
assert len(values) > 10
results.append({'page': 'BOM 工具', 'output': str(output), 'used_rows': len(values),
'real_component_refdes_verified': True, 'completed_while_collapsed': True,
'native_status': json.loads(status.read_text(encoding='utf-8-sig'))})
finally:
if workbook is not None:
workbook.Close(SaveChanges=False)
excel.Quit()
(base / 'native-read-tools.json').write_text(json.dumps({'pid': tool.pid,
'project': str(project), 'cases': results, 'design_values_written': False}, ensure_ascii=False, indent=2), encoding='utf-8')
print('PASS NATIVE BOM WORKBOOK', str(output), flush=True)
if __name__ == '__main__':
main()