Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions can/interfaces/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"""

import sys
from typing import Dict, Tuple
from typing import cast, Dict, Tuple

# interface_name => (module, classname)
BACKENDS: Dict[str, Tuple[str, ...]] = {
BACKENDS: Dict[str, Tuple[str, str]] = {
"kvaser": ("can.interfaces.kvaser", "KvaserBus"),
"socketcan": ("can.interfaces.socketcan", "SocketcanBus"),
"serial": ("can.interfaces.serial.serial_can", "SerialBus"),
Expand Down Expand Up @@ -35,18 +35,31 @@
if sys.version_info >= (3, 8):
from importlib.metadata import entry_points

entries = entry_points().get("can.interface", ())
BACKENDS.update(
{interface.name: tuple(interface.value.split(":")) for interface in entries}
)
# See https://docs.python.org/3/library/importlib.metadata.html#entry-points, "Compatibility Note".
if sys.version_info >= (3, 10):
BACKENDS.update(
{
interface.name: (interface.module, interface.attr)
for interface in entry_points(group="can.interface")
}
)
else:
# The entry_points().get(...) causes a deprecation warning on Python >= 3.10.
BACKENDS.update(
{
interface.name: cast(
Tuple[str, str], tuple(interface.value.split(":", maxsplit=1))
)
for interface in entry_points().get("can.interface", [])
}
)
else:
from pkg_resources import iter_entry_points

entries = iter_entry_points("can.interface")
BACKENDS.update(
{
interface.name: (interface.module_name, interface.attrs[0])
for interface in entries
for interface in iter_entry_points("can.interface")
}
)

Expand Down