@@ -50,12 +50,12 @@ def __init__(self) -> None:
50
50
def register (self , bus : BusABC , notifier : "Notifier" ) -> None :
51
51
"""Register a bus and its associated notifier.
52
52
53
- Ensures that a bus is not added to multiple active Notifier instances.
53
+ Ensures that a bus is not added to multiple active :class:`~can. Notifier` instances.
54
54
55
55
:param bus:
56
56
The CAN bus to register.
57
57
:param notifier:
58
- The Notifier instance associated with the bus.
58
+ The :class:`~can. Notifier` instance associated with the bus.
59
59
:raises ValueError:
60
60
If the bus is already assigned to an active Notifier.
61
61
"""
@@ -75,7 +75,7 @@ def unregister(self, bus: BusABC, notifier: "Notifier") -> None:
75
75
:param bus:
76
76
The CAN bus to unregister.
77
77
:param notifier:
78
- The Notifier instance associated with the bus.
78
+ The :class:`~can. Notifier` instance associated with the bus.
79
79
"""
80
80
with self .lock :
81
81
registered_pairs_to_remove : List [_BusNotifierPair ] = []
@@ -85,6 +85,26 @@ def unregister(self, bus: BusABC, notifier: "Notifier") -> None:
85
85
for pair in registered_pairs_to_remove :
86
86
self .pairs .remove (pair )
87
87
88
+ def find_instance (self , bus : BusABC ) -> Optional ["Notifier" ]:
89
+ """Find the :class:`~can.Notifier` instance associated with a given CAN bus.
90
+
91
+ This method searches the registry for the :class:`~can.Notifier`
92
+ that is linked to the specified bus. If the bus is found, the
93
+ corresponding :class:`~can.Notifier` instance is returned. If the bus is not
94
+ found in the registry, `None` is returned.
95
+
96
+ :param bus:
97
+ The CAN bus for which to find the associated :class:`~can.Notifier` .
98
+ :return:
99
+ The :class:`~can.Notifier` instance associated with the given bus,
100
+ or `None` if no such association exists.
101
+ """
102
+ with self .lock :
103
+ for pair in self .pairs :
104
+ if bus is pair .bus :
105
+ return pair .notifier
106
+ return None
107
+
88
108
89
109
class Notifier (AbstractContextManager ):
90
110
@@ -274,6 +294,23 @@ def stopped(self) -> bool:
274
294
"""Return ``True``, if Notifier was properly shut down with :meth:`~can.Notifier.stop`."""
275
295
return self ._stopped
276
296
297
+ @classmethod
298
+ def find_instance (cls , bus : BusABC ) -> Optional ["Notifier" ]:
299
+ """Find the :class:`~can.Notifier` instance associated with a given CAN bus.
300
+
301
+ This method searches the registry for the :class:`~can.Notifier`
302
+ that is linked to the specified bus. If the bus is found, the
303
+ corresponding :class:`~can.Notifier` instance is returned. If the bus is not
304
+ found in the registry, `None` is returned.
305
+
306
+ :param bus:
307
+ The CAN bus for which to find the associated :class:`~can.Notifier` .
308
+ :return:
309
+ The :class:`~can.Notifier` instance associated with the given bus,
310
+ or `None` if no such association exists.
311
+ """
312
+ return cls ._registry .find_instance (bus )
313
+
277
314
def __exit__ (
278
315
self ,
279
316
exc_type : Optional [Type [BaseException ]],
0 commit comments