From 743b26e01231eecd75bebb56f2588b7cf0122999 Mon Sep 17 00:00:00 2001 From: Donald Campbell <125581724+donaldcampbelljr@users.noreply.github.com> Date: Mon, 3 Jun 2024 14:33:54 -0400 Subject: [PATCH] enable carve out for initializing pipestat with "{record_identifier" --- pipestat/backends/file_backend/filebackend.py | 23 +++++++++------- pipestat/pipestat.py | 26 ++++++++++++++----- tests/test_pipestat.py | 4 +-- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/pipestat/backends/file_backend/filebackend.py b/pipestat/backends/file_backend/filebackend.py index 278fc5a6..8a22de50 100644 --- a/pipestat/backends/file_backend/filebackend.py +++ b/pipestat/backends/file_backend/filebackend.py @@ -66,21 +66,26 @@ def __init__( self.result_formatter = result_formatter self.multi_pipelines = multi_pipelines - self.determine_results_file(self.results_file_path) + self.determine_results_file() - def determine_results_file(self, results_file_path: str) -> None: + def determine_results_file(self) -> None: """Initialize or load results_file from given path :param str results_file_path: YAML file to report into, if file is used as the object back-end """ - if not os.path.exists(self.results_file_path): - _LOGGER.debug( - f"Results file doesn't yet exist. Initializing: {self.results_file_path}" - ) - self._init_results_file() + + if "{record_identifier}" in self.results_file_path: + # In the special case where the user wants to use {record_identifier} in file path + pass else: - _LOGGER.debug(f"Loading results file: {self.results_file_path}") - self._load_results_file() + if not os.path.exists(self.results_file_path): + _LOGGER.debug( + f"Results file doesn't yet exist. Initializing: {self.results_file_path}" + ) + self._init_results_file() + else: + _LOGGER.debug(f"Loading results file: {self.results_file_path}") + self._load_results_file() def check_record_exists( self, diff --git a/pipestat/pipestat.py b/pipestat/pipestat.py index cdc5b264..a0dce5ba 100644 --- a/pipestat/pipestat.py +++ b/pipestat/pipestat.py @@ -229,7 +229,12 @@ def __init__( ), self.cfg["config_path"], ) - make_subdirectories(self.cfg[FILE_KEY]) + + if "{record_identifier}" in str(self.cfg[FILE_KEY]): + # In the special case where the user wants to use {record_identifier} in file path + pass + else: + make_subdirectories(self.cfg[FILE_KEY]) self.cfg[RESULT_FORMATTER] = result_formatter @@ -330,12 +335,19 @@ def resolve_results_file_path(self, results_file_path): # Save for later when assessing if there may be multiple result files if results_file_path: assert isinstance(results_file_path, str), TypeError("Path is expected to be a str") - if not self.record_identifier and "{record_identifier}" in results_file_path: - raise NotImplementedError( - f"Must provide record identifier during PipestatManager creation for this results_file_path: {results_file_path}" - ) - self.cfg["unresolved_result_path"] = results_file_path - return results_file_path.format(record_identifier=self.record_identifier) + if self.record_identifier: + try: + self.cfg["unresolved_result_path"] = results_file_path + results_file_path = results_file_path.format( + record_identifier=self.record_identifier + ) + return results_file_path + except AttributeError: + self.cfg["unresolved_result_path"] = results_file_path + return results_file_path + else: + self.cfg["unresolved_result_path"] = results_file_path + return results_file_path return results_file_path def initialize_filebackend(self, record_identifier, results_file_path, flag_file_dir): diff --git a/tests/test_pipestat.py b/tests/test_pipestat.py index dd211663..0deb25dc 100644 --- a/tests/test_pipestat.py +++ b/tests/test_pipestat.py @@ -2245,8 +2245,8 @@ def test_multi_results_not_implemented( backend_data = {"results_file_path": results_file_path} args.update(backend_data) - with pytest.raises(NotImplementedError): - psm = SamplePipestatManager(**args) + # with pytest.raises(NotImplementedError): + psm = SamplePipestatManager(**args) @pytest.mark.parametrize("backend", ["file"]) def test_multi_results_basic(