Skip to content

Commit

Permalink
feat(generate_classes.py): allow excluding components (#2447)
Browse files Browse the repository at this point in the history
  • Loading branch information
wpbonelli authored Feb 11, 2025
1 parent 2ba4010 commit 48ec3b2
Showing 1 changed file with 13 additions and 4 deletions.
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

0 comments on commit 48ec3b2

Please sign in to comment.