Skip to content

Commit

Permalink
better composite variable docstring format
Browse files Browse the repository at this point in the history
  • Loading branch information
wpbonelli committed Nov 14, 2024
1 parent d9f596b commit 96fc8c5
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 21 deletions.
7 changes: 4 additions & 3 deletions flopy/mf6/utils/codegen/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,10 @@ def type(var: dict) -> str:
children = var.get("children", None)
if children:
if _type == "list":
import pdb; pdb.set_trace()
if len(children) == 1 and (first := list(children.values())[0])["type"] == "record":
return f"{Filters.Var.type(first)}"
if len(children) == 1:
first = list(children.values())[0]
if first["type"] in ["record", "union"]:
return f"[{Filters.Var.type(first)}]"
children = ", ".join(
[v["name"] for v in children.values()]
)
Expand Down
6 changes: 3 additions & 3 deletions flopy/mf6/utils/codegen/templates/exchange.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Modflow{{ title }}(MFPackage):

Parameters
----------
{{ macros.vars_docs(vars, indent=4) }}
{{ macros.vars_docs(vars, indent=4) }}
"""

{% for attr in vars|attrs %}
Expand All @@ -27,7 +27,7 @@ class Modflow{{ title }}(MFPackage):
exgtype="{{ name.r[:3].upper() }}6-{{ name.r[3:].upper() }}6",
exgmnamea=None,
exgmnameb=None,
{{ macros.init_vars(vars, indent=8, skip=name|skip_init) }}
{{ macros.init_vars(vars, indent=8, skip=name|skip_init) }}
filename=None,
pname=None,
**kwargs,
Expand Down Expand Up @@ -64,7 +64,7 @@ class Modflow{{ title }}(MFPackage):
GWE Model with the name exgmnameb must correspond to the GWF Model
with the name gwfmodelname2.

{{ macros.vars_docs(vars, indent=8) }}
{{ macros.vars_docs(vars, indent=8) }}
"""

super().__init__(
Expand Down
10 changes: 5 additions & 5 deletions flopy/mf6/utils/codegen/templates/macros.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@
{% for name, var in vars.items() if name not in skip %}
{% set v = var|untag %}
{% set n = (name if alias else v.name)|safe_name %}
{{ ""|indent(indent, first=false) }}{{ n }}{% if v.default is defined %}={{ v.default|value }}{% endif %},
{{ ""|indent(indent, first=true) }}{{ n }}{% if v.default is defined %}={{ v.default|value }}{% endif %},
{% endfor %}
{% endmacro %}

{% macro vars_docs(vars, indent=0) %}
{% macro vars_docs(vars, indent=0, recurse=true) %}
{% for var in vars.values() recursive %}
{% set v = var|untag %}
{% set n = v.name|safe_name|escape_trailing_underscore %}
{{ ""|indent(indent, first=true) }}{% if loop.depth > 1 %}* {% endif %}{{ n }} : {{ v|type }}
{% if v.description is defined and v.description is not none %}
{{ v.description|wordwrap|indent(indent + (loop.depth * 4), first=true) }}
{% endif %}
{% if v.children is defined and v.children is not none %}
{% if v.type == "list" and v.childen|length == 1 and (v.children.values()|first).type == "record" %}
{% if recurse and v.children is defined and v.children is not none %}
{% if v.type == "list" and v.children|length == 1 and (v.children.values()|first).type == "record" %}
{{ loop((v.children.values()|first).children.values())|indent(indent, first=true) }}
{% else %}
{{ loop(v.children.values())|indent(indent, first=true) }}
{% endif %}
{% endif %}
{% endfor %}
{% endmacro %}
{% endmacro %}
6 changes: 3 additions & 3 deletions flopy/mf6/utils/codegen/templates/model.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Modflow{{ title }}(MFModel):

Parameters
----------
{{ macros.vars_docs(vars, indent=4) }}
{{ macros.vars_docs(vars, indent=4) }}

Methods
-------
Expand All @@ -34,7 +34,7 @@ class Modflow{{ title }}(MFModel):
version="mf6",
exe_name="mf6",
model_rel_path=".",
{{ macros.init_vars(vars, indent=8, skip=name|skip_init) }}
{{ macros.init_vars(vars, indent=8, skip=name|skip_init) }}
**kwargs,
):
"""
Expand All @@ -61,7 +61,7 @@ class Modflow{{ title }}(MFModel):
Simulation that this model is a part of. Model is automatically
added to simulation when it is initialized.

{{ macros.vars_docs(vars, indent=8) }}
{{ macros.vars_docs(vars, indent=8) }}
"""

super().__init__(
Expand Down
8 changes: 4 additions & 4 deletions flopy/mf6/utils/codegen/templates/package.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Modflow{{ title }}(MFPackage):
self,
{{ name|parent }},
loading_package=False,
{{ macros.init_vars(vars, indent=8, skip=name|skip_init) }}
{{ macros.init_vars(vars, indent=8, skip=name|skip_init) }}
filename=None,
pname=None,
**kwargs,
Expand All @@ -43,7 +43,7 @@ class Modflow{{ title }}(MFPackage):
Do not set this parameter. It is intended for debugging and internal
processing purposes only.

{{ macros.vars_docs(vars, indent=8) }}
{{ macros.vars_docs(vars, indent=8) }}

filename : str
File name for this package.
Expand Down Expand Up @@ -94,7 +94,7 @@ class {{ title }}Packages(MFChildPackages):

def initialize(
self,
{{ macros.init_vars(vars, alias=true, indent=8, skip=name|skip_init) }}
{{ macros.init_vars(vars, alias=true, indent=8, skip=name|skip_init) }}
filename=None,
pname=None,
):
Expand All @@ -112,7 +112,7 @@ class {{ title }}Packages(MFChildPackages):
{% if name.r != "obs" %}
def append_package(
self,
{{ macros.init_vars(vars, alias=true, indent=8, skip=name|skip_init) }}
{{ macros.init_vars(vars, alias=true, indent=8, skip=name|skip_init) }}
filename=None,
pname=None,
):
Expand Down
6 changes: 3 additions & 3 deletions flopy/mf6/utils/codegen/templates/simulation.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MF{{ title }}(MFSimulationBase):

Parameters
----------
{{ macros.vars_docs(vars, indent=4) }}
{{ macros.vars_docs(vars, indent=4) }}

Methods
-------
Expand All @@ -35,7 +35,7 @@ class MF{{ title }}(MFSimulationBase):
write_headers: bool = True,
use_pandas: bool = True,
lazy_io: bool = False,
{{ macros.init_vars(vars, indent=8, skip=name|skip_init) }}
{{ macros.init_vars(vars, indent=8, skip=name|skip_init) }}
):
"""
{{ name|description }}
Expand Down Expand Up @@ -66,7 +66,7 @@ class MF{{ title }}(MFSimulationBase):
lazy_io
Whether to use lazy IO

{{ macros.vars_docs(vars, indent=8) }}
{{ macros.vars_docs(vars, indent=8) }}
"""

super().__init__(
Expand Down

0 comments on commit 96fc8c5

Please sign in to comment.