18
18
log_autodetect = log .getChild ("detect_available_configs" )
19
19
20
20
21
- def _get_class_for_interface (interface : str ) -> Type [BusABC ]:
21
+ def _get_class_for_interface (interface : str , installed_interfaces : dict = None ) -> Type [BusABC ]:
22
22
"""
23
23
Returns the main bus class for the given interface.
24
24
@@ -28,8 +28,10 @@ def _get_class_for_interface(interface: str) -> Type[BusABC]:
28
28
if there was a problem while importing the interface or the bus class within that
29
29
"""
30
30
# Find the correct backend
31
+ if not isinstance (installed_interfaces , dict ):
32
+ installed_interfaces = BACKENDS
31
33
try :
32
- module_name , class_name = BACKENDS [interface ]
34
+ module_name , class_name = installed_interfaces [interface ]
33
35
except KeyError :
34
36
raise NotImplementedError (
35
37
"CAN interface '{}' not supported" .format (interface )
@@ -149,6 +151,13 @@ def detect_available_configs(
149
151
150
152
# Figure out where to search
151
153
if interfaces is None :
154
+ try :
155
+ from importlib .metadata import entry_points # If this works, the interfaces have probably been loaded
156
+ except ImportError :
157
+ from pkg_resources import iter_entry_points as entry_points
158
+ entry = entry_points ("can.interface" )
159
+ BACKENDS .update ({interface .name : (interface .module_name , interface .attrs [0 ])
160
+ for interface in entry })
152
161
interfaces = BACKENDS
153
162
elif isinstance (interfaces , str ):
154
163
interfaces = (interfaces ,)
@@ -158,7 +167,7 @@ def detect_available_configs(
158
167
for interface in interfaces :
159
168
160
169
try :
161
- bus_class = _get_class_for_interface (interface )
170
+ bus_class = _get_class_for_interface (interface , installed_interfaces = BACKENDS )
162
171
except CanInterfaceNotImplementedError :
163
172
log_autodetect .debug (
164
173
'interface "%s" cannot be loaded for detection of available configurations' ,
0 commit comments