Skip to content

Commit ba53251

Browse files
author
Magnus Aagaard Sørensen
committed
Updated example
Updates to tests and stricter timezone requirements Alter the dbc load to support both file-like and path-like objects
1 parent 8b2827f commit ba53251

36 files changed

+3924
-63
lines changed

.coveragerc

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[run]
2+
source =
3+
can_decoder
4+
tests
5+
6+
[paths]
7+
source =
8+
src/can_decoder
9+
*/site-packages/can_decoder
10+

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*.mf4 filter=lfs diff=lfs merge=lfs -text
22
*.MF4 filter=lfs diff=lfs merge=lfs -text
33
*.dbc filter=lfs diff=lfs merge=lfs -text
4+
can_decoder/_version.py export-subst

.gitignore

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
__pycache__
2-
.idea
3-
build
4-
can_decoder.egg-info
5-
dist
1+
/__pycache__/
2+
/.idea/
3+
/.pytest_cache/
4+
/.tox/
5+
/build/
6+
/can_decoder.egg-info/
7+
/dist/
8+
/htmlcov/
69

10+
/.coverage

MANIFEST.in

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include versioneer.py
2+
include can_decoder/_version.py

can_decoder/DBCLoader.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
from typing import Dict
2-
31
import canmatrix
42

53
from os import PathLike
4+
from typing import Union, BinaryIO
65

76
from can_decoder.Frame import Frame
87
from can_decoder.Signal import Signal
98
from can_decoder.SignalDB import SignalDB
109

1110

12-
def load_dbc(dbc_file: PathLike, *args, **kwargs):
11+
def load_dbc(dbc_file: Union[str, PathLike, BinaryIO], *args, **kwargs):
1312
"""From a DBC file, load a set of frames and signals for conversion.
1413
1514
:param dbc_file: Path to a DBC file.
@@ -22,9 +21,12 @@ def load_dbc(dbc_file: PathLike, *args, **kwargs):
2221
# If a custom attribute is requested, determine the name here.
2322
use_custom_attribute = kwargs.get("use_custom_attribute", None)
2423

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")
2830

2931
# Create a new DB instance.
3032
result = SignalDB(protocol=dbc.attribute("ProtocolType", None))
@@ -89,7 +91,7 @@ def load_dbc(dbc_file: PathLike, *args, **kwargs):
8991
break
9092

9193
if multiplexer_signal is None:
92-
raise RuntimeError("Could not find multiplexor")
94+
raise RuntimeError("Could not find multiplexer")
9395

9496
# Find all depending signals.
9597
multiplexer_signal.signals = multiplexed[multiplexer_name]

can_decoder/DecoderBase.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import numpy as np
55

6+
from can_decoder.exceptions.DataSizeMismatch import DataSizeMismatch
67
from can_decoder.Signal import Signal
78
from can_decoder.SignalDB import SignalDB
89

@@ -21,7 +22,7 @@ def get_supported_protocols(cls) -> List[Optional[str]]:
2122
2223
:return: List of supported protocols.
2324
"""
24-
raise NotImplementedError("")
25+
raise NotImplementedError("") # pragma: no cover
2526

2627
@classmethod
2728
def _extract_signal_bits(cls, signal: Signal, data: np.ndarray) -> np.ndarray:
@@ -84,7 +85,7 @@ def _decode_signal_raw(cls, signal: Signal, data: np.ndarray) -> np.ndarray:
8485
# Construct the corresponding unsigned datatype.
8586
signal_single_datatype = "<u{}".format(signal_size_in_bytes)
8687

87-
if len(signal_data.shape) == 0:
88+
if len(signal_data.shape) == 1:
8889
# Frames with one dimension are simple to handle.
8990
new_shape = signal_data.shape[0]
9091
elif signal_size_in_bytes in (3, 5, 6, 7):
@@ -105,10 +106,13 @@ def _decode_signal_raw(cls, signal: Signal, data: np.ndarray) -> np.ndarray:
105106
signal_data = expanded_data
106107
new_shape = next_size * data.shape[0]
107108
else:
108-
new_shape = signal_size_in_bytes * data.shape[0]
109+
new_shape = (data.shape[0], signal_size_in_bytes)
109110

110111
# Reshape to a single dimension.
111-
reshaped_data = signal_data.reshape(new_shape)
112+
try:
113+
reshaped_data = signal_data.reshape(new_shape)
114+
except ValueError as e:
115+
raise DataSizeMismatch()
112116

113117
# Interpret as single datatype.
114118
data_return = reshaped_data.view(dtype=signal_single_datatype)

can_decoder/SignalDB.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
from collections import defaultdict
21
from typing import Optional
32

43
from can_decoder.Frame import Frame
54

65

76
class SignalDB(object):
8-
_protocol = None # type: Optional[str]
9-
frames = defaultdict()
10-
117
def __init__(self, protocol: Optional[str] = None):
128
"""Create a new signal database, with a pre-defined protocol.
139
1410
:param protocol: Protocol to associate with the database.
1511
"""
1612
self._protocol = protocol
13+
self.frames = {}
1714
pass
1815

1916
@property

can_decoder/__init__.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
1-
from can_decoder.dataframe import DataFrameDecoder
2-
from can_decoder.iterator import IteratorDecoder, decoded_signal
1+
from can_decoder.exceptions import *
2+
from can_decoder.iterator import IteratorDecoder, DecodedSignal
33

44
from can_decoder.Frame import Frame
55
from can_decoder.Signal import Signal
66
from can_decoder.SignalDB import SignalDB
77

88
try:
9-
from can_decoder.DBCLoader import load_dbc
9+
from can_decoder.dataframe import DataFrameDecoder
1010
except ModuleNotFoundError:
1111
pass
12+
13+
try:
14+
import warnings
15+
16+
with warnings.catch_warnings():
17+
warnings.filterwarnings("ignore", category=DeprecationWarning)
18+
warnings.filterwarnings("ignore", category=SyntaxWarning)
19+
20+
from can_decoder.DBCLoader import load_dbc
21+
except ModuleNotFoundError:
22+
pass
23+
24+
from ._version import get_versions
25+
__version__ = get_versions()["version"]
26+
del get_versions

0 commit comments

Comments
 (0)