Skip to content

rosidl_cli: Add type description support (backport #857) #866

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

Open
wants to merge 1 commit into
base: kilted
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions rosidl_cli/rosidl_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
from typing import Any, List, Union

from rosidl_cli.command.generate import GenerateCommand
from rosidl_cli.command.hash import HashCommand
from rosidl_cli.command.translate import TranslateCommand
from rosidl_cli.common import get_first_line_doc


def add_subparsers(
parser: argparse.ArgumentParser,
cli_name: str,
commands: List[Union[GenerateCommand, TranslateCommand]]
) -> argparse._SubParsersAction[argparse.ArgumentParser]:
commands: List[Union[GenerateCommand, HashCommand, TranslateCommand]]
) -> argparse._SubParsersAction:
"""
Create argparse subparser for each command.

Expand Down Expand Up @@ -79,8 +80,8 @@ def main() -> Union[str, signal.Signals, Any]:
formatter_class=argparse.RawDescriptionHelpFormatter
)

commands: List[Union[GenerateCommand, TranslateCommand]] = \
[GenerateCommand(), TranslateCommand()]
commands: List[Union[GenerateCommand, TranslateCommand, HashCommand]] = \
[GenerateCommand(), TranslateCommand(), HashCommand()]

# add arguments for command extension(s)
add_subparsers(
Expand Down
7 changes: 6 additions & 1 deletion rosidl_cli/rosidl_cli/command/generate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def add_arguments(self, parser: argparse.ArgumentParser) -> None:
'-ts', '--type-support', metavar='TYPESUPPORT',
dest='typesupports', action='append', default=[],
help='Target type supports for generation.')
parser.add_argument(
'-td', '--type-description-file', metavar='PATH',
dest='type_description_files', action='append', default=[],
help='Target type descriptions for generation.')
parser.add_argument(
'-I', '--include-path', type=pathlib.Path, metavar='PATH',
dest='include_paths', action='append', default=[],
Expand All @@ -58,5 +62,6 @@ def main(self, *, args: argparse.Namespace) -> None:
include_paths=args.include_paths,
output_path=args.output_path,
types=args.types,
typesupports=args.typesupports
typesupports=args.typesupports,
type_description_files=args.type_description_files
)
55 changes: 40 additions & 15 deletions rosidl_cli/rosidl_cli/command/generate/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import inspect
import os
import pathlib
from typing import List, Optional
from typing import Any, Callable, Dict, List, Optional

from .extensions import GenerateCommandExtension
from .extensions import load_type_extensions
from .extensions import load_typesupport_extensions
from .extensions import GenerateCommandExtension, load_type_extensions, load_typesupport_extensions


def generate(
Expand All @@ -28,7 +27,8 @@ def generate(
include_paths: Optional[List[str]] = None,
output_path: Optional[pathlib.Path] = None,
types: Optional[List[str]] = None,
typesupports: Optional[List[str]] = None
typesupports: Optional[List[str]] = None,
type_description_files: Optional[List[str]] = None
) -> List[List[str]]:
"""
Generate source code from interface definition files.
Expand Down Expand Up @@ -59,6 +59,7 @@ def generate(
source code files, defaults to the current working directory
:param types: optional list of type representations to generate
:param typesupports: optional list of type supports to generate
:param type_description_files: Optional list of paths to type description files
:returns: list of lists of paths to generated source code files,
one group per type or type support extension invoked
"""
Expand Down Expand Up @@ -87,15 +88,39 @@ def generate(
else:
os.makedirs(output_path, exist_ok=True)

if len(extensions) > 1:
return [
def extra_kwargs(func: Callable, **kwargs: Any) -> Dict[str, Any]:
matched_kwargs = {}
signature = inspect.signature(func)
for name, value in kwargs.items():
if name in signature.parameters:
if signature.parameters[name].kind not in [
inspect.Parameter.POSITIONAL_ONLY,
inspect.Parameter.VAR_POSITIONAL,
inspect.Parameter.VAR_KEYWORD
]:
matched_kwargs[name] = value
return matched_kwargs

generated_files = []
if len(extensions) == 1:
extension = extensions[0]
generated_files.append(
extension.generate(
package_name, interface_files, include_paths,
output_path=output_path / extension.name)
for extension in extensions
]

return [extensions[0].generate(
package_name, interface_files,
include_paths, output_path
)]
output_path=output_path,
**extra_kwargs(extension.generate, type_description_files=type_description_files)
)
)
else:
for extension in extensions:
generated_files.append(
extension.generate(
package_name, interface_files, include_paths,
output_path=output_path / extension.name,
**extra_kwargs(
extension.generate,
type_description_files=type_description_files
)
)
)
return generated_files
4 changes: 3 additions & 1 deletion rosidl_cli/rosidl_cli/command/generate/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def generate(
package_name: str,
interface_files: List[str],
include_paths: List[str],
output_path: Path
output_path: Path,
type_description_files: Optional[List[str]] = None
) -> List[str]:
"""
Generate source code.
Expand All @@ -46,6 +47,7 @@ def generate(
:param include_paths: list of paths to include dependency interface
definition files from.
:param output_path: path to directory to hold generated source code files
:param type_description_files: Optional list of paths to type description files
:returns: list of paths to generated source files
"""
raise NotImplementedError()
Expand Down
51 changes: 51 additions & 0 deletions rosidl_cli/rosidl_cli/command/hash/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2021 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pathlib

from rosidl_cli.command import Command

from .api import generate_type_hashes


class HashCommand(Command):
"""Generate type description hashes from interface definition files."""

name = 'hash'

def add_arguments(self, parser):
parser.add_argument(
'-o', '--output-path', metavar='PATH',
type=pathlib.Path, default=None,
help=('Path to directory to hold generated '
"source code files. Defaults to '.'."))
parser.add_argument(
'-I', '--include-path', type=pathlib.Path, metavar='PATH',
dest='include_paths', action='append', default=[],
help='Paths to include dependency interface definition files from.')
parser.add_argument(
'package_name', help='Name of the package to generate code for')
parser.add_argument(
'interface_files', metavar='interface_file', nargs='+',
help=('Relative path to an interface definition file. '
"If prefixed by another path followed by a colon ':', "
'path resolution is performed against such path.'))

def main(self, *, args):
generate_type_hashes(
package_name=args.package_name,
interface_files=args.interface_files,
include_paths=args.include_paths,
output_path=args.output_path,
)
68 changes: 68 additions & 0 deletions rosidl_cli/rosidl_cli/command/hash/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Copyright 2021 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pathlib

from .extensions import load_hash_extensions


def generate_type_hashes(
*,
package_name,
interface_files,
include_paths=None,
output_path=None,
):
"""
Generate type hashes from interface definition files.

To do so, this function leverages type description hash generation support
as provided by third-party package extensions.

Each path to an interface definition file is a relative path optionally
prefixed by another path followed by a colon ':', against which the first
relative path is to be resolved.

The directory structure that these relative paths exhibit will be replicated
on output (as opposed to the prefix path, which will be ignored).

:param package_name: name of the package to generate hashes for
:param interface_files: list of paths to interface definition files
:param include_paths: optional list of paths to include dependency
interface definition files from
:param output_path: optional path to directory to hold generated
source code files, defaults to the current working directory
:returns: list of lists of paths to generated hashed json files,
one group per type or type support extension invoked
"""
extensions = []
extensions.extend(load_hash_extensions())

if include_paths is None:
include_paths = []
if output_path is None:
output_path = pathlib.Path.cwd()
else:
pathlib.Path.mkdir(output_path, parents=True, exist_ok=True)

generated_hashes = []
for extension in extensions:
generated_hashes.extend(extension.generate_type_hashes(
package_name,
interface_files,
include_paths,
output_path=output_path,
))

return generated_hashes
55 changes: 55 additions & 0 deletions rosidl_cli/rosidl_cli/command/hash/extensions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright 2021 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from rosidl_cli.extensions import Extension
from rosidl_cli.extensions import load_extensions


class HashCommandExtension(Extension):
"""
The extension point for source code generation.

The following methods must be defined:
* `generate_type_hashes`
"""

def generate_type_hashes(
self,
package_name,
interface_files,
include_paths,
output_path,
):
"""
Generate type hashes from interface definition files.

Paths to interface definition files are relative paths optionally
prefixed by an absolute path followed by a colon ':', in which case
path resolution is to be performed against that absolute path.

:param package_name: name of the package to generate source code for
:param interface_files: list of paths to interface definition files
:param include_paths: list of paths to include dependency interface
definition files from.
:param output_path: path to directory to hold generated source code files
:returns: list of paths to generated source files
"""
raise NotImplementedError()


def load_hash_extensions(**kwargs):
"""Load extensions for type hash generation."""
return load_extensions(
'rosidl_cli.command.hash.extensions', **kwargs
)
Loading