Skip to content
Snippets Groups Projects
Commit 41a9154c authored by Cyril Moineau's avatar Cyril Moineau
Browse files

Add generate_str function to aidge_core

parent 52dbd88e
No related branches found
No related tags found
2 merge requests!1190.2.1,!109Add template_docstring decorator which allow to template Python docstring.
...@@ -8,5 +8,5 @@ http://www.eclipse.org/legal/epl-2.0. ...@@ -8,5 +8,5 @@ http://www.eclipse.org/legal/epl-2.0.
SPDX-License-Identifier: EPL-2.0 SPDX-License-Identifier: EPL-2.0
""" """
from aidge_core.aidge_core import * # import so generated by PyBind 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 import aidge_core.utils
...@@ -4,7 +4,7 @@ from jinja2 import Environment, FileSystemLoader ...@@ -4,7 +4,7 @@ from jinja2 import Environment, FileSystemLoader
def generate_file(file_path: str, template_path: str, **kwargs) -> None: 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`. """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 :param file_path: path where to generate the file
:type file_path: str :type file_path: str
...@@ -30,3 +30,17 @@ def generate_file(file_path: str, template_path: str, **kwargs) -> None: ...@@ -30,3 +30,17 @@ def generate_file(file_path: str, template_path: str, **kwargs) -> None:
content = template.render(kwargs) content = template.render(kwargs)
with open(file_path, mode="w", encoding="utf-8") as message: with open(file_path, mode="w", encoding="utf-8") as message:
message.write(content) 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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment