Skip to content

Commit 9f7077c

Browse files
authored
Speed up imports by removing pkg_resources (hardbyte#1110)
* Add method to calculate load on an interface * Fix usage of isinstance for calc_load in bus.py * Include ID length in bus load calculation * Remove pkg_resources to speed up import time * Readd pkg_resources but default to importlib to speed up import time * Resolve handling of importlib entry_points for plugins * Fall back to ImportError only if importlib is missing * Remove pkg_resources to speed up import time * Readd pkg_resources but default to importlib to speed up import time * Resolve handling of importlib entry_points for plugins * Fall back to ImportError only if importlib is missing * Load user-installed interface modules only on demand if importlib is not installed * Revert "Add method to calculate load on an interface" This reverts commit 43c0c55 * Revert "Load user-installed interface modules only on demand if importlib is not installed" This reverts commit 2fed532 * Rename entry_points back to iter_entry_points
1 parent 2d79b51 commit 9f7077c

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

can/interfaces/__init__.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
Interfaces contain low level implementations that interact with CAN hardware.
33
"""
44

5-
import warnings
6-
from pkg_resources import iter_entry_points
7-
8-
95
# interface_name => (module, classname)
106
BACKENDS = {
117
"kvaser": ("can.interfaces.kvaser", "KvaserBus"),
@@ -31,11 +27,14 @@
3127
"neousys": ("can.interfaces.neousys", "NeousysBus"),
3228
}
3329

34-
BACKENDS.update(
35-
{
36-
interface.name: (interface.module_name, interface.attrs[0])
37-
for interface in iter_entry_points("can.interface")
38-
}
39-
)
30+
try:
31+
from importlib.metadata import entry_points
32+
entry = entry_points()
33+
if 'can.interface' in entry:
34+
BACKENDS.update({interface.name: tuple(interface.value.split(':')) for interface in entry['can.interface']})
35+
except ImportError:
36+
from pkg_resources import iter_entry_points
37+
entry = iter_entry_points("can.interface")
38+
BACKENDS.update({interface.name: (interface.module_name, interface.attrs[0]) for interface in entry})
4039

4140
VALID_INTERFACES = frozenset(list(BACKENDS.keys()))

0 commit comments

Comments
 (0)