From 41a9154ca7194eecb411ae86cf70f535d05b0acc Mon Sep 17 00:00:00 2001
From: cmoineau <cyril.moineau@cea.fr>
Date: Wed, 24 Apr 2024 08:36:02 +0000
Subject: [PATCH] Add generate_str function to aidge_core

---
 aidge_core/__init__.py               |  2 +-
 aidge_core/export/code_generation.py | 16 +++++++++++++++-
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/aidge_core/__init__.py b/aidge_core/__init__.py
index 701e25323..4b5c44835 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 f267ac52b..f89b2ad1f 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)
-- 
GitLab