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

Expose Python code generation via rosidl generate CLI #123

Merged
merged 2 commits into from
Mar 10, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions rosidl_generator_py/bin/rosidl_generator_py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def main(argv=sys.argv[1:]):
help='All the available typesupport implementations')
args = parser.parse_args(argv)

return generate_py(args.generator_arguments_file, args.typesupport_impls.split(';'))

generate_py(args.generator_arguments_file, args.typesupport_impls.split(';'))
return 0

if __name__ == '__main__':
sys.exit(main())
2 changes: 2 additions & 0 deletions rosidl_generator_py/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
<buildtool_export_depend>rosidl_typesupport_c</buildtool_export_depend>
<buildtool_export_depend>rosidl_typesupport_interface</buildtool_export_depend>

<exec_depend>ament_index_python</exec_depend>
<exec_depend>python3-numpy</exec_depend>
<exec_depend>rosidl_cli</exec_depend>
<exec_depend>rosidl_generator_c</exec_depend>
<exec_depend>rosidl_parser</exec_depend>
<exec_depend>rosidl_runtime_c</exec_depend>
Expand Down
71 changes: 71 additions & 0 deletions rosidl_generator_py/rosidl_generator_py/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# 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 ament_index_python import get_package_share_directory
from ament_index_python import get_resources

from rosidl_cli.command.generate.extensions import GenerateCommandExtension
from rosidl_cli.command.helpers import legacy_generator_arguments_file
from rosidl_cli.command.translate.api import translate

from rosidl_generator_py import generate_py


class GeneratePython(GenerateCommandExtension):

def generate(
self,
package_name,
interface_files,
include_paths,
output_path
):
package_share_path = \
pathlib.Path(get_package_share_directory('rosidl_generator_py'))
templates_path = package_share_path / 'resource'

# Normalize interface definition format to .idl
idl_interface_files = []
non_idl_interface_files = []
for path in interface_files:
if not path.endswith('.idl'):
non_idl_interface_files.append(path)
else:
idl_interface_files.append(path)
if non_idl_interface_files:
idl_interface_files.extend(translate(
package_name=package_name,
interface_files=non_idl_interface_files,
include_paths=include_paths,
output_format='idl',
output_path=output_path / 'tmp',
))

# Generate code
typesupport_implementations = ['rosidl_typesupport_c']
typesupport_implementations.extend(
get_resources('rosidl_typesupport_c')
)
with legacy_generator_arguments_file(
package_name=package_name,
interface_files=idl_interface_files,
include_paths=include_paths,
templates_path=templates_path,
output_path=output_path
) as path_to_arguments_file:
return generate_py(
path_to_arguments_file,
typesupport_implementations)
5 changes: 3 additions & 2 deletions rosidl_generator_py/rosidl_generator_py/generate_py_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def generate_py(generator_arguments_file, typesupport_impls):
'_idl.py.em': '_%s.py',
'_idl_support.c.em': '_%s_s.c',
}
generate_files(generator_arguments_file, mapping)
generated_files = generate_files(generator_arguments_file, mapping)

args = read_generator_arguments(generator_arguments_file)
package_name = args['package_name']
Expand Down Expand Up @@ -157,8 +157,9 @@ def print_warning_if_reserved_keyword(member_name, interface_type, interface_nam
expand_template(
template_file, data, generated_file,
minimum_timestamp=latest_target_timestamp)
generated_files.append(generated_file)

return 0
return generated_files


def value_to_py(type_, value, array_as_tuple=False):
Expand Down
3 changes: 3 additions & 0 deletions rosidl_generator_py/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[options.entry_points]
rosidl_cli.command.generate.type_extensions =
py = rosidl_generator_py.cli:GeneratePython