Skip to content

Commit 197810a

Browse files
committed
add zip container extraction and UTF-16 decoding for VEC files
1 parent b8e2e0f commit 197810a

2 files changed

Lines changed: 32 additions & 18 deletions

File tree

‎pyarinc/arinc767/vec_parser.py‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import logging
55
import re
6+
import zipfile
67
from pathlib import Path
78
from typing import Any
89

@@ -17,15 +18,29 @@
1718

1819

1920
def _read_text_file(path: Path) -> str:
20-
"""Read text file with automatic encoding fallback."""
21-
for encoding in ("utf-8", "latin-1", "cp1252"):
21+
"""Read text file, safely handling ZIP archives, UTF-16 BOMs, and standard encodings."""
22+
if zipfile.is_zipfile(path):
23+
with zipfile.ZipFile(path, "r") as z:
24+
namelist = z.namelist()
25+
par_files = [f for f in namelist if f.endswith(".par")]
26+
target_file = par_files[0] if par_files else namelist[0]
27+
raw_bytes = z.read(target_file)
28+
else:
29+
raw_bytes = path.read_bytes()
30+
31+
# Check if bytes actually have a UTF-16 BOM (FF FE or FE FF) or start with null bytes characteristic of UTF-16 text
32+
encodings = ("utf-8", "latin-1", "cp1252")
33+
if raw_bytes.startswith((b"\xff\xfe", b"\xfe\xff")):
34+
encodings = ("utf-16",) + encodings
35+
36+
for encoding in encodings:
2237
try:
23-
return path.read_text(encoding=encoding).strip()
38+
return raw_bytes.decode(encoding).strip()
2439
except UnicodeDecodeError:
2540
continue
26-
raise UnicodeDecodeError(
27-
f"Failed to decode VEC file with standard encodings: {path}"
28-
)
41+
42+
# Absolute fallback
43+
return raw_bytes.decode("utf-8", errors="ignore").strip()
2944

3045

3146
def _parse_bitrange_767(token: str) -> dict[str, int] | None:
@@ -117,7 +132,6 @@ def parse_vec_file_767(path: Path) -> dict[str, Any]:
117132

118133
if key == "FID":
119134
try:
120-
# Support both decimal and hex (e.g. FID=0x0F)
121135
base = 16 if val.lower().startswith("0x") else 10
122136
entry["frame_id_767"] = int(val, base)
123137
except ValueError:

‎tests/test_767.py‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
def test_vec_767_parsing(tmp_path: Path):
1313
text = "MACH W0B0-8 1.0 FID=3 COB=raw*0.00390625"
1414
p = tmp_path / "test.vec"
15-
p.write_text(text)
15+
p.write_text(text, encoding="utf-8")
1616

1717
mapping = parse_vec_file_767(p)
1818
assert "MACH" in mapping
@@ -137,7 +137,7 @@ def test_vec_parser_767_tokens_and_hex_fid(tmp_path: Path) -> None:
137137
"""
138138

139139
p = tmp_path / "test.vec"
140-
p.write_text(text)
140+
p.write_text(text, encoding="utf-8")
141141

142142
mapping = parse_vec_file_767(p)
143143

@@ -181,7 +181,7 @@ def test_vec_parser_767_tokens_and_hex_fid(tmp_path: Path) -> None:
181181
def test_vec_767_type_token(tmp_path: Path):
182182
text = "ALT W0B0-7 TYPE=BNR 1.0"
183183
p = tmp_path / "test.vec"
184-
p.write_text(text)
184+
p.write_text(text, encoding="utf-8")
185185

186186
mapping = parse_vec_file_767(p)
187187
assert mapping["ALT"]["type"] == "BNR"
@@ -193,7 +193,7 @@ def test_vec_767_type_token(tmp_path: Path):
193193
def test_vec_767_signed_token(tmp_path: Path):
194194
text = "ALT W0B0-7 TYPE=BNR SIGNED=true 1.0"
195195
p = tmp_path / "test.vec"
196-
p.write_text(text)
196+
p.write_text(text, encoding="utf-8")
197197

198198
mapping = parse_vec_file_767(p)
199199
assert mapping["ALT"]["signed"] is True
@@ -205,7 +205,7 @@ def test_vec_767_signed_token(tmp_path: Path):
205205
def test_vec_767_scale_offset(tmp_path: Path):
206206
text = "ALT W0B0-7 TYPE=BNR SCALE=0.01 OFFSET=100 1.0"
207207
p = tmp_path / "test.vec"
208-
p.write_text(text)
208+
p.write_text(text, encoding="utf-8")
209209

210210
mapping = parse_vec_file_767(p)
211211
assert mapping["ALT"]["scale"] == 0.01
@@ -219,7 +219,7 @@ def test_vec_767_scale_offset(tmp_path: Path):
219219
def test_vec_767_hex_fid_parsed(tmp_path: Path):
220220
text = "ALT W0B0-7 TYPE=BNR FID=0x03 1.0"
221221
p = tmp_path / "test.vec"
222-
p.write_text(text)
222+
p.write_text(text, encoding="utf-8")
223223

224224
mapping = parse_vec_file_767(p)
225225
assert mapping["ALT"].get("frame_id_767") == 3
@@ -231,7 +231,7 @@ def test_vec_767_hex_fid_parsed(tmp_path: Path):
231231
def test_vec_767_no_unsupported_kwargs(tmp_path: Path):
232232
text = "ALT W0B0-7 TYPE=BNR SIGNED=true SCALE=2 OFFSET=3 1.0"
233233
p = tmp_path / "test.vec"
234-
p.write_text(text)
234+
p.write_text(text, encoding="utf-8")
235235

236236
mapping = parse_vec_file_767(p)
237237
params = vec_to_parameters_767(mapping)
@@ -250,7 +250,7 @@ def test_vec_767_no_unsupported_kwargs(tmp_path: Path):
250250
def test_vec_767_char_token(tmp_path: Path):
251251
text = "STR W2B0-7 CHAR 1.0"
252252
p = tmp_path / "test.vec"
253-
p.write_text(text)
253+
p.write_text(text, encoding="utf-8")
254254

255255
mapping = parse_vec_file_767(p)
256256
assert mapping["STR"]["type"] == "CHAR"
@@ -262,7 +262,7 @@ def test_vec_767_char_token(tmp_path: Path):
262262
def test_vec_767_bcd_token(tmp_path: Path):
263263
text = "NUM W3B0-11 BCD 1.0"
264264
p = tmp_path / "test.vec"
265-
p.write_text(text)
265+
p.write_text(text, encoding="utf-8")
266266

267267
mapping = parse_vec_file_767(p)
268268
assert mapping["NUM"]["type"] == "BCD"
@@ -274,7 +274,7 @@ def test_vec_767_bcd_token(tmp_path: Path):
274274
def test_vec_767_cob_formula_with_scale_offset(tmp_path: Path):
275275
text = "MACH W0B0-15 COB=raw*scale+offset SCALE=2 OFFSET=3 1.0"
276276
p = tmp_path / "test.vec"
277-
p.write_text(text)
277+
p.write_text(text, encoding="utf-8")
278278

279279
mapping = parse_vec_file_767(p)
280280
params = vec_to_parameters_767(mapping)
@@ -288,7 +288,7 @@ def test_vec_767_cob_formula_with_scale_offset(tmp_path: Path):
288288
def test_vec_767_invalid_hex_fid(tmp_path: Path):
289289
text = "ALT W0B0-7 FID=0xINVALID 1.0"
290290
p = tmp_path / "test.vec"
291-
p.write_text(text)
291+
p.write_text(text, encoding="utf-8")
292292

293293
mapping = parse_vec_file_767(p)
294294
# Should safely default or ignore instead of raising ValueError

0 commit comments

Comments
 (0)