1
1
# SPDX-License-Identifier: MIT
2
2
import re
3
3
from dataclasses import dataclass
4
- from typing import Any
4
+ from typing import Any , cast
5
5
from xml .etree import ElementTree
6
6
7
+ from bincopy import BinFile
8
+
7
9
from .dataformat import Dataformat
8
10
from .dataformatselection import DataformatSelection
9
11
from .element import IdentifiableElement
10
12
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
14
14
from .odxdoccontext import OdxDocContext
15
15
from .odxlink import OdxLinkDatabase , OdxLinkId
16
16
from .snrefcontext import SnRefContext
@@ -30,31 +30,33 @@ def data_str(self) -> str:
30
30
f"by the { type (self ).__name__ } class" )
31
31
32
32
@property
33
- def dataset (self ) -> IntelHexDataSet | MotorolaSDataSet | bytearray | None :
33
+ def dataset (self ) -> BinFile | bytearray | None :
34
34
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
39
43
elif self .dataformat .selection == DataformatSelection .BINARY :
40
44
return bytearray .fromhex (re .sub (r"\s" , "" , data_str , flags = re .MULTILINE ))
41
45
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 } " )
45
48
return None
46
49
47
50
@property
48
- def blob (self ) -> bytearray | None :
51
+ def blob (self ) -> bytearray :
49
52
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 ())
52
55
elif isinstance (ds , bytearray ):
53
56
return ds
54
57
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 ()
58
60
59
61
@staticmethod
60
62
def from_et (et_element : ElementTree .Element , context : OdxDocContext ) -> "Flashdata" :
0 commit comments