1
- from typing import Dict
2
-
3
1
import canmatrix
4
2
5
3
from os import PathLike
4
+ from typing import Union , BinaryIO
6
5
7
6
from can_decoder .Frame import Frame
8
7
from can_decoder .Signal import Signal
9
8
from can_decoder .SignalDB import SignalDB
10
9
11
10
12
- def load_dbc (dbc_file : PathLike , * args , ** kwargs ):
11
+ def load_dbc (dbc_file : Union [ str , PathLike , BinaryIO ] , * args , ** kwargs ):
13
12
"""From a DBC file, load a set of frames and signals for conversion.
14
13
15
14
:param dbc_file: Path to a DBC file.
@@ -22,9 +21,12 @@ def load_dbc(dbc_file: PathLike, *args, **kwargs):
22
21
# If a custom attribute is requested, determine the name here.
23
22
use_custom_attribute = kwargs .get ("use_custom_attribute" , None )
24
23
25
- # Load the file using canmatrix.
26
- with open (dbc_file , "rb" ) as handle :
27
- dbc = canmatrix .formats .load_flat (handle , "dbc" )
24
+ # Attempt to determine the type of the input. Check for file-like first, attempt to use as a path second.
25
+ if all (hasattr (dbc_file , attr ) for attr in ("seek" , "read" , "readline" )):
26
+ dbc = canmatrix .formats .load_flat (dbc_file , "dbc" )
27
+ else :
28
+ with open (dbc_file , "rb" ) as handle :
29
+ dbc = canmatrix .formats .load_flat (handle , "dbc" )
28
30
29
31
# Create a new DB instance.
30
32
result = SignalDB (protocol = dbc .attribute ("ProtocolType" , None ))
@@ -89,7 +91,7 @@ def load_dbc(dbc_file: PathLike, *args, **kwargs):
89
91
break
90
92
91
93
if multiplexer_signal is None :
92
- raise RuntimeError ("Could not find multiplexor " )
94
+ raise RuntimeError ("Could not find multiplexer " )
93
95
94
96
# Find all depending signals.
95
97
multiplexer_signal .signals = multiplexed [multiplexer_name ]
0 commit comments