Skip to content

Commit

Permalink
whitespace mgmt
Browse files Browse the repository at this point in the history
  • Loading branch information
wpbonelli committed Nov 14, 2024
1 parent 53188ca commit d9f596b
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 31 deletions.
8 changes: 7 additions & 1 deletion flopy/mf6/utils/codegen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ def _get_template_env():
# expect optional deps at module init time
jinja = import_optional_dependency("jinja2")
loader = jinja.PackageLoader("flopy", "mf6/utils/codegen/templates/")
env = jinja.Environment(loader=loader)
env = jinja.Environment(
loader=loader,
trim_blocks=True,
lstrip_blocks=True,
line_statement_prefix="_",
keep_trailing_newline=True,
)

from flopy.mf6.utils.codegen.filters import Filters

Expand Down
2 changes: 1 addition & 1 deletion flopy/mf6/utils/codegen/dfn.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ def _fields() -> Vars:
block=block,
children=None,
description=(
f"* Contains data for the {ref['abbr']} package. Data can be "
f"Contains data for the {ref['abbr']} package. Data can be "
f"stored in a dictionary containing data for the {ref['abbr']} "
"package with variable names as keys and package data as "
f"values. Data just for the {ref['val']} variable is also "
Expand Down
9 changes: 4 additions & 5 deletions flopy/mf6/utils/codegen/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,16 @@ def type(var: dict) -> str:
"""
Get a readable representation of the variable's type.
TODO: eventually replace this with a proper `type` in
the variable spec when we add type hints. For now try
to match the existing format, with a few tweaks; e.g.
distinguishing lists from records by square and round
brackets, respectively, and separating each choice in
a keystring by '|'.
the variable spec when we add type hints
"""
_type = var["type"]
shape = var.get("shape", None)
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)}"
children = ", ".join(
[v["name"] for v in children.values()]
)
Expand Down
4 changes: 2 additions & 2 deletions flopy/mf6/utils/codegen/templates/exchange.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Modflow{{ title }}(MFPackage):

{% for attr in vars|attrs %}
{{ attr }}
{%- endfor %}
{% endfor %}

def __init__(
self,
Expand Down Expand Up @@ -83,6 +83,6 @@ class Modflow{{ title }}(MFPackage):

{% for statement in vars|init %}
{{ statement }}
{%- endfor %}
{% endfor %}

self._init_complete = True
30 changes: 17 additions & 13 deletions flopy/mf6/utils/codegen/templates/macros.jinja
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
{% macro init_vars(vars, alias=false, indent=0, skip=none) %}
{%- 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=true) }}{{ n }}{%- if v.default is defined %}={{ v.default|value }}{%- endif -%},
{%- endfor %}
{% 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 %},
{% endfor %}
{% endmacro %}

{% macro vars_docs(vars, indent=0) %}
{%- for var in vars.values() recursive %}
{% set v = var|untag -%}
{% set n = v.name|safe_name|escape_trailing_underscore -%}
{% 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 %}
{% 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 -%}
{% 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" %}
{{ loop((v.children.values()|first).children.values())|indent(indent, first=true) }}
{% else %}
{{ loop(v.children.values())|indent(indent, first=true) }}
{%- endif %}
{% endfor -%}
{% endif %}
{% endif %}
{% endfor %}
{% endmacro %}
2 changes: 1 addition & 1 deletion flopy/mf6/utils/codegen/templates/model.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Modflow{{ title }}(MFModel):

{% for statement in vars|init %}
{{ statement }}
{%- endfor %}
{% endfor %}

@classmethod
def load(
Expand Down
14 changes: 7 additions & 7 deletions flopy/mf6/utils/codegen/templates/package.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ class Modflow{{ title }}(MFPackage):

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

{% for attr in vars|attrs %}
{{ attr }}
{%- endfor %}
{% endfor %}

def __init__(
self,
Expand Down Expand Up @@ -68,7 +68,7 @@ class Modflow{{ title }}(MFPackage):

{% for statement in vars|init %}
{{ statement }}
{%- endfor %}
{% endfor %}

self._init_complete = True

Expand Down Expand Up @@ -100,9 +100,9 @@ class {{ title }}Packages(MFChildPackages):
):
new_package = Modflow{{ title }}(
self._cpparent,
{%- for n, var in vars.items() if n not in name|skip_init %}
{% for n, var in vars.items() if n not in name|skip_init %}
{{ n|safe_name }}={{ n|safe_name }},
{%- endfor %}
{% endfor %}
filename=filename,
pname=pname,
child_builder_call=True,
Expand All @@ -118,9 +118,9 @@ class {{ title }}Packages(MFChildPackages):
):
new_package = Modflow{{ title }}(
self._cpparent,
{%- for n, var in vars.items() if n not in name|skip_init %}
{% for n, var in vars.items() if n not in name|skip_init %}
{{ n|safe_name }}={{ n|safe_name }},
{%- endfor %}
{% endfor %}
filename=filename,
pname=pname,
child_builder_call=True,
Expand Down
2 changes: 1 addition & 1 deletion flopy/mf6/utils/codegen/templates/simulation.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class MF{{ title }}(MFSimulationBase):

{% for statement in vars|init %}
{{ statement }}
{%- endfor %}
{% endfor %}

@classmethod
def load(
Expand Down

0 comments on commit d9f596b

Please sign in to comment.