87 lines
4.3 KiB
Python
87 lines
4.3 KiB
Python
"""Export all three PDF ranges through the real toolbar on an acceptance copy."""
|
|
import argparse
|
|
import json
|
|
from pathlib import Path
|
|
import shlex
|
|
import time
|
|
|
|
from pypdf import PdfReader
|
|
from pypdf.errors import PdfReadError
|
|
from packaged_menu import tools_running, wait_for
|
|
from pdf_palette import color_operations
|
|
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()
|
|
if 'acceptance' not in project.parts:
|
|
raise SystemExit('PDF acceptance requires an explicit acceptance project')
|
|
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()
|
|
ui.feature('PDF 导出')
|
|
scope = ui.panel.child_window(title='导出范围', control_type='ComboBox')
|
|
results = []
|
|
for index, name in enumerate(('project', 'page', 'block')):
|
|
output = base / name
|
|
if output.exists():
|
|
raise AssertionError(f'Retain previous evidence and choose a fresh directory: {output}')
|
|
ui.select_combo(scope, index, ('整工程', '当前页', '指定原理图或 Block')[index])
|
|
edits = [e for e in ui.panel.descendants(control_type='Edit')
|
|
if e.is_visible() and not e.iface_value.CurrentIsReadOnly]
|
|
if name == 'block':
|
|
assert len(edits) == 2, edits
|
|
edits[0].set_edit_text(snapshot['active_block_name'])
|
|
edits[-1].set_edit_text(str(output))
|
|
ui.panel.child_window(title='导出 PDF', control_type='Button').invoke()
|
|
began = time.monotonic()
|
|
ui.collapse()
|
|
# sch2pdf publishes a raw file before the font repair has finished.
|
|
# The toolbar's completion message is emitted after that repair.
|
|
wait_for(lambda: '任务:PDF 已导出' in ui.status_detail()
|
|
and str(output) in ui.status_detail(), 90)
|
|
def published():
|
|
candidates = tuple(output.glob('*.pdf'))
|
|
if not candidates:
|
|
return None
|
|
assert len(candidates) == 1, candidates
|
|
try:
|
|
pdf = PdfReader(candidates[0])
|
|
return candidates[0], pdf if len(pdf.pages) else None
|
|
except (OSError, ValueError, PdfReadError):
|
|
return None
|
|
path, pdf = wait_for(published, 90)
|
|
assert len(pdf.pages) == 1
|
|
text = '\n'.join(page.extract_text() for page in pdf.pages)
|
|
for expected in ('输入网络', '输出网络', '靠近CPU放置', '电源输入3V3'):
|
|
assert expected in text, (name, expected)
|
|
colors = color_operations(path)
|
|
assert colors[(1., 0., 0.)] > 0 and colors[(0., 0., 1.)] > 0, colors
|
|
ui.expand()
|
|
wait_for(lambda: ui.panel.child_window(title='导出 PDF', control_type='Button').is_enabled(), 10)
|
|
result = {'scope': name, 'pdf': str(path), 'pages': len(pdf.pages), 'chinese_text_verified': True,
|
|
'reference_red_blue_verified': True, 'export_finished_while_collapsed': True,
|
|
'elapsed_seconds': round(time.monotonic() - began, 2)}
|
|
results.append(result)
|
|
ui.capture(ui.panel).save(str(base / f'{name}-page.png'))
|
|
print('PASS TOOLBAR PDF', json.dumps(result, ensure_ascii=False), flush=True)
|
|
ui.panel.child_window(title='打开输出目录', control_type='Button').invoke()
|
|
(base / 'pdf-toolbar-acceptance.json').write_text(json.dumps({'pid': tool.pid,
|
|
'project': str(project), 'cases': results, 'output_directory_button_invoked': True}, ensure_ascii=False, indent=2), encoding='utf-8')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|