diff --git a/aidge_core/__init__.py b/aidge_core/__init__.py index 701e25323642a1d02b4abc2b3c0fae3a6e1533ee..4b5c448355a17fd4274ba45f5cd98afa70b1ae53 100644 --- a/aidge_core/__init__.py +++ b/aidge_core/__init__.py @@ -8,5 +8,5 @@ http://www.eclipse.org/legal/epl-2.0. SPDX-License-Identifier: EPL-2.0 """ from aidge_core.aidge_core import * # import so generated by PyBind -from aidge_core.export import ExportNode, generate_file +from aidge_core.export import ExportNode, generate_file, generate_str import aidge_core.utils diff --git a/aidge_core/export/code_generation.py b/aidge_core/export/code_generation.py index f267ac52b1044adf13ff06a9fcb38400e979ff0e..f89b2ad1f261745cde3ae6bdc7b1d1bb5cadba98 100644 --- a/aidge_core/export/code_generation.py +++ b/aidge_core/export/code_generation.py @@ -4,7 +4,7 @@ from jinja2 import Environment, FileSystemLoader def generate_file(file_path: str, template_path: str, **kwargs) -> None: """Generate a file at `file_path` using the jinja template located at `file_path`. - kwargs are used + kwargs are used to fill the template :param file_path: path where to generate the file :type file_path: str @@ -30,3 +30,17 @@ def generate_file(file_path: str, template_path: str, **kwargs) -> None: content = template.render(kwargs) with open(file_path, mode="w", encoding="utf-8") as message: message.write(content) + +def generate_str(template_path:str, **kwargs) -> str: + """Generate a string using the jinja template located at `file_path`. + kwargs are used to fill the template. + + :param template_path: Path to the template to use for code generation + :type template_path: str + :return: A string of the interpreted template + :rtype: str + """ + dirname = os.path.dirname(template_path) + filename = os.path.basename(template_path) + template = Environment(loader=FileSystemLoader(dirname)).get_template(filename) + return template.render(kwargs)