Skip to content

Commit 24f0e20

Browse files
authored
Update node name matcher (#282)
* Make slash prefix for node name matching optional The node name must be fully qualified, with a namespace, for matching. This change makes the slash optional for convenience. For example, the following two calls are now equivalent: matches_node_name('/foo') matches_node_name('foo') Signed-off-by: Jacob Perron <[email protected]> * Move node name matcher into matchers module The matcher function 'matches_node_name' is not specific to lifecycle nodes, so it makes sense for it to live in a more general module. Add a deprecation warning to the matcher in the lifecycle module. Signed-off-by: Jacob Perron <[email protected]> * Add tests for matcher Signed-off-by: Jacob Perron <[email protected]> * Move into matchers folder Signed-off-by: Jacob Perron <[email protected]>
1 parent d31044d commit 24f0e20

File tree

5 files changed

+98
-1
lines changed

5 files changed

+98
-1
lines changed

launch_ros/launch_ros/events/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"""events Module."""
1616

1717
from . import lifecycle
18+
from .matchers import matches_node_name
1819

1920
__all__ = [
2021
'lifecycle',
22+
'matches_node_name',
2123
]

launch_ros/launch_ros/events/lifecycle/lifecycle_node_matchers.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
from typing import Callable
1818
from typing import Text
19+
import warnings
20+
21+
from ..matchers import matches_node_name as _matches_node_name
1922

2023
if False:
2124
# imports here would cause loops, but are only used as forward-references for type-checking
@@ -24,4 +27,8 @@
2427

2528
def matches_node_name(node_name: Text) -> Callable[['LifecycleNode'], bool]:
2629
"""Return a matcher which matches based on the name of the node itself."""
27-
return lambda action: action.node_name == node_name
30+
warnings.warn(
31+
"'matches_node_name' has been moved into the 'launch.events' module and will be removed "
32+
"from the 'lifecycle' module in the future"
33+
)
34+
return _matches_node_name(node_name)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2021 Open Source Robotics Foundation, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""events.matchers module."""
16+
17+
from .matches_node_name import matches_node_name
18+
19+
__all__ = [
20+
'matches_node_name',
21+
]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2018 Open Source Robotics Foundation, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Matchers for node names."""
16+
17+
from typing import Callable
18+
from typing import Text
19+
20+
if False:
21+
# imports here would cause loops, but are only used as forward-references for type-checking
22+
from ..actions import Node # noqa: F401
23+
24+
25+
def matches_node_name(node_name: Text) -> Callable[['Node'], bool]:
26+
"""Return a matcher which matches based on the name of the node itself."""
27+
if not node_name.startswith('/'):
28+
node_name = f'/{node_name}'
29+
return lambda action: action.node_name == node_name
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2021 Open Source Robotics Foundation, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Tests for the matchers module."""
16+
17+
from launch_ros.events import matches_node_name
18+
19+
20+
class MockNode:
21+
"""Mock node action."""
22+
23+
def __init__(self, node_name):
24+
self.node_name = node_name
25+
26+
27+
def test_matches_node_name():
28+
"""Test the matches_node_name function."""
29+
matcher = matches_node_name('/foo')
30+
31+
assert matcher(MockNode('/foo'))
32+
assert not matcher(MockNode('/foo/bar'))
33+
34+
# Without '/' prefix
35+
matcher = matches_node_name('foo')
36+
37+
assert matcher(MockNode('/foo'))
38+
assert not matcher(MockNode('/foo/bar'))

0 commit comments

Comments
 (0)