Skip to content

Commit aa5e9ec

Browse files
committed
Use the 'bincopy' python python module to handle MOTOROLA-S and INTEL-HEX data records and flash data
thanks to [at]kayoub5 for bringing this up. Signed-off-by: Andreas Lauser <andreas.lauser@mercedes-benz.com>
1 parent b5e6635 commit aa5e9ec

File tree

8 files changed

+42
-437
lines changed

8 files changed

+42
-437
lines changed

odxtools/datarecord.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
# SPDX-License-Identifier: MIT
22
import re
33
from dataclasses import dataclass, field
4-
from functools import cached_property
54
from typing import Any, cast
65
from xml.etree import ElementTree
76

7+
from bincopy import BinFile
8+
89
from .datafile import Datafile
910
from .dataformatselection import DataformatSelection
1011
from .element import NamedElement
1112
from .exceptions import odxraise, odxrequire
1213
from .identvalue import IdentValue
13-
from .intelhexdataset import IntelHexDataSet
14-
from .motorolasdataset import MotorolaSDataSet
1514
from .odxdoccontext import OdxDocContext
1615
from .odxlink import OdxLinkDatabase, OdxLinkId
1716
from .snrefcontext import SnRefContext
@@ -32,9 +31,9 @@ class DataRecord(NamedElement):
3231

3332
dataformat: DataformatSelection
3433

35-
@cached_property
36-
def dataset(self) -> IntelHexDataSet | MotorolaSDataSet | bytearray:
37-
if self.data is None:
34+
@property
35+
def dataset(self) -> BinFile | bytearray:
36+
if self.datafile is not None:
3837
db = odxrequire(self._database)
3938
if db is None:
4039
return bytearray()
@@ -55,10 +54,13 @@ def dataset(self) -> IntelHexDataSet | MotorolaSDataSet | bytearray:
5554
odxraise("No data specified for DATA-RECORD")
5655
return bytearray()
5756

58-
if self.dataformat == DataformatSelection.INTEL_HEX:
59-
return IntelHexDataSet.from_string(data_str)
60-
elif self.dataformat == DataformatSelection.MOTOROLA_S:
61-
return MotorolaSDataSet.from_string(data_str)
57+
if self.dataformat in (DataformatSelection.INTEL_HEX, DataformatSelection.MOTOROLA_S):
58+
bf = BinFile()
59+
60+
# remove white space and empty lines
61+
bf.add("\n".join([re.sub(r"\s", "", x) for x in data_str.splitlines() if x.strip()]))
62+
63+
return bf
6264
elif self.dataformat == DataformatSelection.BINARY:
6365
return bytearray.fromhex(re.sub(r"\s", "", data_str, flags=re.MULTILINE))
6466

@@ -78,8 +80,8 @@ def blob(self) -> bytearray:
7880
run time when it is accessed.
7981
"""
8082

81-
if isinstance(self.dataset, (IntelHexDataSet, MotorolaSDataSet)):
82-
return self.dataset.blob
83+
if isinstance(self.dataset, BinFile):
84+
return cast(bytearray, self.dataset.as_binary())
8385

8486
return self.dataset
8587

odxtools/flashdata.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# SPDX-License-Identifier: MIT
22
import re
33
from dataclasses import dataclass
4-
from typing import Any
4+
from typing import Any, cast
55
from xml.etree import ElementTree
66

7+
from bincopy import BinFile
8+
79
from .dataformat import Dataformat
810
from .dataformatselection import DataformatSelection
911
from .element import IdentifiableElement
1012
from .encryptcompressmethod import EncryptCompressMethod
11-
from .exceptions import odxassert, odxrequire
12-
from .intelhexdataset import IntelHexDataSet
13-
from .motorolasdataset import MotorolaSDataSet
13+
from .exceptions import odxassert, odxraise, odxrequire
1414
from .odxdoccontext import OdxDocContext
1515
from .odxlink import OdxLinkDatabase, OdxLinkId
1616
from .snrefcontext import SnRefContext
@@ -30,31 +30,33 @@ def data_str(self) -> str:
3030
f"by the {type(self).__name__} class")
3131

3232
@property
33-
def dataset(self) -> IntelHexDataSet | MotorolaSDataSet | bytearray | None:
33+
def dataset(self) -> BinFile | bytearray | None:
3434
data_str = self.data_str
35-
if self.dataformat.selection == DataformatSelection.INTEL_HEX:
36-
return IntelHexDataSet.from_string(data_str)
37-
elif self.dataformat.selection == DataformatSelection.MOTOROLA_S:
38-
return MotorolaSDataSet.from_string(data_str)
35+
if self.dataformat.selection in (DataformatSelection.INTEL_HEX,
36+
DataformatSelection.MOTOROLA_S):
37+
bf = BinFile()
38+
39+
# remove white space and empty lines
40+
bf.add("\n".join([re.sub(r"\s", "", x) for x in data_str.splitlines() if x.strip()]))
41+
42+
return bf
3943
elif self.dataformat.selection == DataformatSelection.BINARY:
4044
return bytearray.fromhex(re.sub(r"\s", "", data_str, flags=re.MULTILINE))
4145
else:
42-
odxassert(self.dataformat.selection == DataformatSelection.USER_DEFINED)
43-
# user defined formats cannot be parsed on the odxtools
44-
# level
46+
odxassert(self.dataformat.selection == DataformatSelection.USER_DEFINED,
47+
f"Unsupported data format {self.dataformat.selection}")
4548
return None
4649

4750
@property
48-
def blob(self) -> bytearray | None:
51+
def blob(self) -> bytearray:
4952
ds = self.dataset
50-
if isinstance(ds, (IntelHexDataSet, MotorolaSDataSet)):
51-
return ds.blob
53+
if isinstance(ds, BinFile):
54+
return cast(bytearray, ds.as_binary())
5255
elif isinstance(ds, bytearray):
5356
return ds
5457

55-
# USER-DEFINED flash data cannot be interpreted on the
56-
# odxtools level
57-
return ds
58+
odxraise("USER-DEFINED flash data cannot be interpreted on the odxtools level")
59+
return bytearray()
5860

5961
@staticmethod
6062
def from_et(et_element: ElementTree.Element, context: OdxDocContext) -> "Flashdata":

odxtools/intelhexdatasegment.py

Lines changed: 0 additions & 94 deletions
This file was deleted.

odxtools/intelhexdataset.py

Lines changed: 0 additions & 73 deletions
This file was deleted.

0 commit comments

Comments
 (0)