|
| 1 | +# Copyright 2021 Open Source Robotics Foundation, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import collections |
| 16 | +import os |
| 17 | +import pathlib |
| 18 | + |
| 19 | +from rosidl_cli.command import Command |
| 20 | + |
| 21 | +from .extensions import load_translate_extensions |
| 22 | + |
| 23 | + |
| 24 | +class TranslateCommand(Command): |
| 25 | + """Translate interface definition files from one format to another.""" |
| 26 | + |
| 27 | + name = 'translate' |
| 28 | + |
| 29 | + def add_arguments(self, parser): |
| 30 | + parser.add_argument( |
| 31 | + '-o', '--output-path', metavar='PATH', |
| 32 | + type=pathlib.Path, default=pathlib.Path.cwd(), |
| 33 | + help=('Path to directory to hold translated interface definition' |
| 34 | + "files. Defaults to '.'.")) |
| 35 | + parser.add_argument( |
| 36 | + '--use', '--translator', metavar='TRANSLATOR_SPEC', |
| 37 | + dest='translator_specs', action='append', default=[], |
| 38 | + help=('Translators to be used. If none is given, ' |
| 39 | + 'suitable available ones will be used.') |
| 40 | + ) |
| 41 | + parser.add_argument( |
| 42 | + '--to', '--output-format', required=True, |
| 43 | + metavar='FORMAT', dest='output_format', |
| 44 | + help='Output format for translate interface definition files.' |
| 45 | + ) |
| 46 | + parser.add_argument( |
| 47 | + '--from', '--input-format', default=None, |
| 48 | + metavar='FORMAT', dest='input_format', |
| 49 | + help=('Input format for all source interface definition files. ' |
| 50 | + 'If not given, file extensions will be used to deduce ' |
| 51 | + 'the format of each interface definition file.') |
| 52 | + ) |
| 53 | + parser.add_argument( |
| 54 | + '-I', '--include-path', metavar='PATH', type=pathlib.Path, |
| 55 | + dest='include_paths', action='append', default=[], |
| 56 | + help='Paths to include dependency interface definition files from.' |
| 57 | + ) |
| 58 | + parser.add_argument( |
| 59 | + 'package_name', |
| 60 | + help='Name of the package all interface files belong to') |
| 61 | + parser.add_argument( |
| 62 | + 'interface_files', metavar='interface_file', nargs='+', |
| 63 | + help=('Normalized relative path to an interface definition file. ' |
| 64 | + "If prefixed by another path followed by a colon ':', " |
| 65 | + 'path resolution is performed against such path.') |
| 66 | + ) |
| 67 | + |
| 68 | + def main(self, *, args): |
| 69 | + extensions = load_translate_extensions( |
| 70 | + specs=args.translator_specs, |
| 71 | + strict=any(args.translator_specs) |
| 72 | + ) |
| 73 | + if not extensions: |
| 74 | + return 'No translate extensions found' |
| 75 | + |
| 76 | + if not args.input_format: |
| 77 | + interface_files_per_format = collections.defaultdict(list) |
| 78 | + for interface_file in args.interface_files: |
| 79 | + input_format = os.path.splitext(interface_file)[-1][1:] |
| 80 | + interface_files_per_format[input_format].append(interface_file) |
| 81 | + else: |
| 82 | + interface_files_per_format = { |
| 83 | + args.input_format: args.interface_files} |
| 84 | + |
| 85 | + for input_format, interface_files in interface_files_per_format.items(): |
| 86 | + extension = next(( |
| 87 | + extension for extension in extensions |
| 88 | + if extension.input_format == input_format and \ |
| 89 | + extension.output_format == args.output_format |
| 90 | + ), None) |
| 91 | + |
| 92 | + if not extension: |
| 93 | + return (f"Translation from '{input_format}' to " |
| 94 | + f"'{args.output_format}' is not supported") |
| 95 | + |
| 96 | + extension.translate( |
| 97 | + args.package_name, interface_files, |
| 98 | + args.include_paths, args.output_path) |
0 commit comments