Skip to content

Commit 492c539

Browse files
cowo78hardbyte
authored andcommitted
Moved implementations description into a dict and use importlib
1 parent a12d173 commit 492c539

1 file changed

Lines changed: 34 additions & 34 deletions

File tree

can/interface.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
import can
2+
import importlib
3+
24
from can.broadcastmanager import CyclicSendTaskABC, MultiRateCyclicSendTaskABC
35
from can.util import load_config
46

7+
# interface_name => (module, classname)
8+
BACKENDS = {
9+
'kvaser': ('can.interfaces.kvaser', 'KvaserBus'),
10+
'socketcan_ctypes': ('can.interfaces.socketcan', 'SocketcanCtypes_Bus'),
11+
'socketcan_native': ('can.interfaces.socketcan', 'SocketcanNative_Bus'),
12+
'serial': ('can.interfaces.serial.serial_can', 'SerialBus'),
13+
'pcan': ('can.interfaces.pcan', 'PcanBus'),
14+
'usb2can': ('can.interfaces.usb2can', 'Usb2canBus'),
15+
'ixxat': ('can.interfaces.ixxat', 'IXXATBus'),
16+
'nican': ('can.interfaces.nican', 'NicanBus'),
17+
'remote': ('can.interfaces.remote', 'RemoteBus'),
18+
'virtual': ('can.interfaces.virtual', 'VirtualBus'),
19+
'neovi': ('can.interfaces.neovi_api', 'NeoVIBus')
20+
}
521

622
class Bus(object):
723
"""
@@ -36,42 +52,26 @@ def __new__(cls, other, channel=None, *args, **kwargs):
3652
channel = config['channel']
3753

3854
# Import the correct Bus backend
39-
if interface == 'kvaser':
40-
from can.interfaces.kvaser import KvaserBus
41-
cls = KvaserBus
42-
elif interface == 'socketcan_ctypes':
43-
from can.interfaces.socketcan import SocketcanCtypes_Bus
44-
cls = SocketcanCtypes_Bus
45-
elif interface == 'socketcan_native':
46-
from can.interfaces.socketcan import SocketcanNative_Bus
47-
cls = SocketcanNative_Bus
48-
elif interface == 'serial':
49-
from can.interfaces.serial.serial_can import SerialBus
50-
cls = SerialBus
51-
elif interface == 'pcan':
52-
from can.interfaces.pcan import PcanBus
53-
cls = PcanBus
54-
elif interface == 'usb2can':
55-
from can.interfaces.usb2can import Usb2canBus
56-
cls = Usb2canBus
57-
elif interface == 'ixxat':
58-
from can.interfaces.ixxat import IXXATBus
59-
cls = IXXATBus
60-
elif interface == 'nican':
61-
from can.interfaces.nican import NicanBus
62-
cls = NicanBus
63-
elif interface == 'remote':
64-
from can.interfaces.remote import RemoteBus
65-
cls = RemoteBus
66-
elif interface == 'virtual':
67-
from can.interfaces.virtual import VirtualBus
68-
cls = VirtualBus
69-
elif interface == 'neovi':
70-
from can.interfaces.neovi_api import NeoVIBus
71-
cls = NeoVIBus
72-
else:
55+
try:
56+
(module_name, class_name) = BACKENDS[interface]
57+
except KeyError:
7358
raise NotImplementedError("CAN interface '{}' not supported".format(interface))
7459

60+
try:
61+
module = importlib.import_module(module_name)
62+
except Exception as e:
63+
raise ImportError(
64+
"Cannot import module {} for CAN interface '{}': {}".format(module_name, interface, e)
65+
)
66+
try:
67+
cls = getattr(module, class_name)
68+
except Exception as e:
69+
raise ImportError(
70+
"Cannot import class {} from module {} for CAN interface '{}': {}".format(
71+
class_name, module_name, interface, e
72+
)
73+
)
74+
7575
return cls(channel, **kwargs)
7676

7777

0 commit comments

Comments
 (0)