From c9b08ac292b29c830e6d9142def464fd2f271863 Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Mon, 26 Feb 2024 13:58:27 +0100 Subject: [PATCH] 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. --- python/sdist/amici/_codegen/__init__.py | 0 python/sdist/amici/_codegen/template.py | 42 +++++++++++++++++++++++++ python/sdist/amici/de_export.py | 40 +---------------------- 3 files changed, 43 insertions(+), 39 deletions(-) create mode 100644 python/sdist/amici/_codegen/__init__.py create mode 100644 python/sdist/amici/_codegen/template.py diff --git a/python/sdist/amici/_codegen/__init__.py b/python/sdist/amici/_codegen/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/python/sdist/amici/_codegen/template.py b/python/sdist/amici/_codegen/template.py new file mode 100644 index 0000000000..34f3391ed6 --- /dev/null +++ b/python/sdist/amici/_codegen/template.py @@ -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) diff --git a/python/sdist/amici/de_export.py b/python/sdist/amici/de_export.py index 3f8696a86b..513a7c0e63 100644 --- a/python/sdist/amici/de_export.py +++ b/python/sdist/amici/de_export.py @@ -20,7 +20,6 @@ from dataclasses import dataclass from itertools import chain from pathlib import Path -from string import Template from typing import ( TYPE_CHECKING, Callable, @@ -42,6 +41,7 @@ amiciSwigPath, splines, ) +from ._codegen.template import apply_template from .constants import SymbolId from .cxxcodeprinter import ( AmiciCxxCodePrinter, @@ -3995,44 +3995,6 @@ def set_name(self, model_name: str) -> None: self.model_name = model_name -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) - - def get_function_extern_declaration(fun: str, name: str, ode: bool) -> str: """ Constructs the extern function declaration for a given function