99from modflow_devtools .misc import get_model_paths
1010from modflow_devtools .models import BASE_URL
1111
12- REGISTRY_PATH = Path (__file__ ).parent / "registry" / "registry.toml"
12+ REGISTRY_DIR = Path (__file__ ).parent / "registry"
13+ REGISTRY_PATH = REGISTRY_DIR / "registry.toml"
14+ MODELMAP_PATH = REGISTRY_DIR / "models.toml"
15+ EXAMPLES_PATH = REGISTRY_DIR / "examples.toml"
1316
1417
1518def _sha256 (path : Path ) -> str :
@@ -28,21 +31,16 @@ def _sha256(path: Path) -> str:
2831
2932def write_registry (
3033 path : str | PathLike ,
31- registry_path : str | PathLike ,
3234 url : str ,
3335 append : bool = False ,
3436):
3537 path = Path (path ).expanduser ().absolute ()
36- registry_path = Path (registry_path ).expanduser ().absolute ()
37- modelmap_path = registry_path .parent / "models.toml"
38-
3938 if not path .is_dir ():
4039 raise NotADirectoryError (f"Path { path } is not a directory." )
41- if not registry_path .exists ():
42- registry_path .parent .mkdir (parents = True , exist_ok = True )
4340
4441 registry : dict [str , dict [str , str | None ]] = {}
4542 modelmap : dict [str , list [str ]] = {}
43+ examples : dict [str , list [str ]] = {}
4644 exclude = [".DS_Store" , "compare" ]
4745 if is_zip := url .endswith ((".zip" , ".tar" )):
4846 registry [url .rpartition ("/" )[2 ]] = {"hash" : None , "url" : url }
@@ -60,10 +58,13 @@ def _find_examples_dir(p):
6058 # then the model names could correspond directly to directory names.
6159 model_path = model_path .expanduser ().absolute ()
6260 base_path = _find_examples_dir (model_path ) if is_zip else path
63- model_name = (
64- str (model_path .relative_to (base_path )).replace ("/" , "_" ).replace ("-" , "_" )
65- )
61+ rel_path = model_path .relative_to (base_path )
62+ model_name = str (rel_path ).replace ("/" , "_" ).replace ("-" , "_" )
6663 modelmap [model_name ] = []
64+ if is_zip :
65+ if rel_path .parts [0 ] not in examples :
66+ examples [rel_path .parts [0 ]] = []
67+ examples [rel_path .parts [0 ]].append (model_name )
6768 for p in model_path .glob ("*" ):
6869 if not p .is_file () or any (e in p .name for e in exclude ):
6970 continue
@@ -85,15 +86,19 @@ def drop_none_or_empty(path, key, value):
8586 return False
8687 return True
8788
88- with registry_path .open ("ab+" if append else "wb" ) as registry_file :
89+ REGISTRY_DIR .mkdir (parents = True , exist_ok = True )
90+ with REGISTRY_PATH .open ("ab+" if append else "wb" ) as registry_file :
8991 tomli .dump (
9092 remap (dict (sorted (registry .items ())), visit = drop_none_or_empty ),
9193 registry_file ,
9294 )
9395
94- with modelmap_path .open ("ab+" if append else "wb" ) as modelmap_file :
96+ with MODELMAP_PATH .open ("ab+" if append else "wb" ) as modelmap_file :
9597 tomli .dump (dict (sorted (modelmap .items ())), modelmap_file )
9698
99+ with EXAMPLES_PATH .open ("ab+" if append else "wb" ) as examples_file :
100+ tomli .dump (dict (sorted (examples .items ())), examples_file )
101+
97102
98103if __name__ == "__main__" :
99104 parser = argparse .ArgumentParser (description = "Make a registry of example models." )
@@ -113,4 +118,4 @@ def drop_none_or_empty(path, key, value):
113118 args = parser .parse_args ()
114119 path = Path (args .path )
115120 url = args .url if args .url else BASE_URL
116- write_registry (path = path , registry_path = REGISTRY_PATH , url = url , append = args .append )
121+ write_registry (path = path , url = url , append = args .append )
0 commit comments