From 44f5db44988aa55db86f94741bc39aeb42e880e9 Mon Sep 17 00:00:00 2001 From: wpbonelli Date: Thu, 31 Oct 2024 12:48:49 -0400 Subject: [PATCH] add params macro --- .../utils/codegen/templates/exchange.py.jinja | 5 ++--- .../mf6/utils/codegen/templates/macros.jinja | 6 ++++++ .../utils/codegen/templates/model.py.jinja | 5 ++--- .../utils/codegen/templates/package.py.jinja | 21 ++++++------------- .../codegen/templates/simulation.py.jinja | 5 ++--- 5 files changed, 18 insertions(+), 24 deletions(-) diff --git a/flopy/mf6/utils/codegen/templates/exchange.py.jinja b/flopy/mf6/utils/codegen/templates/exchange.py.jinja index a5d4a1e98..4b76026d5 100644 --- a/flopy/mf6/utils/codegen/templates/exchange.py.jinja +++ b/flopy/mf6/utils/codegen/templates/exchange.py.jinja @@ -1,4 +1,5 @@ {% import 'macros.jinja' as macros %} +# autogenerated file, do not modify from os import PathLike, curdir from typing import Union @@ -26,9 +27,7 @@ class Modflow{{ name.title.title() }}(MFPackage): exgtype="{{ name.r[:3].upper() }}6-{{ name.r[3:].upper() }}6", exgmnamea=None, exgmnameb=None, - {%- for n, var in vars.items() if n not in init_skip %} - {{ var.name }}{%- if var.default is defined %}={{ var.default }}{%- endif -%}, - {%- endfor %} + {{ macros.vars_params(vars, init_skip, start_indent=8) }} filename=None, pname=None, **kwargs, diff --git a/flopy/mf6/utils/codegen/templates/macros.jinja b/flopy/mf6/utils/codegen/templates/macros.jinja index be5b1ab1a..fe5ceb3fb 100644 --- a/flopy/mf6/utils/codegen/templates/macros.jinja +++ b/flopy/mf6/utils/codegen/templates/macros.jinja @@ -1,3 +1,9 @@ +{% macro vars_params(vars, init_skip=none, start_indent=0) %} +{%- for n, var in vars.items() if n not in init_skip %} +{{ ""|indent(start_indent, first=true) }}{{ var.name }}{%- if var.default is defined %}={{ var.default }}{% endif -%}, +{%- endfor %} +{% endmacro %} + {% macro vars_docs(vars, start_indent=0) %} {%- for v in vars.values() recursive %} {{ ""|indent(start_indent, first=true) }}{% if loop.depth > 1 %}* {% endif %}{% if v.name|last == "_" %}{{ v.name.replace("_", "\\\_") }}{% else %}{{ v.name }}{% endif %}{% if v.meta is defined and v.meta.type is defined %} : {{ v.meta.type }}{% endif %} diff --git a/flopy/mf6/utils/codegen/templates/model.py.jinja b/flopy/mf6/utils/codegen/templates/model.py.jinja index a922bad9e..3754124af 100644 --- a/flopy/mf6/utils/codegen/templates/model.py.jinja +++ b/flopy/mf6/utils/codegen/templates/model.py.jinja @@ -1,4 +1,5 @@ {% import 'macros.jinja' as macros %} +# autogenerated file, do not modify from os import PathLike, curdir from typing import Union @@ -33,9 +34,7 @@ class Modflow{{ name.title.title() }}(MFModel): version="mf6", exe_name="mf6", model_rel_path=".", - {%- for n, var in vars.items() if n not in init_skip %} - {{ var.name }}{%- if var.default is defined %}={{ var.default }}{%- endif -%}, - {%- endfor %} + {{ macros.vars_params(vars, init_skip, start_indent=8) }} **kwargs, ): """ diff --git a/flopy/mf6/utils/codegen/templates/package.py.jinja b/flopy/mf6/utils/codegen/templates/package.py.jinja index 576ba79a7..e3d2375a2 100644 --- a/flopy/mf6/utils/codegen/templates/package.py.jinja +++ b/flopy/mf6/utils/codegen/templates/package.py.jinja @@ -1,4 +1,5 @@ {% import 'macros.jinja' as macros %} +# autogenerated file, do not modify from os import PathLike, curdir from typing import Union @@ -24,9 +25,7 @@ class Modflow{{ name.title.title() }}(MFPackage): self, {{ parent }}, loading_package=False, - {%- for n, var in vars.items() if n not in init_skip %} - {{ var.name }}{%- if var.default is defined %}={{ var.default }}{%- endif -%}, - {%- endfor %} + {{ macros.vars_params(vars, init_skip, start_indent=8) }} filename=None, pname=None, **kwargs, @@ -95,17 +94,13 @@ class {{ name.title.title() }}Packages(MFChildPackages): def initialize( self, - {%- for n, var in vars.items() if n not in init_skip %} - {{ n }}{%- if var.default is defined %}={{ var.default }}{% endif -%}, - {%- endfor %} + {{ macros.vars_params(vars, init_skip, start_indent=8) }} filename=None, pname=None, ): new_package = Modflow{{ name.title.title() }}( self._cpparent, - {%- for n, var in vars.items() if n not in init_skip %} - {{ n }}={{ n }}, - {%- endfor %} + {{ macros.vars_params(vars, init_skip, start_indent=12) }} filename=filename, pname=pname, child_builder_call=True, @@ -115,17 +110,13 @@ class {{ name.title.title() }}Packages(MFChildPackages): {% if name.r != "obs" %} def append_package( self, - {%- for n, var in vars.items() if n not in init_skip %} - {{ n }}{%- if var.default is defined %}={{ var.default }}{% endif -%}, - {%- endfor %} + {{ macros.vars_params(vars, init_skip, start_indent=8) }} filename=None, pname=None, ): new_package = Modflow{{ name.title.title() }}( self._cpparent, - {%- for n, var in vars.items() if n not in init_skip %} - {{ n }}={{ n }}, - {%- endfor %} + {{ macros.vars_params(vars, init_skip, start_indent=12) }} filename=filename, pname=pname, child_builder_call=True, diff --git a/flopy/mf6/utils/codegen/templates/simulation.py.jinja b/flopy/mf6/utils/codegen/templates/simulation.py.jinja index 39f807574..eb4b98f18 100644 --- a/flopy/mf6/utils/codegen/templates/simulation.py.jinja +++ b/flopy/mf6/utils/codegen/templates/simulation.py.jinja @@ -1,4 +1,5 @@ {% import 'macros.jinja' as macros %} +# autogenerated file, do not modify from os import PathLike, curdir from typing import Union @@ -34,9 +35,7 @@ class MF{{ name.title.title() }}(MFSimulationBase): write_headers: bool = True, use_pandas: bool = True, lazy_io: bool = False, - {%- for n, var in vars.items() if n not in init_skip %} - {{ var.name }}{%- if var.default is defined %}={{ var.default }}{%- endif -%}, - {%- endfor %} + {{ macros.vars_params(vars, init_skip, start_indent=8) }} ): """ {{ name.description }}