Skip to content

Commit e35610a

Browse files
committed
implement Notifier.find_instance(bus)
1 parent 6fc0d7d commit e35610a

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

can/notifier.py

+40-3
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ def __init__(self) -> None:
5050
def register(self, bus: BusABC, notifier: "Notifier") -> None:
5151
"""Register a bus and its associated notifier.
5252
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.
5454
5555
:param bus:
5656
The CAN bus to register.
5757
:param notifier:
58-
The Notifier instance associated with the bus.
58+
The :class:`~can.Notifier` instance associated with the bus.
5959
:raises ValueError:
6060
If the bus is already assigned to an active Notifier.
6161
"""
@@ -75,7 +75,7 @@ def unregister(self, bus: BusABC, notifier: "Notifier") -> None:
7575
:param bus:
7676
The CAN bus to unregister.
7777
:param notifier:
78-
The Notifier instance associated with the bus.
78+
The :class:`~can.Notifier` instance associated with the bus.
7979
"""
8080
with self.lock:
8181
registered_pairs_to_remove: List[_BusNotifierPair] = []
@@ -85,6 +85,26 @@ def unregister(self, bus: BusABC, notifier: "Notifier") -> None:
8585
for pair in registered_pairs_to_remove:
8686
self.pairs.remove(pair)
8787

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+
88108

89109
class Notifier(AbstractContextManager):
90110

@@ -274,6 +294,23 @@ def stopped(self) -> bool:
274294
"""Return ``True``, if Notifier was properly shut down with :meth:`~can.Notifier.stop`."""
275295
return self._stopped
276296

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+
277314
def __exit__(
278315
self,
279316
exc_type: Optional[Type[BaseException]],

test/notifier_test.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,24 @@ def test_context_manager(self):
5252
def test_registry(self):
5353
with can.Bus("test", interface="virtual", receive_own_messages=True) as bus:
5454
reader = can.BufferedReader()
55-
with can.Notifier(bus, [reader], 0.1):
55+
with can.Notifier(bus, [reader], 0.1) as notifier:
5656
# creating a second notifier for the same bus must fail
5757
self.assertRaises(ValueError, can.Notifier, bus, [reader], 0.1)
5858

59+
# find_instance must return the existing instance
60+
self.assertIs(can.Notifier.find_instance(bus), notifier)
61+
62+
# Notifier is stopped, find instance must return None
63+
self.assertIsNone(can.Notifier.find_instance(bus))
64+
5965
# now the first notifier is stopped, a new notifier can be created without error:
60-
with can.Notifier(bus, [reader], 0.1):
66+
with can.Notifier(bus, [reader], 0.1) as notifier:
6167
# the next notifier call should fail again since there is an active notifier already
6268
self.assertRaises(ValueError, can.Notifier, bus, [reader], 0.1)
6369

70+
# find_instance must return the existing instance
71+
self.assertIs(can.Notifier.find_instance(bus), notifier)
72+
6473

6574
class AsyncNotifierTest(unittest.TestCase):
6675
def test_asyncio_notifier(self):

0 commit comments

Comments
 (0)