g85/g85/write.py

151 lines
4.9 KiB
Python
Raw Normal View History

2024-07-29 21:50:44 -07:00
from typing import TextIO, cast
from collections.abc import Sequence
2021-11-11 23:53:13 -08:00
import logging
import math
from dataclasses import fields
from xml.etree import ElementTree
from .main import Map, Device
logger = logging.getLogger(__name__)
2024-07-29 21:50:31 -07:00
class G85Error(Exception):
pass
2021-11-11 23:53:13 -08:00
# Hack to directly pass through <![CDATA[...]]>
2024-07-29 21:50:44 -07:00
def _escape_cdata(text: str) -> str:
2021-11-11 23:53:13 -08:00
if text.startswith('<![CDATA[') and text.endswith(']]>'):
return text
2024-07-29 21:50:57 -07:00
return _original_escape_cdata(text)
2021-11-11 23:53:13 -08:00
_original_escape_cdata = ElementTree._escape_cdata # type: ignore
ElementTree._escape_cdata = _escape_cdata # type: ignore
####
def write(maps: Sequence[Map], stream: TextIO) -> None:
el_root = ElementTree.Element('Maps')
for wmap in maps:
write_wmap(wmap, el_root)
tree = ElementTree.ElementTree(element=el_root)
ElementTree.indent(tree)
tree.write(stream)
def write_wmap(wmap: Map, el_root: ElementTree.Element) -> None:
el_map = ElementTree.SubElement(el_root, 'Map')
write_devices(wmap.devices, el_map)
map_fields = [ff.name for ff in fields(wmap)]
for field in map_fields:
2023-07-13 14:10:31 -07:00
if field[0].isupper() or field == 'xmlns':
2021-11-12 14:18:10 -08:00
val = getattr(wmap, field)
2021-11-12 14:01:00 -08:00
if val is None:
continue
2021-11-12 14:05:25 -08:00
el_map.set(field, val)
2021-11-11 23:53:13 -08:00
for key, value in wmap.misc.items():
if key[0].isupper() and key in map_fields:
continue
el_map.set(key, value)
def write_devices(devices: Sequence[Device], el_map: ElementTree.Element) -> None:
for device in devices:
el_device = ElementTree.SubElement(el_map, 'Device')
# ReferenceDevice
if device.reference_xy is not None:
el_ref = ElementTree.SubElement(el_device, 'ReferenceDevice')
el_ref.set('ReferenceDeviceX', str(device.reference_xy[0]))
el_ref.set('ReferenceDeviceY', str(device.reference_xy[1]))
# Row data prep
if device.map is None:
2024-07-29 21:50:31 -07:00
raise G85Error(f'No _data for device pformat({device})')
2021-11-11 23:53:13 -08:00
is_decimal = device.BinType == 'Decimal'
row_texts, bin_length = prepare_data(device.map, decimal=is_decimal)
# Bins
if not device.bin_pass:
logger.warning('No bins were provided!')
bin_counts = device.bin_counts()
for bin_code, passed in device.bin_pass.items():
el_bin = ElementTree.SubElement(el_device, 'Bin')
if is_decimal:
el_bin.set('BinCode', str(bin_code).zfill(bin_length))
else:
el_bin.set('BinCode', str(bin_code))
el_bin.set('BinQuality', 'Pass' if passed else 'Fail')
el_bin.set('BinCount', str(bin_counts[bin_code]))
2023-07-13 14:02:14 -07:00
el_data = ElementTree.SubElement(el_device, 'Data')
2021-11-11 23:53:13 -08:00
for row_text in row_texts:
2023-07-13 14:02:14 -07:00
el_row = ElementTree.SubElement(el_data, 'Row')
2021-11-11 23:53:13 -08:00
el_row.text = f'<![CDATA[{row_text}]]>'
# Device attribs
dev_fields = [ff.name for ff in fields(device)]
for field in dev_fields:
if field[0].isupper():
val = getattr(device, field)
2021-11-12 14:01:00 -08:00
if val is None:
continue
2021-11-11 23:53:13 -08:00
if field in ('WaferSize', 'DeviceSizeX', 'DeviceSizeY', 'Orientation'):
val = f'{val:g}'
elif field in ('OriginLocation',):
val = f'{val:d}'
elif field == 'CreateDate':
val = val.strftime('%Y%m%d%H%M%S%f')[:-3]
2021-11-12 14:01:18 -08:00
elif field == 'NullBin' and device.BinType == 'Decimal':
val = f'{val:d}'
2021-11-11 23:53:13 -08:00
el_device.set(field, val)
for key, value in device.misc.items():
if key[0].isupper() and key in dev_fields:
continue
el_device.set(key, value)
for key, value in device.data_misc.items():
el_data.set(key, value)
if device.supplier_data:
el_suppdata = ElementTree.SubElement(el_device, 'SupplierData')
for key, value in device.data_misc.items():
el_suppdata.set(key, value)
2021-11-11 23:53:13 -08:00
2023-07-13 13:16:01 -07:00
def prepare_data(data: list[list[str]] | list[list[int]], decimal: bool) -> tuple[list[str], int]:
2021-11-11 23:53:13 -08:00
is_char = isinstance(data[0][0], str)
2023-07-13 13:16:01 -07:00
row_texts = []
2021-11-11 23:53:13 -08:00
if is_char:
2023-07-13 13:16:01 -07:00
data = cast(list[list[str]], data)
2021-11-11 23:53:13 -08:00
char_len = len(data[0][0])
2023-07-13 13:16:01 -07:00
for srow in data:
if char_len == 1:
row_text = ''.join(srow)
else:
row_text = ' '.join(srow) + ' '
row_texts.append(row_text)
return row_texts, char_len
2024-07-29 21:50:57 -07:00
else: # noqa: RET505
2023-07-13 13:16:01 -07:00
data = cast(list[list[int]], data)
2021-11-11 23:53:13 -08:00
max_value = max(max(rr) for rr in data)
max_digits = math.ceil(math.log10(max_value))
2023-07-13 13:16:01 -07:00
for irow in data:
row_text = ' '.join(str(vv).zfill(max_digits) for vv in irow) + ' '
row_texts.append(row_text)
2021-11-11 23:53:13 -08:00
return row_texts, max_digits