Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
wpbonelli committed Feb 1, 2024
1 parent 613ad01 commit 81887bd
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,5 @@ app
# in case developer installs modflow executables in the project root
bin

**.DS_Store
**.DS_Store
data_backup
20 changes: 20 additions & 0 deletions autotest/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import modflow_devtools.models as examples
from modflow_devtools.markers import requires_github


@requires_github
def test_mf2005_freyberg():
files = examples.mf2005_freyberg()
assert any(files)


@requires_github
def test_mf6_freyberg():
files = examples.mf6_freyberg()
assert any(files)


@requires_github
def test_mfusg_freyberg():
files = examples.mfusg_freyberg()
assert any(files)
Binary file added data/mf2005_freyberg.zip
Binary file not shown.
Binary file added data/mf6_freyberg.zip
Binary file not shown.
Binary file added data/mfusg_freyberg.zip
Binary file not shown.
3 changes: 3 additions & 0 deletions docs/md/models.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Models API

The `modflow_devtools.models` module provides programmatic access to a number of MODFLOW 6 (and other) example models.
79 changes: 79 additions & 0 deletions modflow_devtools/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import hashlib
from pathlib import Path

import pkg_resources

from modflow_devtools.imports import import_optional_dependency

pooch = import_optional_dependency("pooch")


def _sha256(filename) -> str:
"""
Compute the SHA256 hash of the given file.
Reference: https://stackoverflow.com/a/44873382/6514033
"""
h = hashlib.sha256()
b = bytearray(128 * 1024)
mv = memoryview(b)
with open(filename, "rb", buffering=0) as f:
for n in iter(lambda: f.readinto(mv), 0):
h.update(mv[:n])
return h.hexdigest()


PROJ_ROOT = Path(__file__).parents[1]
DATA_PATH = PROJ_ROOT / "data"
OWNER = "wpbonelli"
PACKAGE = "modflow-devtools"
VERSION = pkg_resources.get_distribution(PACKAGE).version.rpartition(".dev")[0]
EXCLUDED = [
".DS_Store",
]
REGISTRY = {
p.name: _sha256(p)
for p in DATA_PATH.rglob("*")
if p.is_file() and not any(e in p.name for e in EXCLUDED)
}
FETCHER = pooch.create(
# Folder where the data will be stored. For a sensible default, use the
# default cache folder for your OS.
path=pooch.os_cache(PACKAGE),
# Base URL of the remote data store. Will call .format on this string
# to insert the version (see below).
base_url="https://github.com/"
+ OWNER
+ "/"
+ PACKAGE
+ "/raw/models-api/data/", # todo: replace branch (debugging)
# Pooches are versioned so that you can use multiple versions of a
# package simultaneously. Use PEP440 compliant version number. The
# version will be appended to the path.
version=VERSION,
# The cache file registry. A dictionary with all files managed by this
# pooch. Keys are the file names (relative to *base_url*) and values
# are their respective SHA256 hashes. Files will be downloaded
# automatically when needed (see fetch_gravity_data).
registry=REGISTRY,
)


def mf2005_freyberg():
"""
Load the MF2005 Freyberg example model files.
"""
return FETCHER.fetch("mf2005_freyberg.zip", processor=pooch.Unzip())


def mf6_freyberg():
"""
Load the MF6 Freyberg example model files.
"""
return FETCHER.fetch("mf6_freyberg.zip", processor=pooch.Unzip())


def mfusg_freyberg():
"""
Load the MFUSG Freyberg example model files.
"""
return FETCHER.fetch("mfusg_freyberg.zip", processor=pooch.Unzip())
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ test = [
"meson!=0.63.0",
"ninja",
"numpy",
"pandas",
"pooch",
"pytest",
"pytest-cov",
"pytest-dotenv",
Expand Down
7 changes: 2 additions & 5 deletions scripts/update_version.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import argparse
import textwrap
from datetime import datetime
from enum import Enum
from os import PathLike
from pathlib import Path
from typing import NamedTuple

from filelock import FileLock
from packaging.version import Version
Expand Down Expand Up @@ -37,7 +34,7 @@ def update_init_py(timestamp: datetime, version: Version):
print(f"Updated {_package_init_path} to version {version}")


def update_docs_config(timestamp: datetime, version: Version):
def update_docs_config(version: Version):
lines = _docs_config_path.read_text().rstrip().split("\n")
with open(_docs_config_path, "w") as f:
for line in lines:
Expand All @@ -64,7 +61,7 @@ def update_version(
with lock:
update_version_txt(version)
update_init_py(timestamp, version)
update_docs_config(timestamp, version)
update_docs_config(version)
finally:
try:
lock_path.unlink()
Expand Down

0 comments on commit 81887bd

Please sign in to comment.