diff --git a/flopy/mf6/utils/codegen/shim.py b/flopy/mf6/utils/codegen/shim.py index 4ca953552..5935750b8 100644 --- a/flopy/mf6/utils/codegen/shim.py +++ b/flopy/mf6/utils/codegen/shim.py @@ -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: @@ -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"], @@ -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), ], diff --git a/flopy/mf6/utils/codegen/templates/attrs.py.jinja b/flopy/mf6/utils/codegen/templates/attrs.py.jinja deleted file mode 100644 index 55653dc42..000000000 --- a/flopy/mf6/utils/codegen/templates/attrs.py.jinja +++ /dev/null @@ -1,9 +0,0 @@ - {%- if base == "MFModel" %} - model_type = "{{ name.title }}" - {%- elif base == "MFPackage" %} - {{ class_attrs }} - package_abbr = "{% if name.l != "sln" and name.l != "sim" and name.l != "exg" and name.l is not none %}{{ name.l }}{% endif %}{{ name.r }}" - _package_type = "{{ name.r }}" - dfn_file_name = "{% if name.l is not none %}{{ name.l }}-{% elif name.l is none %}sim-{% endif %}{{ name.r }}.dfn" - dfn = {{ dfn|pprint|indent(10) }} - {% endif -%} \ No newline at end of file diff --git a/flopy/mf6/utils/codegen/templates/container.py.jinja b/flopy/mf6/utils/codegen/templates/container.py.jinja index a61ceb0f9..ac4f7997d 100644 --- a/flopy/mf6/utils/codegen/templates/container.py.jinja +++ b/flopy/mf6/utils/codegen/templates/container.py.jinja @@ -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. @@ -53,4 +53,4 @@ class {{ name.title.title() }}Packages(mfpackage.MFChildPackages): child_builder_call=True, ) self._append_package(new_package, filename) -{% endif %} \ No newline at end of file +{% endif %} diff --git a/flopy/mf6/utils/codegen/templates/context.py.jinja b/flopy/mf6/utils/codegen/templates/context.py.jinja index 75d3271db..34537ea42 100644 --- a/flopy/mf6/utils/codegen/templates/context.py.jinja +++ b/flopy/mf6/utils/codegen/templates/context.py.jinja @@ -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 %} \ No newline at end of file diff --git a/flopy/mf6/utils/codegen/templates/init.py.jinja b/flopy/mf6/utils/codegen/templates/init.py.jinja index b49ea5323..78da10e5e 100644 --- a/flopy/mf6/utils/codegen/templates/init.py.jinja +++ b/flopy/mf6/utils/codegen/templates/init.py.jinja @@ -6,6 +6,4 @@ def __init__( **kwargs, ): {{ init_body }} - {% if base == "MFPackage" %} - self._init_complete = True - {% endif %} \ No newline at end of file + \ No newline at end of file diff --git a/flopy/mf6/utils/codegen/templates/load.py.jinja b/flopy/mf6/utils/codegen/templates/load.py.jinja deleted file mode 100644 index e36e13c64..000000000 --- a/flopy/mf6/utils/codegen/templates/load.py.jinja +++ /dev/null @@ -1,58 +0,0 @@ -{% if base == "MFSimulationBase" %} - @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 mfsimbase.MFSimulationBase.load( - cls, - sim_name, - version, - exe_name, - sim_ws, - strict, - verbosity_level, - load_only, - verify_data, - write_headers, - lazy_io, - use_pandas, - ) -{% elif base == "MFModel" %} - @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.MFModel.load_base( - cls, - simulation, - structure, - modelname, - model_nam_file, - "{{ name.title }}6", - version, - exe_name, - strict, - model_rel_path, - load_only, - ) -{% endif %} \ No newline at end of file diff --git a/flopy/mf6/utils/codegen/templates/model.py.jinja b/flopy/mf6/utils/codegen/templates/model.py.jinja new file mode 100644 index 000000000..ccb9b8ecf --- /dev/null +++ b/flopy/mf6/utils/codegen/templates/model.py.jinja @@ -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, + ) diff --git a/flopy/mf6/utils/codegen/templates/package.py.jinja b/flopy/mf6/utils/codegen/templates/package.py.jinja new file mode 100644 index 000000000..50da2d813 --- /dev/null +++ b/flopy/mf6/utils/codegen/templates/package.py.jinja @@ -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 %} diff --git a/flopy/mf6/utils/codegen/templates/simulation.py.jinja b/flopy/mf6/utils/codegen/templates/simulation.py.jinja new file mode 100644 index 000000000..84437a0c4 --- /dev/null +++ b/flopy/mf6/utils/codegen/templates/simulation.py.jinja @@ -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, + )