|
2 | 2 | """ |
3 | 3 | General CLI utility functions. |
4 | 4 | """ |
| 5 | +import os |
5 | 6 | import logging |
| 7 | +from copy import deepcopy |
6 | 8 | from warnings import warn |
7 | 9 |
|
8 | 10 | import pandas as pd |
@@ -157,3 +159,41 @@ def compile_descriptions(cols=None): |
157 | 159 | data.append((str(scf), scf.units, scf.description)) |
158 | 160 |
|
159 | 161 | return pd.DataFrame(data, columns=["reV Column", "Units", "Description"]) |
| 162 | + |
| 163 | + |
| 164 | +def add_to_run_attrs(run_attrs=None, config_file=None, project_dir=None, |
| 165 | + module=ModuleName.GENERATION): |
| 166 | + """Add config and project directory to run attrs |
| 167 | +
|
| 168 | + Parameters |
| 169 | + ---------- |
| 170 | + run_attrs : dict, optional |
| 171 | + Existing `run_attrs` (if any). By default, ``None``. |
| 172 | + config_file : str, optional |
| 173 | + Path to config file used for this module's run (if applicable). |
| 174 | + This is used to store the run config in the output attrs. |
| 175 | + By default, ``None``. |
| 176 | + project_dir : _type_, optional |
| 177 | + Path to run directory used for this module's run (if |
| 178 | + applicable). This is used to store information about the run |
| 179 | + in the output attrs. By default, ``None``. |
| 180 | + module : :obj:`~reV.utilities.ModuleName`, optional |
| 181 | + Module that this run represents. |
| 182 | + By default, ``ModuleName.GENERATION``. |
| 183 | +
|
| 184 | + Returns |
| 185 | + ------- |
| 186 | + dict |
| 187 | + Run attributes that can be written to the file's attrs. |
| 188 | + """ |
| 189 | + out = {} |
| 190 | + if run_attrs: |
| 191 | + out = deepcopy(run_attrs) |
| 192 | + |
| 193 | + out["run_directory"] = str(project_dir) |
| 194 | + out[f"{module}_config"] = "{}" |
| 195 | + if config_file and os.path.exists(config_file): |
| 196 | + with open(config_file, "r") as fh: |
| 197 | + out[f"{module}_config"] = fh.read() |
| 198 | + |
| 199 | + return out |
0 commit comments