From 5c07d10e830d6dbb118771df9b2a3d9335c5e583 Mon Sep 17 00:00:00 2001 From: Minos Galanakis Date: Mon, 15 Jul 2024 11:11:45 +0100 Subject: [PATCH] c_wrapper_generator: Added blockstr_format utility function. Signed-off-by: Minos Galanakis --- .../mbedtls_framework/c_wrapper_generator.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/scripts/mbedtls_framework/c_wrapper_generator.py b/scripts/mbedtls_framework/c_wrapper_generator.py index 8644137bc..32b8edcfd 100644 --- a/scripts/mbedtls_framework/c_wrapper_generator.py +++ b/scripts/mbedtls_framework/c_wrapper_generator.py @@ -11,7 +11,7 @@ import os import re import sys -import textwrap +import inspect from typing import Dict, NamedTuple, List, Optional, Tuple from .c_parsing_helper import ArgumentInfo, FunctionInfo @@ -31,6 +31,9 @@ WrapperInfo = NamedTuple('WrapperInfo', [ ('wrapper_name', str), ]) +def blockstr_format(in_str: str, new_lines: int = 1) -> str: + """Return a whitespace stripped str, appended with user defined empty lines.""" + return inspect.cleandoc(in_str) + ("\n" * (new_lines + 1)) class Base: """Generate a C source file containing wrapper functions.""" @@ -67,17 +70,17 @@ class Base: This includes a description comment and some include directives. """ - prologue = textwrap.dedent(f''' + prologue = blockstr_format(f''' /* Automatically generated by {self.program_name}, do not edit! */ /* Copyright The Mbed TLS Contributors * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ - ''').lstrip() + ''') if header: - prologue += textwrap.dedent(f''' + prologue += blockstr_format(f''' #ifndef {self.header_guard} #define {self.header_guard} @@ -85,7 +88,7 @@ class Base: extern "C" {{ #endif - ''').lstrip() + ''') for include in self._INCLUDES: prologue += "#include {}\n".format(include) @@ -100,14 +103,14 @@ class Base: """Write the epilogue of a C file.""" epilogue = "" if header: - epilogue += textwrap.dedent(f''' + epilogue += blockstr_format(f''' #ifdef __cplusplus }} #endif #endif /* {self.header_guard} */ - ''').lstrip() + ''') epilogue += ('/* End of automatically generated file. */\n') out.write(epilogue)