8
8
import logging
9
9
from typing import Any , cast , Iterable , Type , Optional , Union , List
10
10
11
+ from . import util
11
12
from .bus import BusABC
12
- from .util import load_config , deprecated_args_alias
13
13
from .interfaces import BACKENDS
14
14
from .exceptions import CanInterfaceNotImplementedError
15
15
from .typechecking import AutoDetectedConfig , Channel
@@ -61,6 +61,13 @@ class Bus(BusABC): # pylint: disable=abstract-method
61
61
Instantiates a CAN Bus of the given ``interface``, falls back to reading a
62
62
configuration file from default locations.
63
63
64
+ .. note::
65
+ Please note that while the arguments provided to this class take precedence
66
+ over any existing values from configuration, it is possible that other parameters
67
+ from the configuration may be added to the bus instantiation.
68
+ This could potentially have unintended consequences. To prevent this,
69
+ you may use the *ignore_config* parameter to ignore any existing configurations.
70
+
64
71
:param channel:
65
72
Channel identification. Expected type is backend dependent.
66
73
Set to ``None`` to let it be resolved automatically from the default
@@ -71,8 +78,13 @@ class Bus(BusABC): # pylint: disable=abstract-method
71
78
Set to ``None`` to let it be resolved automatically from the default
72
79
:ref:`configuration`.
73
80
74
- :param args:
75
- ``interface`` specific positional arguments.
81
+ :param config_context:
82
+ Extra 'context', that is passed to config sources.
83
+ This can be used to select a section other than 'default' in the configuration file.
84
+
85
+ :param ignore_config:
86
+ If ``True``, only the given arguments will be used for the bus instantiation. Existing
87
+ configuration sources will be ignored.
76
88
77
89
:param kwargs:
78
90
``interface`` specific keyword arguments.
@@ -88,25 +100,28 @@ class Bus(BusABC): # pylint: disable=abstract-method
88
100
"""
89
101
90
102
@staticmethod
91
- @deprecated_args_alias (bustype = "interface" ) # Deprecated since python-can 4.2
92
- def __new__ ( # type: ignore # pylint: disable=keyword-arg-before-vararg
103
+ @util .deprecated_args_alias (
104
+ deprecation_start = "4.2.0" ,
105
+ deprecation_end = "5.0.0" ,
106
+ bustype = "interface" ,
107
+ context = "config_context" ,
108
+ )
109
+ def __new__ ( # type: ignore
93
110
cls : Any ,
94
111
channel : Optional [Channel ] = None ,
95
112
interface : Optional [str ] = None ,
96
- * args : Any ,
113
+ config_context : Optional [str ] = None ,
114
+ ignore_config : bool = False ,
97
115
** kwargs : Any ,
98
116
) -> BusABC :
99
117
# figure out the rest of the configuration; this might raise an error
100
118
if interface is not None :
101
119
kwargs ["interface" ] = interface
102
120
if channel is not None :
103
121
kwargs ["channel" ] = channel
104
- if "context" in kwargs :
105
- context = kwargs ["context" ]
106
- del kwargs ["context" ]
107
- else :
108
- context = None
109
- kwargs = load_config (config = kwargs , context = context )
122
+
123
+ if not ignore_config :
124
+ kwargs = util .load_config (config = kwargs , context = config_context )
110
125
111
126
# resolve the bus class to use for that interface
112
127
cls = _get_class_for_interface (kwargs ["interface" ])
@@ -115,17 +130,12 @@ def __new__( # type: ignore # pylint: disable=keyword-arg-before-vararg
115
130
del kwargs ["interface" ]
116
131
117
132
# make sure the bus can handle this config format
118
- if "channel" not in kwargs :
119
- raise ValueError ("'channel' argument missing" )
120
- else :
121
- channel = kwargs ["channel" ]
122
- del kwargs ["channel" ]
123
-
133
+ channel = kwargs .pop ("channel" , channel )
124
134
if channel is None :
125
135
# Use the default channel for the backend
126
- bus = cls (* args , * *kwargs )
136
+ bus = cls (** kwargs )
127
137
else :
128
- bus = cls (channel , * args , * *kwargs )
138
+ bus = cls (channel , ** kwargs )
129
139
130
140
return cast (BusABC , bus )
131
141
0 commit comments