schlib_write: encode parameter records as UTF-8 (fixes crash on Ω/µ and other non-latin-1 chars in template columns like ESR(mΩ))

main
admin 2026-07-15 09:00:13 +00:00
parent 7ff9751504
commit 2719f5c75b
No known key found for this signature in database
2 changed files with 5 additions and 5 deletions

Binary file not shown.

View File

@ -113,7 +113,7 @@ def _uid(name):
def _param_record(idx, name, value): def _param_record(idx, name, value):
s=(f"|RECORD=41|IndexInSheet={idx}|OwnerPartId=1|Justification=4|FontID=2|IsHidden=T" s=(f"|RECORD=41|IndexInSheet={idx}|OwnerPartId=1|Justification=4|FontID=2|IsHidden=T"
f"|Text={value}|Name={name}|UniqueID={_uid(name)}") f"|Text={value}|Name={name}|UniqueID={_uid(name)}")
payload=s.encode('latin-1')+b'\x00' payload=s.encode('utf-8')+b'\x00' # utf-8: handles Ω, µ, °, ± in names/values
return struct.pack('<I',len(payload))+payload return struct.pack('<I',len(payload))+payload
@ -134,7 +134,7 @@ def _leading_text_records(data):
def _rec_name(block): def _rec_name(block):
t=block[4:-1].decode('latin-1') t=block[4:-1].decode('utf-8','replace')
return t.split('|Name=')[1].split('|')[0] if '|Name=' in t else None return t.split('|Name=')[1].split('|')[0] if '|Name=' in t else None
@ -142,12 +142,12 @@ def _patch_field(block, field, value):
"""Replace |field=...| inside a length-prefixed text record, re-framing its 4-byte length. """Replace |field=...| inside a length-prefixed text record, re-framing its 4-byte length.
Used to set the component's ComponentDescription in the RECORD=1 header.""" Used to set the component's ComponentDescription in the RECORD=1 header."""
import re import re
text = block[4:-1].decode('latin-1') text = block[4:-1].decode('utf-8', 'replace')
if f"|{field}=" in text: if f"|{field}=" in text:
text = re.sub(rf"\|{re.escape(field)}=[^|]*", f"|{field}={value}", text, count=1) text = re.sub(rf"\|{re.escape(field)}=[^|]*", f"|{field}={value}", text, count=1)
elif text.startswith("|RECORD="): elif text.startswith("|RECORD="):
text = text + f"|{field}={value}" text = text + f"|{field}={value}"
payload = text.encode('latin-1') + b'\x00' payload = text.encode('utf-8') + b'\x00'
return struct.pack('<I', len(payload)) + payload return struct.pack('<I', len(payload)) + payload
@ -286,7 +286,7 @@ def write_params(schlib, params_json, out, typeid=None, template=None):
ok=True ok=True
for e in ole.listdir(streams=True): for e in ole.listdir(streams=True):
if e[-1].lower()=="data": if e[-1].lower()=="data":
t=ole.openstream(e).read().decode('latin-1','ignore') t=ole.openstream(e).read().decode('utf-8','ignore')
for nm in fields: for nm in fields:
if f"|Name={nm}|" not in t and f"|Name={nm}\x00" not in t and f"Name={nm}" not in t: if f"|Name={nm}|" not in t and f"|Name={nm}\x00" not in t and f"Name={nm}" not in t:
ok=False ok=False