diff --git a/ros2autodoc/api/__init__.py b/ros2autodoc/api/__init__.py index 56d5b30..d64efa0 100644 --- a/ros2autodoc/api/__init__.py +++ b/ros2autodoc/api/__init__.py @@ -47,12 +47,12 @@ def get_nodes(package_name): return nodes -def document_node(node, package_name, node_name, path, file_name="/README.md"): +def document_node(node, package_name, node_name, file_path): """Document the given node.""" interface_collector = NodeInterfaceCollector(node, node_name) node_interface = interface_collector.get_interfaces() writer = DocWriter(package_name, node_name, node_interface) - writer.write(path + file_name) + writer.write(file_path) def check_node_documentation(node, node_name, file_path): diff --git a/ros2autodoc/api/node_interface_collector.py b/ros2autodoc/api/node_interface_collector.py index a803652..959add6 100644 --- a/ros2autodoc/api/node_interface_collector.py +++ b/ros2autodoc/api/node_interface_collector.py @@ -57,61 +57,66 @@ def get_interfaces(self): def _query_interfaces(self): # Parameters param_names, params_map, desciption_map = self._get_parameters() - if len(param_names) > 0: - for param in param_names: - if param not in PARAMS_TO_IGNORE: - param_type = params_map[param] - param_description = desciption_map[param] - if not param_description: - param_description = TODO - self.interfaces["parameters"][param] = { - "type": param_type, - "description": param_description, - } + if param_names: + self._add_parameter_info(param_names, params_map, desciption_map) + # Subscribers subscribers = get_subscriber_info( node=self.node, remote_node_name=self.node_name, include_hidden=False ) - if len(subscribers) > 0: - for sub in subscribers: - if sub.name not in SUBSCRIBERS_TO_IGNORE: - self.interfaces["subscribers"][sub.name] = { - "type": ", ".join(sub.types), - "description": TODO, - } + if subscribers: + self._add_interface_info( + self.interfaces["subscribers"], subscribers, SUBSCRIBERS_TO_IGNORE + ) + # Publishers publishers = get_publisher_info( node=self.node, remote_node_name=self.node_name, include_hidden=False ) - if len(publishers) > 0: - for pub in publishers: - if pub.name not in PUBLISHERS_TO_IGNORE: - self.interfaces["publishers"][pub.name] = { - "type": ", ".join(pub.types), - "description": TODO, - } + if publishers: + self._add_interface_info( + self.interfaces["publishers"], publishers, PUBLISHERS_TO_IGNORE + ) + # Services service_servers = get_service_server_info( node=self.node, remote_node_name=self.node_name, include_hidden=False ) - if len(service_servers) > 0: - for srv in service_servers: - if not self._ignore_service(self.node_name, srv.name): - self.interfaces["services"][srv.name] = { - "type": ", ".join(srv.types), - "description": TODO, - } + if service_servers: + filtered_services = [ + srv + for srv in service_servers + if not self._ignore_service(self.node_name, srv.name) + ] + self._add_interface_info(self.interfaces["services"], filtered_services, []) + # Actions actions_servers = get_action_server_info( node=self.node, remote_node_name=self.node_name, include_hidden=False ) - if len(actions_servers) > 0: - for action in actions_servers: - self.interfaces["actions"][action.name] = { - "type": ", ".join(action.types), + if actions_servers: + self._add_interface_info(self.interfaces["actions"], actions_servers, []) + + def _add_interface_info(self, interface_dict, items, ignore_list): + for item in items: + if item.name not in ignore_list: + interface_dict[item.name] = { + "type": ", ".join(item.types), "description": TODO, } + def _add_parameter_info(self, param_names, params_map, desciption_map): + for param in param_names: + if param not in PARAMS_TO_IGNORE: + param_type = params_map[param] + param_description = desciption_map[param] + if not param_description: + param_description = TODO + self.interfaces["parameters"][param] = { + "type": param_type, + "description": param_description, + } + def _get_parameters(self): name_to_type_map = {} name_to_description_map = {} diff --git a/ros2autodoc/verb/generate.py b/ros2autodoc/verb/generate.py index bf3750d..685791a 100644 --- a/ros2autodoc/verb/generate.py +++ b/ros2autodoc/verb/generate.py @@ -42,6 +42,9 @@ def main(self, *, args): if args.package_name and not check_for_package(args.package_name): return f"Package '{args.package_name}' could not be found." + # Ensure no trailing slash + args.output_dir = args.output_dir.rstrip("/") + with NodeStrategy(args) as node: for node_name in args.nodes: if not check_for_node(node, f"/{node_name}"): @@ -49,7 +52,12 @@ def main(self, *, args): continue if args.seperate_files: document_node( - node, None, node_name, args.output_dir, f"/{node_name}.md" + node, None, node_name, f"{args.output_dir}/{node_name}.md" ) else: - document_node(node, args.package_name, node_name, args.output_dir) + document_node( + node, + args.package_name, + node_name, + f"{args.output_dir}/README.md", + )