Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(generate_classes.py): allow excluding components #2447

Merged
merged 1 commit into from
Feb 11, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions flopy/mf6/utils/generate_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
flopypth = os.path.join(thisfilepath, "..", "..")
flopypth = os.path.abspath(flopypth)
protected_dfns = ["flopy.dfn"]

default_owner = "MODFLOW-USGS"
default_repo = "modflow6"

Expand Down Expand Up @@ -88,14 +87,17 @@ def backup_existing_dfns(flopy_dfn_path):
), f"dfn backup files not found: {backup_folder}"


def replace_dfn_files(new_dfn_pth, flopy_dfn_path):
def replace_dfn_files(new_dfn_pth, flopy_dfn_path, exclude):
# remove the old files, unless the file is protected
filenames = os.listdir(flopy_dfn_path)
delete_files(filenames, flopy_dfn_path, exclude=protected_dfns)

# copy the new ones into the folder
filenames = os.listdir(new_dfn_pth)
for filename in filenames:
if exclude and any(pattern in filename for pattern in exclude):
print(f" excluding..{filename}")
continue
filename_w_path = os.path.join(new_dfn_pth, filename)
print(f" copying..{filename}")
shutil.copy(filename_w_path, flopy_dfn_path)
Expand All @@ -118,6 +120,7 @@ def generate_classes(
ref="master",
dfnpath=None,
backup=True,
exclude=None
):
"""
Generate the MODFLOW 6 flopy classes using definition files from the
Expand Down Expand Up @@ -147,7 +150,8 @@ def generate_classes(
backup : bool, default True
Keep a backup of the definition files in dfn_backup with a date and
timestamp from when the definition files were replaced.

exclude : list of str, optional, default None
Patterns for excluding DFN files and corresponding components.
"""

# print header
Expand Down Expand Up @@ -187,7 +191,7 @@ def generate_classes(
print(" Using pre-existing definition files")
else:
print(" Replacing existing definition files")
replace_dfn_files(new_dfn_pth, flopy_dfn_path)
replace_dfn_files(new_dfn_pth, flopy_dfn_path, exclude)
if dfnpath is None:
shutil.rmtree(new_dfn_pth)

Expand Down Expand Up @@ -230,6 +234,11 @@ def cli_main():
help="Path to a definition file folder that will be used to generate "
"the MODFLOW 6 classes.",
)
parser.add_argument(
"--exclude",
help="Exclude DFNs matching a pattern.",
action="append"
)
parser.add_argument(
"--no-backup",
action="store_true",
Expand Down
Loading