|
8 | 8 | import stat |
9 | 9 | from functools import lru_cache |
10 | 10 | from pathlib import Path |
| 11 | +from typing import Any |
11 | 12 |
|
12 | 13 | import yaml |
13 | 14 |
|
@@ -122,3 +123,49 @@ def load_intake_config(): |
122 | 123 | def get_intake_config(): |
123 | 124 | """Get the esgf-pyclient configuration.""" |
124 | 125 | return load_intake_config() |
| 126 | + |
| 127 | + |
| 128 | +def _read_facets( |
| 129 | + cfg: dict, |
| 130 | + fhandle: str | None, |
| 131 | + project: str | None = None, |
| 132 | +) -> tuple[dict[str, Any], str]: |
| 133 | + """ |
| 134 | + Extract facet mapping from ESMValCore configuration for a given catalog file handle. |
| 135 | +
|
| 136 | + Recursively traverses the ESMValCore configuration structure to find the |
| 137 | + facet mapping that corresponds to the specified file handle. |
| 138 | +
|
| 139 | + Parameters |
| 140 | + ---------- |
| 141 | + cfg : dict |
| 142 | + The ESMValCore intake configuration dictionary. |
| 143 | + fhandle : str |
| 144 | + The file handle/path of the intake-esm catalog to match. |
| 145 | + project : str, optional |
| 146 | + The current project name in the configuration hierarchy. |
| 147 | +
|
| 148 | + Returns |
| 149 | + ------- |
| 150 | + tuple |
| 151 | + A tuple containing: |
| 152 | + - dict: Facet mapping between ESMValCore facets and catalog columns |
| 153 | + - str: The project name associated with the catalog file |
| 154 | + """ |
| 155 | + if fhandle is None: |
| 156 | + raise ValueError( |
| 157 | + "Unable to ascertain facets without valid file handle." |
| 158 | + ) |
| 159 | + |
| 160 | + for _project, val in cfg.items(): |
| 161 | + if not (isinstance(val, list)): |
| 162 | + return _read_facets(val, fhandle, project or _project) |
| 163 | + for facet_info in val: |
| 164 | + file, facets = facet_info.get("file"), facet_info.get("facets") |
| 165 | + if file == fhandle: |
| 166 | + return facets, project # type: ignore[return-value] |
| 167 | + else: |
| 168 | + raise ValueError( |
| 169 | + f"No facets found for {fhandle} in the config file. " |
| 170 | + "Please check the config file and ensure it is valid." |
| 171 | + ) |
0 commit comments