Skip to content

Commit

Permalink
Just add warning at the top of the node names list
Browse files Browse the repository at this point in the history
Signed-off-by: Emerson Knapp <emerson.b.knapp@gmail.com>
  • Loading branch information
emersonknapp committed Mar 13, 2020
1 parent 77271e4 commit b66cf91
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
7 changes: 7 additions & 0 deletions ros2node/ros2node/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# limitations under the License.

from collections import namedtuple
from typing import Any
from typing import List

from rclpy.action.graph import get_action_client_names_and_types_by_node
from rclpy.action.graph import get_action_server_names_and_types_by_node
Expand Down Expand Up @@ -51,6 +53,11 @@ def parse_node_name(node_name):
return NodeName(node_basename, namespace, full_node_name)


def has_duplicates(values: List[Any]) -> bool:
"""Find out if there are any exact duplicates in a list of strings."""
return len(set(values)) < len(values)


def get_node_names(*, node, include_hidden_nodes=False):
node_names_and_namespaces = node.get_node_names_and_namespaces()
return [
Expand Down
9 changes: 8 additions & 1 deletion ros2node/ros2node/verb/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import sys

from ros2cli.node.strategy import add_arguments
from ros2cli.node.strategy import NodeStrategy
from ros2node.api import get_node_names
from ros2node.api import has_duplicates
from ros2node.verb import VerbExtension


Expand All @@ -37,4 +40,8 @@ def main(self, *, args):
if args.count_nodes:
print(len(node_names))
elif node_names:
print(*sorted(n.full_name for n in node_names), sep='\n')
sorted_names = sorted(n.full_name for n in node_names)
if has_duplicates(sorted_names):
print('WARNING: Be aware that are nodes in the graph that share an exact name, '
'this can have unintended side effects.', file=sys.stderr)
print(*sorted_names, sep='\n')
9 changes: 9 additions & 0 deletions ros2node/test/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from ros2node.api import has_duplicates
from ros2node.api import parse_node_name


Expand All @@ -34,3 +35,11 @@ def test_parse_node_name():
assert name.full_name == '/ns/talker'
assert name.namespace == '/ns'
assert name.name == 'talker'


def test_has_duplicates():
assert not has_duplicates([])
assert not has_duplicates(['ns_foo/foo', 'ns_foo/bar'])
assert has_duplicates(['ns_foo/foo'] * 2)
assert has_duplicates(['ns_foo/foo'] * 3)
assert has_duplicates(['ns_foo/foo', 'ns_foo/foo', 'ns_foo/bar'])

0 comments on commit b66cf91

Please sign in to comment.