11import can
22from can .broadcastmanager import CyclicSendTaskABC , MultiRateCyclicSendTaskABC
3- from can .util import load_config , choose_socketcan_implementation
4-
5- VALID_INTERFACES = set (['kvaser' , 'serial' , 'pcan' , 'socketcan_native' ,
6- 'socketcan_ctypes' , 'socketcan' , 'usb2can' , 'ixxat' ,
7- 'nican' , 'remote' , 'virtual' , 'neovi' ])
3+ from can .util import load_config
84
95
106class Bus (object ):
@@ -21,30 +17,25 @@ def __new__(cls, other, channel=None, *args, **kwargs):
2117 :param kwargs:
2218 Should contain a bustype key with a valid interface name.
2319
24- :raises: NotImplementedError if the bustype isn't recognized
20+ :raises:
21+ NotImplementedError if the bustype isn't recognized
22+ :raises:
23+ ValueError if the bustype or channel isn't either passed as an argument
24+ or set in the can.rc config.
25+
2526 """
27+ config = load_config (config = {
28+ 'interface' : kwargs .get ('bustype' ),
29+ 'channel' : channel
30+ })
31+
2632 if 'bustype' in kwargs :
27- can . rc [ 'interface' ] = kwargs [ ' bustype' ]
33+ # remove the bustype so it doesn't get passed to the backend
2834 del kwargs ['bustype' ]
29-
30- if can .rc ['interface' ] == 'socketcan' :
31- can .rc ['interface' ] = choose_socketcan_implementation ()
32-
33- # Update can.rc from kwargs
34- for kw in ('interface' , 'bitrate' ):
35- if kw in kwargs :
36- can .rc [kw ] = kwargs [kw ]
37-
38- if 'interface' not in can .rc or 'channel' not in can .rc or can .rc ['interface' ] is None :
39- can .log .debug ("Loading default configuration" )
40- # Load defaults
41- can .rc = load_config ()
42-
43- if can .rc ['interface' ] not in VALID_INTERFACES :
44- raise NotImplementedError ('Invalid CAN Bus Type - {}' .format (can .rc ['interface' ]))
35+ interface = config ['interface' ]
36+ channel = config ['channel' ]
4537
4638 # Import the correct Bus backend
47- interface = can .rc ['interface' ]
4839 if interface == 'kvaser' :
4940 from can .interfaces .kvaser import KvaserBus
5041 cls = KvaserBus
@@ -81,8 +72,6 @@ def __new__(cls, other, channel=None, *args, **kwargs):
8172 else :
8273 raise NotImplementedError ("CAN interface '{}' not supported" .format (interface ))
8374
84- if channel is None :
85- channel = can .rc ['channel' ]
8675 return cls (channel , ** kwargs )
8776
8877
@@ -91,20 +80,13 @@ class CyclicSendTask(CyclicSendTaskABC):
9180 @classmethod
9281 def __new__ (cls , other , channel , * args , ** kwargs ):
9382
94- # If can.rc doesn't look valid: load default
95- if 'interface' not in can .rc or 'channel' not in can .rc :
96- can .log .debug ("Loading default configuration" )
97- can .rc = load_config ()
98-
99- print (can .rc )
100- if can .rc ['interface' ] not in VALID_INTERFACES :
101- raise NotImplementedError ('Invalid CAN Bus Type - {}' .format (can .rc ['interface' ]))
83+ config = load_config (config = {'channel' : channel })
10284
10385 # Import the correct implementation of CyclicSendTask
104- if can . rc ['interface' ] == 'socketcan_ctypes' :
86+ if config ['interface' ] == 'socketcan_ctypes' :
10587 from can .interfaces .socketcan .socketcan_ctypes import CyclicSendTask as _ctypesCyclicSendTask
10688 cls = _ctypesCyclicSendTask
107- elif can . rc ['interface' ] == 'socketcan_native' :
89+ elif config ['interface' ] == 'socketcan_native' :
10890 from can .interfaces .socketcan .socketcan_native import CyclicSendTask as _nativeCyclicSendTask
10991 cls = _nativeCyclicSendTask
11092 # CyclicSendTask has not been fully implemented on remote interface yet.
@@ -114,33 +96,26 @@ def __new__(cls, other, channel, *args, **kwargs):
11496 # from can.interfaces.remote import CyclicSendTask as _remoteCyclicSendTask
11597 # cls = _remoteCyclicSendTask
11698 else :
117- can .log . info ("Current CAN interface doesn't support CyclicSendTask" )
99+ raise can .CanError ("Current CAN interface doesn't support CyclicSendTask" )
118100
119- return cls (channel , * args , ** kwargs )
101+ return cls (config [ ' channel' ] , * args , ** kwargs )
120102
121103
122104class MultiRateCyclicSendTask (MultiRateCyclicSendTaskABC ):
123105
124106 @classmethod
125107 def __new__ (cls , other , channel , * args , ** kwargs ):
126108
127- # If can.rc doesn't look valid: load default
128- if 'interface' not in can .rc or 'channel' not in can .rc :
129- can .log .debug ("Loading default configuration" )
130- can .rc = load_config ()
131-
132- print (can .rc )
133- if can .rc ['interface' ] not in VALID_INTERFACES :
134- raise NotImplementedError ('Invalid CAN Bus Type - {}' .format (can .rc ['interface' ]))
109+ config = load_config (config = {'channel' : channel })
135110
136111 # Import the correct implementation of CyclicSendTask
137- if can . rc ['interface' ] == 'socketcan_ctypes' :
112+ if config ['interface' ] == 'socketcan_ctypes' :
138113 from can .interfaces .socketcan .socketcan_ctypes import MultiRateCyclicSendTask as _ctypesMultiRateCyclicSendTask
139114 cls = _ctypesMultiRateCyclicSendTask
140- elif can . rc ['interface' ] == 'socketcan_native' :
115+ elif config ['interface' ] == 'socketcan_native' :
141116 from can .interfaces .socketcan .socketcan_native import MultiRateCyclicSendTask as _nativeMultiRateCyclicSendTask
142117 cls = _nativeMultiRateCyclicSendTask
143118 else :
144119 can .log .info ("Current CAN interface doesn't support CyclicSendTask" )
145120
146- return cls (channel , * args , ** kwargs )
121+ return cls (config [ ' channel' ] , * args , ** kwargs )
0 commit comments