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..a51fdc9f6 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 diff --git a/flopy/mf6/utils/codegen/__init__.py b/flopy/mf6/utils/codegen/__init__.py index 68466b91e..0cebbeb98 100644 --- a/flopy/mf6/utils/codegen/__init__.py +++ b/flopy/mf6/utils/codegen/__init__.py @@ -1,19 +1,25 @@ from pathlib import Path - -from jinja2 import Environment, PackageLoader +from warnings import warn 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): """Generate Python source file(s) from the given input definition.""" + if not jinja: + warn("Jinja2 not installed, can't make targets") + for context in Context.from_dfn(dfn): name = context.name target = outdir / name.target @@ -27,6 +33,9 @@ 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.""" + if not jinja: + warn("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"]