Skip to content

Commit

Permalink
Moving code from shim to jinja
Browse files Browse the repository at this point in the history
Removed qual_base
Removed load and attrs, moved it to model, package and simulation.
  • Loading branch information
deltamarnix authored and wpbonelli committed Oct 24, 2024
1 parent 7d4c1a2 commit 21a55b7
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 107 deletions.
14 changes: 1 addition & 13 deletions flopy/mf6/utils/codegen/shim.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ def _attr(var: dict) -> Optional[str]:
return None

attrs = [_attr(v) for v in ctx["vars"].values()]
return "\n ".join([a for a in attrs if a])
return [a for a in attrs if a]


def _init_body(ctx: dict) -> str:
Expand Down Expand Up @@ -695,17 +695,6 @@ def _var(var: dict) -> List[str]:
return [["header"] + _meta()] + _dfn()


def _qual_base(ctx: dict):
base = ctx["base"]
if base == "MFSimulationBase":
module = "mfsimbase"
elif base == "MFModel":
module = "mfmodel"
else:
module = "mfpackage"
return f"{module}.{base}"


SHIM = {
"keep_none": ["default", "block", "metadata"],
"quote_str": ["default"],
Expand All @@ -714,7 +703,6 @@ def _qual_base(ctx: dict):
_is_context,
[
("dfn", _dfn),
("qual_base", _qual_base),
("class_attrs", _class_attrs),
("init_body", _init_body),
],
Expand Down
9 changes: 0 additions & 9 deletions flopy/mf6/utils/codegen/templates/attrs.py.jinja

This file was deleted.

4 changes: 2 additions & 2 deletions flopy/mf6/utils/codegen/templates/container.py.jinja
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class {{ name.title.title() }}Packages(mfpackage.MFChildPackages):
class {{ name.title.title() }}Packages(MFChildPackages):
"""
{{ name.title.title() }}Packages is a container class for the Modflow{{ name.title.title() }} class.

Expand Down Expand Up @@ -53,4 +53,4 @@ class {{ name.title.title() }}Packages(mfpackage.MFChildPackages):
child_builder_call=True,
)
self._append_package(new_package, filename)
{% endif %}
{% endif %}
27 changes: 5 additions & 22 deletions flopy/mf6/utils/codegen/templates/context.py.jinja
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
# autogenerated file, do not modify
from os import PathLike, curdir
from typing import Union

from flopy.mf6.data.mfdatautil import ArrayTemplateGenerator, ListTemplateGenerator
from flopy.mf6 import mfpackage
from flopy.mf6 import mfmodel
{# avoid circular import; some pkgs (e.g. mfnam) are used by mfsimbase.py #}
{% if base == "MFSimulationBase" %}
from flopy.mf6 import mfsimbase
{% include "simulation.py.jinja" %}
{% elif base == "MFModel" %}
{% include "model.py.jinja" %}
{% else %}
{% include "package.py.jinja" %}
{% endif %}

class {% if base == "MFSimulationBase" %}MF{% else %}Modflow{% endif %}{{ name.title.title() }}({{ qual_base }}):
{% include "docstring.jinja" %}

{% include "attrs.py.jinja" %}

{% include "init.py.jinja" %}

{% include "load.py.jinja" %}

{# TODO: cleaner way to filter out hpc subpkg? #}
{% if "ref" in meta and name.r != "hpc" %}
{% include "container.py.jinja" %}
{% endif %}
4 changes: 1 addition & 3 deletions flopy/mf6/utils/codegen/templates/init.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ def __init__(
**kwargs,
):
{{ init_body }}
{% if base == "MFPackage" %}
self._init_complete = True
{% endif %}

58 changes: 0 additions & 58 deletions flopy/mf6/utils/codegen/templates/load.py.jinja

This file was deleted.

40 changes: 40 additions & 0 deletions flopy/mf6/utils/codegen/templates/model.py.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from os import PathLike, curdir
from typing import Union

from flopy.mf6.data.mfdatautil import ArrayTemplateGenerator, ListTemplateGenerator
from flopy.mf6.mfmodel import MFModel


class Modflow{{ name.title.title() }}(MFModel):
{% include "docstring.jinja" %}

model_type = "{{ name.title }}"

{% include "init.py.jinja" %}

@classmethod
def load(
cls,
simulation,
structure,
modelname="NewModel",
model_nam_file="modflowtest.nam",
version="mf6",
exe_name="mf6",
strict=True,
model_rel_path=curdir,
load_only=None,
):
return MFModel.load_base(
cls,
simulation,
structure,
modelname,
model_nam_file,
"{{ name.title }}6",
version,
exe_name,
strict,
model_rel_path,
load_only,
)
24 changes: 24 additions & 0 deletions flopy/mf6/utils/codegen/templates/package.py.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from os import PathLike, curdir
from typing import Union

from flopy.mf6.data.mfdatautil import ArrayTemplateGenerator, ListTemplateGenerator
from flopy.mf6.mfpackage import MFPackage, MFChildPackages

class Modflow{{ name.title.title() }}(MFPackage):
{% include "docstring.jinja" %}

{%- for item in class_attrs %}
{{ item }}
{%- endfor %}
package_abbr = "{{ '' if name.l in ["sln", "sim", "exg", none] else name.l }}{{ name.r }}"
_package_type = "{{ name.r }}"
dfn_file_name = "{{ name.l | default('sim', true) }}-{{ name.r }}.dfn"
dfn = {{ dfn|pprint|indent(10) }}

{% include "init.py.jinja" %}
self._init_complete = True

{# TODO: cleaner way to filter out hpc subpkg? #}
{% if "ref" in meta and name.r != "hpc" %}
{% include "container.py.jinja" %}
{% endif %}
40 changes: 40 additions & 0 deletions flopy/mf6/utils/codegen/templates/simulation.py.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from os import PathLike, curdir
from typing import Union

from flopy.mf6.data.mfdatautil import ArrayTemplateGenerator, ListTemplateGenerator
from flopy.mf6.mfsimbase import MFSimulationBase

class MF{{ name.title.title() }}(MFSimulationBase):
{% include "docstring.jinja" %}

{% include "init.py.jinja" %}

@classmethod
def load(
cls,
sim_name="modflowsim",
version="mf6",
exe_name: Union[str, PathLike] = "mf6",
sim_ws: Union[str, PathLike] = curdir,
strict=True,
verbosity_level=1,
load_only=None,
verify_data=False,
write_headers=True,
lazy_io=False,
use_pandas=True,
):
return MFSimulationBase.load(
cls,
sim_name,
version,
exe_name,
sim_ws,
strict,
verbosity_level,
load_only,
verify_data,
write_headers,
lazy_io,
use_pandas,
)

0 comments on commit 21a55b7

Please sign in to comment.