1616)
1717
1818
19+ def _read_text_file (path : Path ) -> str :
20+ """Read text file with automatic encoding fallback."""
21+ for encoding in ("utf-8" , "latin-1" , "cp1252" ):
22+ try :
23+ return path .read_text (encoding = encoding ).strip ()
24+ except UnicodeDecodeError :
25+ continue
26+ raise UnicodeDecodeError (
27+ f"Failed to decode VEC file with standard encodings: { path } "
28+ )
29+
30+
1931def _parse_bitrange_767 (token : str ) -> dict [str , int ] | None :
2032 m = _VEC_BITRANGE_RE_767 .search (token )
2133 if not m :
@@ -35,15 +47,9 @@ def _parse_bitrange_767(token: str) -> dict[str, int] | None:
3547
3648
3749def parse_vec_file_767 (path : Path ) -> dict [str , Any ]:
38- """ARINC 767 VEC parser with improved hex FID and single-pass token parsing ."""
50+ """Advanced ARINC 767 VEC parser with robust encoding, inline comment stripping, and validation ."""
3951 try :
40- text = path .read_text (encoding = "utf-8" ).strip ()
41- except UnicodeDecodeError :
42- try :
43- text = path .read_text (encoding = "latin-1" ).strip ()
44- except Exception as e :
45- logger .error (f"Failed to read VEC file 767 { path } : { e } " )
46- raise
52+ text = _read_text_file (path )
4753 except Exception as e :
4854 logger .error (f"Failed to read VEC file 767 { path } : { e } " )
4955 raise
@@ -59,8 +65,9 @@ def parse_vec_file_767(path: Path) -> dict[str, Any]:
5965 out : dict [str , Any ] = {}
6066
6167 for line_num , line in enumerate (text .splitlines (), start = 1 ):
62- line = line .strip ()
63- if not line or line .startswith ("#" ):
68+ # Strip inline comments and whitespace
69+ line = line .split ("#" )[0 ].strip ()
70+ if not line :
6471 continue
6572
6673 parts = line .split ()
@@ -81,6 +88,9 @@ def parse_vec_file_767(path: Path) -> dict[str, Any]:
8188 entry .update (br )
8289 break
8390
91+ if "word" not in entry :
92+ logger .debug (f"Line { line_num } : No bitrange found for parameter '{ name } '" )
93+
8494 # Rate extraction fallback (ignoring key-value parameters)
8595 for tok in reversed (parts ):
8696 if "=" in tok :
@@ -91,10 +101,10 @@ def parse_vec_file_767(path: Path) -> dict[str, Any]:
91101 except ValueError :
92102 continue
93103
94- # Single-pass token processing
104+ # Single-pass token processing for metadata flags
95105 for tok in parts [1 :]:
96106 upper_tok = tok .upper ()
97- if upper_tok in ("BNR" , "BCD" , "CHAR" ):
107+ if upper_tok in ("BNR" , "BCD" , "CHAR" , "DISCRETE" ):
98108 entry ["type" ] = upper_tok
99109 continue
100110
@@ -111,7 +121,9 @@ def parse_vec_file_767(path: Path) -> dict[str, Any]:
111121 base = 16 if val .lower ().startswith ("0x" ) else 10
112122 entry ["frame_id_767" ] = int (val , base )
113123 except ValueError :
114- pass
124+ logger .warning (
125+ f"Line { line_num } : Invalid FID value '{ val } ' for parameter '{ name } '"
126+ )
115127 elif key == "COB" :
116128 entry ["cob_formula" ] = val
117129 elif key == "TYPE" :
0 commit comments