Skip to content

Commit 0f1dad8

Browse files
committed
Break out functionality into utility function
1 parent ea23c9c commit 0f1dad8

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

reV/generation/base.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
OutputWarning,
2929
ParallelExecutionWarning,
3030
)
31+
from reV.utilities.cli_functions import add_to_run_attrs
3132

3233
logger = logging.getLogger(__name__)
3334

@@ -1049,12 +1050,10 @@ def _init_h5(self, mode="w", config_file=None, project_dir=None,
10491050
else:
10501051
ti = None
10511052

1052-
run_attrs = copy.deepcopy(self.run_attrs)
1053-
run_attrs["run_directory"] = str(project_dir)
1054-
run_attrs[f"{module}_config"] = "{}"
1055-
if config_file and os.path.exists(config_file):
1056-
with open(config_file, "r") as fh:
1057-
run_attrs[f"{module}_config"] = fh.read()
1053+
run_attrs = add_to_run_attrs(run_attrs=self.run_attrs,
1054+
config_file=config_file,
1055+
project_dir=project_dir,
1056+
module=ModuleName.GENERATION)
10581057

10591058
Outputs.init_h5(
10601059
self._out_fpath,

reV/utilities/cli_functions.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"""
33
General CLI utility functions.
44
"""
5+
import os
56
import logging
7+
from copy import deepcopy
68
from warnings import warn
79

810
import pandas as pd
@@ -157,3 +159,41 @@ def compile_descriptions(cols=None):
157159
data.append((str(scf), scf.units, scf.description))
158160

159161
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

Comments
 (0)