Skip to content

Commit

Permalink
Expose Python code generation via rosidl generate CLI
Browse files Browse the repository at this point in the history
Signed-off-by: Michel Hidalgo <michel@ekumenlabs.com>
  • Loading branch information
hidmic committed Mar 1, 2021
1 parent eb8d406 commit 534167d
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 4 deletions.
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())
1 change: 1 addition & 0 deletions rosidl_generator_py/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<buildtool_export_depend>rosidl_typesupport_interface</buildtool_export_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

0 comments on commit 534167d

Please sign in to comment.