feat(mlg): write .MLG files as Blastware does; retire the old writer
mlg_bytes()/write_mlg() rebuild all 12 fixture files byte for byte: little-endian times, length-led records, each record's checksum. The old blastware_file writer (big-endian times, a zero CRC at the wrong end, 308- byte header) and the read_mlg stub are removed; nothing called them. No re-export: event_file_io imports blastware_file, and mlg_file imports event_file_io. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ALX2R9LdWawLzUxwewZGA1
This commit is contained in:
1 parent
3cd95db405
commit
a8adb03d23
3 files changed
+70
-220
No files matched your search
@@ -139,3 +139,47 @@ def parse_mlg(data: bytes) -> MlgFile:
|
||||
def read_mlg(path: Union[str, Path]) -> MlgFile:
|
||||
"""Read and parse a .MLG file."""
|
||||
return parse_mlg(Path(path).read_bytes())
|
||||
|
||||
|
||||
# ── Writer ────────────────────────────────────────────────────────────────────
|
||||
|
||||
def _ts_bytes(ts: Optional[datetime.datetime]) -> bytes:
|
||||
if ts is None:
|
||||
return bytes(8)
|
||||
return bytes([ts.day, ts.month]) + struct.pack("<H", ts.year) + bytes([0, ts.hour, ts.minute, ts.second])
|
||||
|
||||
|
||||
def _serial_field(serial: str) -> bytes:
|
||||
return serial.encode("ascii", errors="replace")[:10].ljust(10, b"\x00")
|
||||
|
||||
|
||||
def _sealed(body: bytes) -> bytes:
|
||||
"""`body` is record[0:288]; append the two zero bytes and the LE checksum."""
|
||||
return body + b"\x00\x00" + struct.pack("<H", crc16_aug_ccitt(body))
|
||||
|
||||
|
||||
def _header_record(serial: str) -> bytes:
|
||||
# Every byte but the serial is zero on all 1,273 DL2 files (measured 2026-11-13).
|
||||
body = struct.pack("<H", _RECORD_LENGTH) + _HEADER_ID + bytes(20) + _serial_field(serial)
|
||||
return _sealed(body.ljust(288, b"\x00"))
|
||||
|
||||
|
||||
def _log_record_bytes(r: MlgRecord) -> bytes:
|
||||
body = (struct.pack("<H", _RECORD_LENGTH) + _LOG_ID + _ts_bytes(r.start) + _ts_bytes(r.stop)
|
||||
+ struct.pack("<HH", r.flags, r.trigger_source) + _serial_field(r.serial)
|
||||
+ r.text_raw.ljust(254, b"\x00")[:254])
|
||||
return _sealed(body)
|
||||
|
||||
|
||||
def mlg_bytes(mlg: MlgFile) -> bytes:
|
||||
"""The bytes of a .MLG file, as Blastware writes them: little-endian times,
|
||||
length-led records, each record's checksum computed. An empty MlgFile is
|
||||
the prefix alone."""
|
||||
if mlg.empty:
|
||||
return MLG_PREFIX
|
||||
return MLG_PREFIX + _header_record(mlg.serial or "") + b"".join(_log_record_bytes(r) for r in mlg.records)
|
||||
|
||||
|
||||
def write_mlg(mlg: MlgFile, path: Union[str, Path]) -> None:
|
||||
"""Write `mlg` to `path` (see mlg_bytes)."""
|
||||
Path(path).write_bytes(mlg_bytes(mlg))
|
||||
Reference in new issue
Block a user