|
1 | 1 | import can |
| 2 | +import importlib |
| 3 | + |
2 | 4 | from can.broadcastmanager import CyclicSendTaskABC, MultiRateCyclicSendTaskABC |
3 | 5 | from can.util import load_config |
4 | 6 |
|
| 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 | +} |
5 | 21 |
|
6 | 22 | class Bus(object): |
7 | 23 | """ |
@@ -36,42 +52,26 @@ def __new__(cls, other, channel=None, *args, **kwargs): |
36 | 52 | channel = config['channel'] |
37 | 53 |
|
38 | 54 | # 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: |
73 | 58 | raise NotImplementedError("CAN interface '{}' not supported".format(interface)) |
74 | 59 |
|
| 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 | + |
75 | 75 | return cls(channel, **kwargs) |
76 | 76 |
|
77 | 77 |
|
|
0 commit comments