c_wrapper_generator: Refactored blockstr_format().

* Method has been renamed to strip_identation().
* Dependency to inspect module has been removed.
* Added ident_lv parameter.

Signed-off-by: Minos Galanakis <[email protected]>
This commit is contained in:
Minos Galanakis
2024-07-17 21:36:34 +01:00
parent 03bfc58000
commit 0ef9b2d05c
@@ -11,7 +11,6 @@
import os
import re
import sys
import inspect
from typing import Dict, NamedTuple, List, Optional, Tuple
from .c_parsing_helper import ArgumentInfo, FunctionInfo
@@ -31,9 +30,16 @@ 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))
def strip_indentation(in_str: str, new_lines: int = 1, ident_lv: int = 4) -> str:
"""Return a whitespace stripped str, appended with user defined empty lines.
The method will remove (ident_lv * 4) space-character identations from
input string. It will also remove all whitespace around the text-block.
At the end of the stream a `new_lines` ammount of empty lines will be added.
"""
return re.sub(r"(?m)^ {{{ident}}}".format(ident=ident_lv * 4), "",
in_str.strip()) + ("\n" * (new_lines + 1))
class Base:
"""Generate a C source file containing wrapper functions."""
@@ -70,17 +76,17 @@ class Base:
This includes a description comment and some include directives.
"""
prologue = blockstr_format(f'''
prologue = strip_indentation(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
*/
''')
''', ident_lv=3)
if header:
prologue += blockstr_format(f'''
prologue += strip_indentation(f'''
#ifndef {self.header_guard}
#define {self.header_guard}
@@ -102,7 +108,7 @@ class Base:
"""Write the epilogue of a C file."""
epilogue = ""
if header:
epilogue += blockstr_format(f'''
epilogue += strip_indentation(f'''
#ifdef __cplusplus
}}
#endif