Skip to content

Commit

Permalink
Annotate duplicate nodes in the ros2 node 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 Feb 27, 2020
1 parent 1038758 commit 3c2c67c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
20 changes: 20 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 List
from typing import Tuple

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 @@ -47,6 +49,24 @@ def parse_node_name(node_name):
return NodeName(node_basename, namespace, full_node_name)


def annotate_duplicate_nodes(node_names: List[NodeName]) -> Tuple[bool, List[str]]:
annotation_string = ' (non-unique name)'

any_duplicates = False
name_count = {}
for n in node_names:
count = name_count.get(n.full_name, 0) + 1
if count > 1:
any_duplicates = True
name_count[n.full_name] = name_count.get(n.full_name, 0) + 1

sorted_names = sorted(
n.full_name + (annotation_string if name_count[n.full_name] > 1 else '')
for n in node_names
)
return any_duplicates, sorted_names


def get_node_names(*, node, include_hidden_nodes=False):
node_names_and_namespaces = node.get_node_names_and_namespaces()
return [
Expand Down
6 changes: 5 additions & 1 deletion ros2node/ros2node/verb/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

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

Expand All @@ -37,4 +38,7 @@ 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')
has_duplicates, sorted_names = annotate_duplicate_nodes(node_names)
if has_duplicates:
print('# WARNING: There are nodes in the graph that share a name')
print(*sorted_names, sep='\n')

0 comments on commit 3c2c67c

Please sign in to comment.