|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import inspect |
| 3 | +from pathlib import Path, PurePosixPath |
| 4 | + |
| 5 | +""""Script for generating a C++ file that contains the content from all SDF files""" |
| 6 | + |
| 7 | +# The list of supported SDF specification versions. This will let us drop |
| 8 | +# versions without removing the directories. |
| 9 | +SUPPORTED_SDF_VERSIONS = ['1.10', '1.9', '1.8', '1.7', '1.6', '1.5', '1.4', '1.3', '1.2'] |
| 10 | + |
| 11 | +# The list of supported SDF conversions. This list includes versions that |
| 12 | +# a user can convert an existing SDF version to. |
| 13 | +SUPPORTED_SDF_CONVERSIONS = ['1.10', '1.9', '1.8', '1.7', '1.6', '1.5', '1.4', '1.3'] |
| 14 | + |
| 15 | +# whitespace indentation for C++ code |
| 16 | +INDENTATION = ' ' |
| 17 | +# newline character |
| 18 | +NEWLINE = '\n' |
| 19 | + |
| 20 | +def get_copyright_notice() -> str: |
| 21 | + """ |
| 22 | + Provides the copyrigt notice for the C++ file |
| 23 | +
|
| 24 | + :returns: copyright notice |
| 25 | + """ |
| 26 | + res = inspect.cleandoc(""" |
| 27 | + /* |
| 28 | + * Copyright 2022 Open Source Robotics Foundation |
| 29 | + * |
| 30 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 31 | + * you may not use this file except in compliance with the License. |
| 32 | + * You may obtain a copy of the License at |
| 33 | + * |
| 34 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 35 | + * |
| 36 | + * Unless required by applicable law or agreed to in writing, software |
| 37 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 38 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 39 | + * See the License for the specific language governing permissions and |
| 40 | + * limitations under the License. |
| 41 | + * |
| 42 | + */ |
| 43 | + """) |
| 44 | + return res + 2*NEWLINE |
| 45 | + |
| 46 | + |
| 47 | +def get_file_header_prolog() -> str: |
| 48 | + """ |
| 49 | + Provides the include statement, namespace and variable declaration of the C++ file |
| 50 | +
|
| 51 | + :returns: prolog of the C++ file |
| 52 | + """ |
| 53 | + res = inspect.cleandoc(""" |
| 54 | + #include "EmbeddedSdf.hh" |
| 55 | +
|
| 56 | + namespace sdf |
| 57 | + { |
| 58 | + inline namespace SDF_VERSION_NAMESPACE |
| 59 | + { |
| 60 | + ///////////////////////////////////////////////// |
| 61 | + const std::map<std::string, std::string> &GetEmbeddedSdf() |
| 62 | + { |
| 63 | + static const std::map<std::string, std::string> result { |
| 64 | + """) |
| 65 | + return res + NEWLINE |
| 66 | + |
| 67 | + |
| 68 | +def embed_sdf_content(arg_path: str, arg_file_content: str) -> str: |
| 69 | + """ |
| 70 | + Generates a string pair with the folder and filename as well as the content of the file |
| 71 | +
|
| 72 | + :param arg_path: Foldername and filename of the SDF |
| 73 | + :param arg_file_content: Content of the provided file |
| 74 | + :returns: raw string literal mapping pair for the std::map |
| 75 | + """ |
| 76 | + res = [] |
| 77 | + res.append('// NOLINT') |
| 78 | + res.append('{') |
| 79 | + res.append(f'"{arg_path}",') |
| 80 | + res.append('R"__sdf_literal__(') |
| 81 | + res.append(f'{arg_file_content}') |
| 82 | + res.append(')__sdf_literal__"') |
| 83 | + res.append('},') |
| 84 | + res.append('') |
| 85 | + return NEWLINE.join(res) |
| 86 | + |
| 87 | + |
| 88 | +def get_map_content(arg_pathlist: Path) -> str: |
| 89 | + """ |
| 90 | + Generates a string pair with the folder and filename as well as the content |
| 91 | + of the file in ascending order |
| 92 | +
|
| 93 | + :param arg_pathlist: Foldername and all filenames inside it |
| 94 | + :returns: mapping pairs for the std::map |
| 95 | + """ |
| 96 | + map_str = '' |
| 97 | + files = [] |
| 98 | + for path in arg_pathlist: |
| 99 | + # dir separator is hardcoded to '/' in C++ mapping |
| 100 | + posix_path = PurePosixPath(path) |
| 101 | + files.append(str(posix_path)) |
| 102 | + # get ascending order |
| 103 | + files.sort() |
| 104 | + for file in files: |
| 105 | + with Path(file).open() as f: |
| 106 | + content = f.read() |
| 107 | + map_str += embed_sdf_content(file, content) |
| 108 | + return map_str |
| 109 | + |
| 110 | + |
| 111 | +def get_file_header_epilog() -> str: |
| 112 | + """ |
| 113 | + Provides the return statement and the closing brackets of the C++ file |
| 114 | +
|
| 115 | + :returns: epilog of the C++ file |
| 116 | + """ |
| 117 | + res = inspect.cleandoc(""" |
| 118 | + }; |
| 119 | +
|
| 120 | + return result; |
| 121 | + } |
| 122 | +
|
| 123 | + } |
| 124 | + } // namespace sdf |
| 125 | +
|
| 126 | + """) |
| 127 | + return NEWLINE + res |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + copyright = get_copyright_notice() |
| 132 | + prolog = get_file_header_prolog() |
| 133 | + |
| 134 | + map_str = "" |
| 135 | + for sdf_version in SUPPORTED_SDF_VERSIONS: |
| 136 | + pathlist = Path(sdf_version).glob('*.sdf') |
| 137 | + map_str += get_map_content(pathlist) |
| 138 | + |
| 139 | + for sdf_conversion in SUPPORTED_SDF_CONVERSIONS: |
| 140 | + pathlist = Path(sdf_conversion).glob('*.convert') |
| 141 | + map_str += get_map_content(pathlist) |
| 142 | + |
| 143 | + # remove the last comma |
| 144 | + map_str = map_str[:-2] |
| 145 | + |
| 146 | + epilog = get_file_header_epilog() |
| 147 | + |
| 148 | + output = copyright + prolog + map_str + epilog |
| 149 | + |
| 150 | + # output to stdin so that CMake can read it and create the appropriate file |
| 151 | + print(output) |
0 commit comments