|
2 | 2 | Interfaces contain low level implementations that interact with CAN hardware.
|
3 | 3 | """
|
4 | 4 |
|
| 5 | +import sys |
| 6 | +from typing import Dict, Tuple |
| 7 | + |
5 | 8 | # interface_name => (module, classname)
|
6 |
| -BACKENDS = { |
| 9 | +BACKENDS: Dict[str, Tuple[str, ...]] = { |
7 | 10 | "kvaser": ("can.interfaces.kvaser", "KvaserBus"),
|
8 | 11 | "socketcan": ("can.interfaces.socketcan", "SocketcanBus"),
|
9 | 12 | "serial": ("can.interfaces.serial.serial_can", "SerialBus"),
|
|
29 | 32 | "socketcand": ("can.interfaces.socketcand", "SocketCanDaemonBus"),
|
30 | 33 | }
|
31 | 34 |
|
32 |
| -try: |
| 35 | +if sys.version_info >= (3, 8): |
33 | 36 | from importlib.metadata import entry_points
|
34 | 37 |
|
35 |
| - try: |
36 |
| - entries = entry_points(group="can.interface") |
37 |
| - except TypeError: |
38 |
| - # Fallback for Python <3.10 |
39 |
| - # See https://docs.python.org/3/library/importlib.metadata.html#entry-points, "Compatibility Note" |
40 |
| - entries = entry_points().get("can.interface", []) |
41 |
| - |
| 38 | + entries = entry_points().get("can.interface", ()) |
42 | 39 | BACKENDS.update(
|
43 | 40 | {interface.name: tuple(interface.value.split(":")) for interface in entries}
|
44 | 41 | )
|
45 |
| -except ImportError: |
| 42 | +else: |
46 | 43 | from pkg_resources import iter_entry_points
|
47 | 44 |
|
48 |
| - entry = iter_entry_points("can.interface") |
| 45 | + entries = iter_entry_points("can.interface") |
49 | 46 | BACKENDS.update(
|
50 | 47 | {
|
51 | 48 | interface.name: (interface.module_name, interface.attrs[0])
|
52 |
| - for interface in entry |
| 49 | + for interface in entries |
53 | 50 | }
|
54 | 51 | )
|
55 | 52 |
|
|
0 commit comments