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 0bccd4a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 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
2 changes: 1 addition & 1 deletion .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
17 changes: 13 additions & 4 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 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
Expand All @@ -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"]
Expand Down

0 comments on commit 0bccd4a

Please sign in to comment.