36 lines
1.7 KiB
Python
36 lines
1.7 KiB
Python
"""Export only .drawio inputs, verify SVG content, and build PNG previews.
|
|
|
|
Run with ~/.venvs/codex-schematics/bin/python. The desktop renderer may need
|
|
execution outside a restricted Electron sandbox. It never edits source XML.
|
|
"""
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
import xml.etree.ElementTree as ET
|
|
import cairosvg
|
|
|
|
OUT=Path(__file__).resolve().parent
|
|
sources=sorted(OUT.glob('*.drawio'))
|
|
assert sources, 'No draw.io sources found'
|
|
with tempfile.TemporaryDirectory(prefix='neoecu-drawio-') as tmp:
|
|
for source in sources: shutil.copy2(source,Path(tmp)/source.name)
|
|
subprocess.run(['/usr/bin/drawio','-x','-f','svg','-e','-b','20','--svg-theme','light','-o',str(OUT),tmp],check=True)
|
|
|
|
ET.register_namespace('', 'http://www.w3.org/2000/svg')
|
|
ET.register_namespace('xlink', 'http://www.w3.org/1999/xlink')
|
|
for source in sources:
|
|
svg=source.with_suffix('.svg')
|
|
tree=ET.parse(svg);root=tree.getroot()
|
|
assert any(e.tag.endswith('text') for e in root.iter()),f'Empty figure: {svg}'
|
|
# draw.io emits an irrelevant fallback warning even for native SVG text.
|
|
# Remove only that advisory, and only when there is no foreignObject text.
|
|
assert not any(e.tag.endswith('foreignObject') for e in root.iter()),f'Nonportable SVG text: {svg}'
|
|
for parent in root.iter():
|
|
for child in list(parent):
|
|
if child.tag.endswith('switch') and 'Text is not SVG - cannot display' in ''.join(child.itertext()):
|
|
parent.remove(child)
|
|
tree.write(svg,encoding='utf-8',xml_declaration=True)
|
|
cairosvg.svg2png(url=str(svg),write_to=str(svg.with_suffix('.png')),background_color='white')
|
|
print(f'Exported and checked {len(sources)} block figures.')
|