From ce065d4fbf243ce4f082b0ebc78f3ac19ced4bb2 Mon Sep 17 00:00:00 2001 From: Timothy Willard <9395586+TimothyWillard@users.noreply.github.com> Date: Tue, 29 Oct 2024 08:44:42 -0400 Subject: [PATCH 01/78] Jinja2 templating infrastructure Functionality to render jinja2 templates to make creating batch files easier. Also opens up the door to future use of templates like creating config files. Associated docs+tests included. --- flepimop/gempyor_pkg/src/gempyor/_jinja.py | 114 ++++++++++++++++++ .../src/gempyor/templates/test_template.j2 | 1 + .../tests/_jinja/test__get_template.py | 32 +++++ .../tests/_jinja/test__render_template.py | 19 +++ .../_jinja/test__render_template_to_file.py | 24 ++++ .../test__render_template_to_temp_file.py | 26 ++++ 6 files changed, 216 insertions(+) create mode 100644 flepimop/gempyor_pkg/src/gempyor/_jinja.py create mode 100644 flepimop/gempyor_pkg/src/gempyor/templates/test_template.j2 create mode 100644 flepimop/gempyor_pkg/tests/_jinja/test__get_template.py create mode 100644 flepimop/gempyor_pkg/tests/_jinja/test__render_template.py create mode 100644 flepimop/gempyor_pkg/tests/_jinja/test__render_template_to_file.py create mode 100644 flepimop/gempyor_pkg/tests/_jinja/test__render_template_to_temp_file.py diff --git a/flepimop/gempyor_pkg/src/gempyor/_jinja.py b/flepimop/gempyor_pkg/src/gempyor/_jinja.py new file mode 100644 index 000000000..406c8f2ef --- /dev/null +++ b/flepimop/gempyor_pkg/src/gempyor/_jinja.py @@ -0,0 +1,114 @@ +""" +Internal Jinja2 template rendering utilities. + +This module contains utilities for finding and rendering Jinja2 templates. This module +is tightly coupled to the organization of the package and not intended for external use. +""" + +# Exports +__all__ = [] + + +# Imports +from pathlib import Path +from tempfile import mkstemp +from typing import Any + +from jinja2 import Environment, PackageLoader, Template + + +# Globals +_jinja_environment = Environment(loader=PackageLoader("gempyor", "templates")) + + +# Functions +def _get_template(name: str) -> Template: + """ + Get a jinja template by name. + + Args: + name: The name of the template to pull. + + Returns: + A jinja template object corresponding to `name`. + + Examples: + >>> _get_template("test_template.j2") +