-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor de_export.py, extract template functions (#2314)
Start moving functionality for amici model code generation to a private subpackage `amici._codegen`. More to follow.
- Loading branch information
Showing
3 changed files
with
43 additions
and
39 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
"""Functions to apply template substitution to files.""" | ||
from pathlib import Path | ||
from string import Template | ||
from typing import Union | ||
|
||
|
||
class TemplateAmici(Template): | ||
""" | ||
Template format used in AMICI (see :class:`string.Template` for more | ||
details). | ||
:cvar delimiter: | ||
delimiter that identifies template variables | ||
""" | ||
|
||
delimiter = "TPL_" | ||
|
||
|
||
def apply_template( | ||
source_file: Union[str, Path], | ||
target_file: Union[str, Path], | ||
template_data: dict[str, str], | ||
) -> None: | ||
""" | ||
Load source file, apply template substitution as provided in | ||
templateData and save as targetFile. | ||
:param source_file: | ||
relative or absolute path to template file | ||
:param target_file: | ||
relative or absolute path to output file | ||
:param template_data: | ||
template keywords to substitute (key is template | ||
variable without :attr:`TemplateAmici.delimiter`) | ||
""" | ||
with open(source_file) as filein: | ||
src = TemplateAmici(filein.read()) | ||
result = src.safe_substitute(template_data) | ||
with open(target_file, "w") as fileout: | ||
fileout.write(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters