Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node name with namespace #146

Merged
merged 1 commit into from Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion ros2cli/ros2cli/daemon/__init__.py
Expand Up @@ -55,7 +55,8 @@ def main(*, script_name='_ros2_daemon', argv=None):

# expose getter functions of node
server.register_function(
_print_invoked_function_name(node.get_node_names))
_print_invoked_function_name(
node.get_node_names_and_namespaces))
server.register_function(
_print_invoked_function_name(node.get_topic_names_and_types))
server.register_function(
Expand Down
31 changes: 21 additions & 10 deletions ros2node/ros2node/api/__init__.py
Expand Up @@ -12,19 +12,29 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from collections import namedtuple

from ros2cli.node.strategy import NodeStrategy

# TODO(mikaelarguedas) revisit this once it's specified
HIDDEN_NODE_PREFIX = '_'

NodeName = namedtuple('NodeName', ('name', 'namespace', 'full_name'))


def get_node_names(*, node, include_hidden_nodes=False):
node_names = node.get_node_names()
if not include_hidden_nodes:
node_names = [
n for n in node_names
if n and not n.startswith(HIDDEN_NODE_PREFIX)]
return node_names
node_names_and_namespaces = node.get_node_names_and_namespaces()
return [
NodeName(
name=t[0],
namespace=t[1],
full_name=t[1] + ('' if t[1].endswith('/') else '/') + t[0])
for t in node_names_and_namespaces
if (
include_hidden_nodes or
(t[0] and not t[0].startswith(HIDDEN_NODE_PREFIX))
)
]


class NodeNameCompleter:
Expand All @@ -35,7 +45,8 @@ def __init__(self, *, include_hidden_nodes_key=None):

def __call__(self, prefix, parsed_args, **kwargs):
with NodeStrategy(parsed_args) as node:
return get_node_names(
node=node,
include_hidden_nodes=getattr(
parsed_args, self.include_hidden_nodes_key))
return [
n.full_name for n in get_node_names(
node=node,
include_hidden_nodes=getattr(
parsed_args, self.include_hidden_nodes_key))]
2 changes: 1 addition & 1 deletion ros2node/ros2node/verb/list.py
Expand Up @@ -37,4 +37,4 @@ def main(self, *, args):
if args.count_nodes:
print(len(node_names))
elif node_names:
print(*node_names, sep='\n')
print(*[n.full_name for n in node_names], sep='\n')
4 changes: 2 additions & 2 deletions ros2param/ros2param/api/__init__.py
Expand Up @@ -57,7 +57,7 @@ def call_get_parameters(*, node, node_name, parameter_names):
# create client
client = node.create_client(
GetParameters,
'/{node_name}/get_parameters'.format_map(locals()))
'{node_name}/get_parameters'.format_map(locals()))

# call as soon as ready
ready = client.wait_for_service(timeout_sec=5.0)
Expand All @@ -83,7 +83,7 @@ def call_set_parameters(*, node, node_name, parameters):
# create client
client = node.create_client(
SetParameters,
'/{node_name}/set_parameters'.format_map(locals()))
'{node_name}/set_parameters'.format_map(locals()))

# call as soon as ready
ready = client.wait_for_service(timeout_sec=5.0)
Expand Down
2 changes: 1 addition & 1 deletion ros2param/ros2param/verb/delete.py
Expand Up @@ -46,7 +46,7 @@ def main(self, *, args): # noqa: D102
node_names = get_node_names(
node=node, include_hidden_nodes=args.include_hidden_nodes)

if args.node_name not in node_names:
if args.node_name not in [n.full_name for n in node_names]:
return 'Node not found'

with DirectNode(args) as node:
Expand Down
2 changes: 1 addition & 1 deletion ros2param/ros2param/verb/get.py
Expand Up @@ -45,7 +45,7 @@ def main(self, *, args): # noqa: D102
node_names = get_node_names(
node=node, include_hidden_nodes=args.include_hidden_nodes)

if args.node_name not in node_names:
if args.node_name not in [n.full_name for n in node_names]:
return 'Node not found'

with DirectNode(args) as node:
Expand Down
11 changes: 6 additions & 5 deletions ros2param/ros2param/verb/list.py
Expand Up @@ -46,9 +46,10 @@ def main(self, *, args): # noqa: D102
node=node, include_hidden_nodes=args.include_hidden_nodes)

if args.node_name:
if args.node_name not in node_names:
if args.node_name not in [n.full_name for n in node_names]:
return 'Node not found'
node_names = [args.node_name]
node_names = [
n for n in node_names if args.node_name == n.full_name]

with DirectNode(args) as node:
clients = {}
Expand All @@ -57,7 +58,7 @@ def main(self, *, args): # noqa: D102
for node_name in node_names:
client = node.create_client(
ListParameters,
'/{node_name}/list_parameters'.format_map(locals()))
'{node_name.full_name}/list_parameters'.format_map(locals()))
clients[node_name] = client

# wait until all clients have been called
Expand Down Expand Up @@ -85,13 +86,13 @@ def main(self, *, args): # noqa: D102
future = futures[node_name]
if future.result() is not None:
if not args.node_name:
print('{node_name}:'.format_map(locals()))
print('{node_name.full_name}:'.format_map(locals()))
response = future.result()
for name in sorted(response.result.names):
print(' {name}'.format_map(locals()))
else:
e = future.exception()
print(
'Exception while calling service of node '
"'{node_name}': {e}".format_map(locals()),
"'{node_name.full_name}': {e}".format_map(locals()),
file=sys.stderr)
2 changes: 1 addition & 1 deletion ros2param/ros2param/verb/set.py
Expand Up @@ -47,7 +47,7 @@ def main(self, *, args): # noqa: D102
node_names = get_node_names(
node=node, include_hidden_nodes=args.include_hidden_nodes)

if args.node_name not in node_names:
if args.node_name not in [n.full_name for n in node_names]:
return 'Node not found'

with DirectNode(args) as node:
Expand Down