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

Jinja code generation function raise error if args not provided.

parent 2a3e810f
No related branches found
No related tags found
No related merge requests found
from pathlib import Path from pathlib import Path
from jinja2 import Environment, FileSystemLoader from jinja2 import Environment, FileSystemLoader, StrictUndefined
from typing import Union from typing import Union
...@@ -21,13 +21,9 @@ def generate_file(file_path: Union[Path, str], template_path: Union[Path, str], ...@@ -21,13 +21,9 @@ def generate_file(file_path: Union[Path, str], template_path: Union[Path, str],
# Make dir # Make dir
file_path.parent.mkdir(parents=True, exist_ok=True) file_path.parent.mkdir(parents=True, exist_ok=True)
# Select template
template = Environment(loader=FileSystemLoader(
template_path.parent)).get_template(template_path.name)
# Generate file # Generate file
with open(file_path, mode="w", encoding="utf-8") as file: with open(file_path, mode="w", encoding="utf-8") as file:
file.write(template.render(kwargs)) file.write(generate_str(template_path, **kwargs))
def generate_str(template_path: Union[Path, str], **kwargs) -> str: def generate_str(template_path: Union[Path, str], **kwargs) -> str:
...@@ -43,4 +39,4 @@ def generate_str(template_path: Union[Path, str], **kwargs) -> str: ...@@ -43,4 +39,4 @@ def generate_str(template_path: Union[Path, str], **kwargs) -> str:
if isinstance(template_path, str): if isinstance(template_path, str):
template_path = Path(template_path) template_path = Path(template_path)
return Environment(loader=FileSystemLoader( return Environment(loader=FileSystemLoader(
template_path.parent)).get_template(template_path.name).render(kwargs) template_path.parent), undefined=StrictUndefined).get_template(template_path.name).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