Skip to content

Commit

Permalink
handle missing jinja
Browse files Browse the repository at this point in the history
  • Loading branch information
wpbonelli committed Oct 29, 2024
1 parent 38353b4 commit 63ed2b4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
2 changes: 0 additions & 2 deletions .docs/md/generate_classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ The FloPy classes for MODFLOW 6 are largely generated by a utility which convert

**Note**: to use this functionality, the `codegen` optional dependency group must be installed.

For instance (output much abbreviated):

```bash
$ python -m flopy.mf6.utils.generate_classes

Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ jobs:
run: |
pip install --upgrade pip
pip install .
pip install ".[test, optional]"
pip install ".[dev]"
- name: Install Modflow executables
uses: modflowpy/install-modflow-action@v1
Expand Down Expand Up @@ -156,7 +156,9 @@ jobs:
powershell
- name: Install FloPy
run: pip install .
run: |
pip install .
pip install ".[codegen]"
- name: Install Modflow-related executables
uses: modflowpy/install-modflow-action@v1
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ jobs:
powershell
- name: Install FloPy
run: pip install .
run: |
pip install .
pip install ".[codegen]"
- name: OpenGL workaround on Linux
if: runner.os == 'Linux'
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/mf6.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
pip install https://github.com/modflowpy/pymake/zipball/master
pip install https://github.com/Deltares/xmipy/zipball/develop
pip install https://github.com/MODFLOW-USGS/modflowapi/zipball/develop
pip install .[test,optional]
pip install .[codegen,test,optional]
pip install meson ninja
- name: Setup GNU Fortran
Expand Down Expand Up @@ -120,7 +120,7 @@ jobs:
pip install https://github.com/modflowpy/pymake/zipball/master
pip install https://github.com/Deltares/xmipy/zipball/develop
pip install https://github.com/MODFLOW-USGS/modflowapi/zipball/develop
pip install .[test,optional]
pip install .[codegen,test,optional]
pip install meson ninja
pip install -r modflow6-examples/etc/requirements.pip.txt
Expand Down
26 changes: 19 additions & 7 deletions flopy/mf6/utils/codegen/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
from pathlib import Path
from warnings import warn

from jinja2 import Environment, PackageLoader

from flopy.mf6.utils.codegen.context import Context
from flopy.mf6.utils.codegen.dfn import Dfn, Dfns, Ref, Refs
from flopy.utils import import_optional_dependency

__all__ = ["make_targets", "make_all"]

_TEMPLATE_LOADER = PackageLoader("flopy", "mf6/utils/codegen/templates/")
_TEMPLATE_ENV = Environment(loader=_TEMPLATE_LOADER)
jinja = import_optional_dependency("jinja2", errors="ignore")
if jinja:
_TEMPLATES_PATH = "mf6/utils/codegen/templates/"
_TEMPLATE_LOADER = jinja.PackageLoader("flopy", _TEMPLATES_PATH)
_TEMPLATE_ENV = jinja.Environment(loader=_TEMPLATE_LOADER)


def make_targets(dfn: Dfn, outdir: Path, verbose: bool = False):
def make_targets(dfn, outdir: Path, verbose: bool = False):
"""Generate Python source file(s) from the given input definition."""

from flopy.mf6.utils.codegen.context import Context

if not jinja:
raise RuntimeError("Jinja2 not installed, can't make targets")

for context in Context.from_dfn(dfn):
name = context.name
target = outdir / name.target
Expand All @@ -27,6 +33,12 @@ def make_targets(dfn: Dfn, outdir: Path, verbose: bool = False):
def make_all(dfndir: Path, outdir: Path, verbose: bool = False):
"""Generate Python source files from the DFN files in the given location."""

from flopy.mf6.utils.codegen.context import Context
from flopy.mf6.utils.codegen.dfn import Dfn, Dfns, Ref, Refs

if not jinja:
raise RuntimeError("Jinja2 not installed, can't make targets")

# find definition files
paths = [
p for p in dfndir.glob("*.dfn") if p.stem not in ["common", "flopy"]
Expand Down

0 comments on commit 63ed2b4

Please sign in to comment.