From 4e7df49536624c254cb19ef61264d9a92fb6427f Mon Sep 17 00:00:00 2001 From: Leonardo Schwarz Date: Mon, 12 Aug 2024 11:00:16 +0200 Subject: [PATCH] refactor --- .../job_export_results.py | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/depiction_targeted_preprocbatch/job_export_results.py b/src/depiction_targeted_preprocbatch/job_export_results.py index 8345f38..a66f1c9 100644 --- a/src/depiction_targeted_preprocbatch/job_export_results.py +++ b/src/depiction_targeted_preprocbatch/job_export_results.py @@ -14,10 +14,13 @@ class JobExportResults: """Exports the results of the job to the output storage and registers it in B-Fabric.""" - def __init__(self, client: Bfabric, work_dir: Path, workunit_config: WorkunitConfig) -> None: + def __init__( + self, client: Bfabric, work_dir: Path, workunit_config: WorkunitConfig, output_storage: Storage + ) -> None: self._client = client self._workunit_config = workunit_config self.output_dir = work_dir / "output" + self._output_storage = output_storage @classmethod def export( @@ -30,16 +33,17 @@ def export( output_storage: Storage, ) -> None: """Exports the results of one job.""" - instance = cls(client=client, work_dir=work_dir, workunit_config=workunit_config) - instance.export_results(sample_name, result_files, output_storage) + instance = cls(client=client, work_dir=work_dir, workunit_config=workunit_config, output_storage=output_storage) + instance.export_results(sample_name, result_files) - def export_results(self, sample_name: str, result_files: list[Path], output_storage: Storage) -> None: + def export_results(self, sample_name: str, result_files: list[Path]) -> None: """Exports the results of one job.""" zip_file_path = self._create_zip_file(result_files, sample_name) - output_path_relative = self._copy_zip_to_storage(zip_file_path, output_storage) - self._register_zip_in_workunit(output_path_relative, output_storage, zip_file_path) + output_path_relative = self._copy_zip_to_storage(zip_file_path) + self._register_zip_in_workunit(output_path_relative, zip_file_path) - def _create_zip_file(self, result_files, sample_name): + def _create_zip_file(self, result_files: list[Path], sample_name: str) -> Path: + """Creates a ZIP file containing the results for one sample, and returns the zip file's path.""" self.output_dir.mkdir(exist_ok=True, parents=True) zip_file_path = self.output_dir / f"{sample_name}.zip" with zipfile.ZipFile(zip_file_path, "w") as zip_file: @@ -47,14 +51,14 @@ def _create_zip_file(self, result_files, sample_name): zip_file.write(result_file, arcname=Path(sample_name) / result_file.name) return zip_file_path - def _register_zip_in_workunit(self, output_path_relative, output_storage, zip_file_path): + def _register_zip_in_workunit(self, output_path_relative: Path, zip_file_path: Path) -> None: checksum = FileChecksums(file_path=zip_file_path).checksum_md5 self._client.save( "resource", { "name": zip_file_path.name, "workunitid": self._workunit_config.workunit_id, - "storageid": output_storage.id, + "storageid": self._output_storage.id, "relativepath": output_path_relative, "filechecksum": checksum, "status": "available", @@ -62,9 +66,9 @@ def _register_zip_in_workunit(self, output_path_relative, output_storage, zip_fi }, ) - def _copy_zip_to_storage(self, zip_file_path, output_storage): + def _copy_zip_to_storage(self, zip_file_path: Path) -> Path: output_path = self._workunit_config.output_folder_absolute_path / zip_file_path.name - output_path_relative = output_path.relative_to(output_storage.base_path) - output_uri = f"{output_storage.scp_prefix}{output_path_relative}" + output_path_relative = output_path.relative_to(self._output_storage.base_path) + output_uri = f"{self._output_storage.scp_prefix}{output_path_relative}" scp(zip_file_path, output_uri) return output_path_relative