Skip to content

Commit

Permalink
Merge GH-365/emcee-submit into test/willard
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyWillard committed Nov 25, 2024
2 parents 05b760b + 2ecab37 commit 5062606
Show file tree
Hide file tree
Showing 47 changed files with 3,821 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,6 @@ flepimop/gempyor_pkg/.coverage
!flepimop/gempyor_pkg/tests/inference/model_output/
flepimop/gempyor_pkg/tests/inference/model_output/*
!flepimop/gempyor_pkg/tests/inference/model_output/minimal_test_Scenario1_None

# info/ directory
info/**/*.json
2 changes: 2 additions & 0 deletions flepimop/gempyor_pkg/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ dependencies = [
"emcee",
"graphviz",
"h5py",
"Jinja2",
"matplotlib",
"numba>=0.53.1",
"numpy",
"pandas",
"pyarrow",
"pydantic",
"scipy",
"seaborn",
"sympy",
Expand Down
114 changes: 114 additions & 0 deletions flepimop/gempyor_pkg/src/gempyor/_jinja.py
Original file line number Diff line number Diff line change
@@ -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")
<Template 'test_template.j2'>
"""
return _jinja_environment.get_template(name)


def _render_template(name: str, data: dict[str, Any]) -> str:
"""
Render a jinja template by name.
Args:
name: The name of the template to pull.
data: The data to pass to the template when rendering.
Returns:
The rendered template as a string.
Examples:
>>> _render_template("test_template.j2", {"name": "Bob"})
'Hello Bob!'
"""
return _get_template(name).render(data)


def _render_template_to_file(name: str, data: dict[str, Any], file: Path) -> None:
"""
Render a jinja template and save to a file.
Args:
name: The name of the template to pull.
data: The data to pass to the template when rendering.
file: The file to save the rendered template to.
Examples:
>>> from pathlib import Path
>>> file = Path("hi.txt")
>>> _render_template_to_file("test_template.j2", {"name": "Jane"}, file)
>>> file.read_text()
'Hello Jane!'
"""
with file.open(mode="w", encoding="utf-8") as f:
f.write(_render_template(name, data))


def _render_template_to_temp_file(
name: str,
data: dict[str, Any],
suffix: str | None = None,
prefix: str | None = None,
) -> Path:
"""
Render a jinja template and save to a temporary file.
Args:
name: The name of the template to pull.
data: The data to pass to the template when rendering.
suffix: The suffix of the temporary file, such as an extension. Passed on to
`tempfile.mkstemp`.
prefix: The prefix of the temporary file. Passed on to `tempfile.mkstemp`.
Returns:
The file containing the rendered template as a `Path` object.
See Also:
[`tempfile.mkstemp`](https://docs.python.org/3/library/tempfile.html#tempfile.mkstemp)
Examples:
>>> file = _render_template_to_temp_file(
... "test_template.j2", {"name": "John"}, suffix=".txt", prefix="foo_"
... )
>>> file
PosixPath('/var/folders/2z/h3pc0p7s3ng1tvxrgsw5kr680000gp/T/foo_ocaomg4k.txt')
>>> file.read_text()
'Hello John!'
"""
_, tmp = mkstemp(suffix=suffix, prefix=prefix, text=True)
tmp = Path(tmp)
_render_template_to_file(name, data, tmp)
return tmp
Loading

0 comments on commit 5062606

Please sign in to comment.