diff --git a/.docs/md/generate_classes.md b/.docs/md/generate_classes.md index 3e91086fa..e7b302577 100644 --- a/.docs/md/generate_classes.md +++ b/.docs/md/generate_classes.md @@ -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 diff --git a/.github/workflows/commit.yml b/.github/workflows/commit.yml index 64eab8078..bbb48f375 100644 --- a/.github/workflows/commit.yml +++ b/.github/workflows/commit.yml @@ -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 @@ -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 diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml index 04ccd791f..37ae54630 100644 --- a/.github/workflows/examples.yml +++ b/.github/workflows/examples.yml @@ -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' diff --git a/.github/workflows/mf6.yml b/.github/workflows/mf6.yml index 72011cdfc..62c1d8ed3 100644 --- a/.github/workflows/mf6.yml +++ b/.github/workflows/mf6.yml @@ -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 @@ -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 diff --git a/flopy/mf6/utils/codegen/__init__.py b/flopy/mf6/utils/codegen/__init__.py index 68466b91e..78ba16e23 100644 --- a/flopy/mf6/utils/codegen/__init__.py +++ b/flopy/mf6/utils/codegen/__init__.py @@ -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 @@ -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"]