Skip to content

Commit c2ef964

Browse files
committed
Add CANsub interface (provided by python-can-cansub)
1 parent a594781 commit c2ef964

8 files changed

Lines changed: 71 additions & 0 deletions

File tree

‎can/interfaces/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"BACKENDS",
99
"VALID_INTERFACES",
1010
"canalystii",
11+
"cansub",
1112
"cantact",
1213
"etas",
1314
"gs_usb",
@@ -57,6 +58,7 @@
5758
"neousys": ("can.interfaces.neousys", "NeousysBus"),
5859
"etas": ("can.interfaces.etas", "EtasBus"),
5960
"socketcand": ("can.interfaces.socketcand", "SocketCanDaemonBus"),
61+
"cansub": ("can.interfaces.cansub", "CanSub"),
6062
}
6163

6264

‎can/interfaces/cansub.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
Interface for the CSS Electronics CANsub CAN-USB/Ethernet interfaces.
3+
4+
The bus implementation is provided by the ``python-can-cansub`` package, which is
5+
maintained by CSS Electronics and released independently of python-can to follow
6+
CANsub firmware API updates.
7+
"""
8+
9+
__all__ = ["CanSub"]
10+
11+
try:
12+
from python_can_cansub import CanSub
13+
except ModuleNotFoundError as exc:
14+
if exc.name != "python_can_cansub":
15+
raise
16+
raise ImportError(
17+
"The CANsub interface requires the python-can-cansub package. "
18+
"Install it with: pip install python-can-cansub"
19+
) from exc

‎doc/changelog.d/+cansub.added.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added the CSS Electronics CANsub interface (``interface="cansub"``), provided by the `python-can-cansub <https://pypi.org/project/python-can-cansub/>`__ package (install with ``pip install python-can[cansub]``).

‎doc/configuration.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ Lookup table of interface names:
114114
+=====================+=====================================+
115115
| ``"canalystii"`` | :doc:`interfaces/canalystii` |
116116
+---------------------+-------------------------------------+
117+
| ``"cansub"`` | :doc:`interfaces/cansub` |
118+
+---------------------+-------------------------------------+
117119
| ``"cantact"`` | :doc:`interfaces/cantact` |
118120
+---------------------+-------------------------------------+
119121
| ``"etas"`` | :doc:`interfaces/etas` |

‎doc/interfaces.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ The following hardware interfaces are included in python-can:
1818
:maxdepth: 1
1919

2020
interfaces/canalystii
21+
interfaces/cansub
2122
interfaces/cantact
2223
interfaces/etas
2324
interfaces/gs_usb

‎doc/interfaces/cansub.rst‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
CANsub CAN interface (CSS Electronics)
2+
=======================================
3+
4+
The `CANsub.2 <https://www.csselectronics.com/products/can-fd-usb-interface-ethernet-cansub-2>`__/`CANsub.4 <https://www.csselectronics.com/products/can-bus-interface-usb-ethernet-cansub-4>`__
5+
by `CSS Electronics <https://www.csselectronics.com/>`__ are 2-4 x CAN-USB/Ethernet interfaces.
6+
7+
The bus implementation is provided by the
8+
`python-can-cansub <https://pypi.org/project/python-can-cansub/>`__ package, which is
9+
maintained by CSS Electronics and released independently of python-can to follow CANsub
10+
firmware updates.
11+
12+
Install python-can with the ``cansub`` extra:
13+
14+
.. code-block:: bash
15+
16+
pip install python-can[cansub]
17+
18+
For usage, configuration and compatibility details, see the
19+
`python-can-cansub documentation <https://github.com/CSS-Electronics/python-can-cansub#readme>`__.

‎pyproject.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ seeedstudio = ["pyserial>=3.0"]
6262
serial = ["pyserial~=3.0"]
6363
neovi = ["filelock", "python-ics>=2.12"]
6464
canalystii = ["canalystii>=0.1.0"]
65+
cansub = ["python-can-cansub"]
6566
cantact = ["cantact>=0.0.7"]
6667
cvector = ["python-can-cvector"]
6768
gs-usb = ["gs-usb>=0.2.1", "pyusb>=1.0.2"]

‎test/test_cansub.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
Tests for the CANsub interface stub, which delegates to the python-can-cansub package.
3+
"""
4+
5+
import sys
6+
from unittest.mock import patch
7+
8+
import pytest
9+
10+
import can
11+
from can.exceptions import CanInterfaceNotImplementedError
12+
from can.interfaces import BACKENDS
13+
14+
15+
def test_missing_package_raises_install_hint():
16+
# Skip if python-can-cansub replaced the built-in entry via its own entry point
17+
if BACKENDS["cansub"] != ("can.interfaces.cansub", "CanSub"):
18+
pytest.skip("cansub backend overridden by an installed plugin")
19+
20+
with patch.dict(sys.modules, {"python_can_cansub": None}):
21+
sys.modules.pop("can.interfaces.cansub", None)
22+
with pytest.raises(
23+
CanInterfaceNotImplementedError, match="pip install python-can-cansub"
24+
):
25+
can.Bus(interface="cansub", channel="aabbccdd-usb.local@1")
26+
sys.modules.pop("can.interfaces.cansub", None)

0 commit comments

Comments
 (0)