diff --git a/src/ansys/sherlock/core/errors.py b/src/ansys/sherlock/core/errors.py index f7d527c03..976459eab 100644 --- a/src/ansys/sherlock/core/errors.py +++ b/src/ansys/sherlock/core/errors.py @@ -972,3 +972,15 @@ def __init__(self, message): def __str__(self): """Format error message.""" return f"Import zipped project archive error: {self.message}" + + +class SherlockImportProjectZipArchiveSingleModeError(Exception): + """Contains the error raised when a .zip project archive cannot be imported.""" + + def __init__(self, message): + """Initialize error message.""" + self.message = message + + def __str__(self): + """Format error message.""" + return f"Import zipped project archive error: {self.message}" diff --git a/src/ansys/sherlock/core/project.py b/src/ansys/sherlock/core/project.py index a9be6d48d..a6d29b367 100644 --- a/src/ansys/sherlock/core/project.py +++ b/src/ansys/sherlock/core/project.py @@ -21,6 +21,7 @@ SherlockImportIpc2581Error, SherlockImportODBError, SherlockImportProjectZipArchiveError, + SherlockImportProjectZipArchiveSingleModeError, SherlockListCCAsError, SherlockListStrainMapsError, SherlockListThermalMapsError, @@ -1468,14 +1469,86 @@ def import_project_zip_archive(self, project, category, archive_file): response = self.stub.importProjectZipArchive(request) - return_code = response.returnCode + if response.value == -1: + raise SherlockImportProjectZipArchiveError(message=response.message) - if return_code.value == -1: - raise SherlockImportProjectZipArchiveError(message=return_code.message) + except SherlockImportProjectZipArchiveError as e: + LOG.error(str(e)) + raise e - return return_code.value + return response.value - except SherlockImportProjectZipArchiveError as e: - for error in e.str_itr(): - LOG.error(error) + def import_project_zip_archive_single_mode( + self, project, category, archive_file, destination_file_directory + ): + """ + Import a zipped project archive -- single project mode. + + Parameters + ---------- + project : str + Name of the Sherlock project. + category : str + Sherlock project category. + archive_file : str + Full path to the .zip archive file containing the project data. + destination_file_directory : str + Directory in which the Sherlock project folder will be created. + Returns + ------- + int + Status code of the response. 0 for success. + Examples + -------- + >>> from ansys.sherlock.core.launcher import launch_sherlock + >>> sherlock = launch_sherlock() + >>> sherlock.project.import_project_zip_archive_single_mode("Tutorial Project", + "Demos", + "Tutorial Project.zip", + "New Tutorial Project") + """ + try: + if project == "": + raise SherlockImportProjectZipArchiveSingleModeError( + message="Project name is invalid." + ) + + if category == "": + raise SherlockImportProjectZipArchiveSingleModeError( + message="Project category is invalid." + ) + + if archive_file == "": + raise SherlockImportProjectZipArchiveSingleModeError( + message="Archive file path is invalid." + ) + + if destination_file_directory == "": + raise SherlockImportProjectZipArchiveSingleModeError( + message="Directory of the destination file is invalid." + ) + + if not self._is_connection_up(): + LOG.error("There is no connection to a gRPC service.") + return + + request = SherlockProjectService_pb2.ImportProjectZipSingleModeRequest( + destFileDir=destination_file_directory + ) + + # Add the other properties to the request + projZipProperty = request.projZipRequest + projZipProperty.project = project + projZipProperty.category = category + projZipProperty.archiveFile = archive_file + + response = self.stub.importProjectZipArchiveSingleMode(request) + + if response.value == -1: + raise SherlockImportProjectZipArchiveSingleModeError(message=response.message) + + except SherlockImportProjectZipArchiveSingleModeError as e: + LOG.error(str(e)) raise e + + return response.value diff --git a/tests/test_project.py b/tests/test_project.py index 3d07b2640..374bb56f1 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -18,6 +18,7 @@ SherlockImportIpc2581Error, SherlockImportODBError, SherlockImportProjectZipArchiveError, + SherlockImportProjectZipArchiveSingleModeError, SherlockListCCAsError, SherlockListStrainMapsError, SherlockListThermalMapsError, @@ -48,6 +49,8 @@ def test_all(): helper_test_delete_project(project) helper_test_import_odb_archive(project) helper_test_import_ipc2581_archive(project) + helper_test_import_project_zip_archive(project) + helper_test_import_project_zip_archive_single_mode(project) helper_test_generate_project_report(project) helper_test_list_ccas(project) helper_test_add_cca(project) @@ -2337,28 +2340,74 @@ def helper_test_import_project_zip_archive(project): project.import_project_zip_archive("", "Demos", "Tutorial Project.zip") pytest.fail("No exception raised when using an invalid parameter") except SherlockImportProjectZipArchiveError as e: - assert str(e) == "Import zipped project archive error: Project name is required." + assert str(e) == "Import zipped project archive error: Project name is invalid." try: project.import_project_zip_archive("Tutorial Project", "", "Tutorial Project.zip") pytest.fail("No exception raised when using an invalid parameter") except SherlockImportProjectZipArchiveError as e: - assert str(e) == "Import zipped project archive error: Project category is required." + assert str(e) == "Import zipped project archive error: Project category is invalid." try: project.import_project_zip_archive("Tutorial Project", "Demos", "") pytest.fail("No exception raised when using an invalid parameter") except SherlockImportProjectZipArchiveError as e: - assert str(e) == "Import zipped project archive error: Archive file path is required." + assert str(e) == "Import zipped project archive error: Archive file path is invalid." if project._is_connection_up(): try: - project.import_ipc2581_archive("Tutorial Project", "Demos", "Missing Archive File.zip") + project.import_project_zip_archive( + "Tutorial Project", "Demos", "Missing Archive File.zip" + ) pytest.fail("No exception raised when using an invalid parameter") except Exception as e: assert type(e) == SherlockImportProjectZipArchiveError +def helper_test_import_project_zip_archive_single_mode(project): + """Test import_project_zip_archive_single_mode API""" + try: + project.import_project_zip_archive_single_mode( + "", "Demos", "Tutorial Project.zip", "New Tutorial Project" + ) + pytest.fail("No exception raised when using an invalid parameter") + except SherlockImportProjectZipArchiveSingleModeError as e: + assert str(e) == "Import zipped project archive error: Project name is invalid." + + try: + project.import_project_zip_archive_single_mode( + "Tutorial Project", "", "Tutorial Project.zip", "New Tutorial Project" + ) + pytest.fail("No exception raised when using an invalid parameter") + except SherlockImportProjectZipArchiveSingleModeError as e: + assert str(e) == "Import zipped project archive error: Project category is invalid." + + try: + project.import_project_zip_archive_single_mode( + "Tutorial Project", "Demos", "", "New Tutorial Project" + ) + pytest.fail("No exception raised when using an invalid parameter") + except SherlockImportProjectZipArchiveSingleModeError as e: + assert str(e) == "Import zipped project archive error: Archive file path is invalid." + + try: + project.import_project_zip_archive_single_mode("Tutorial Project", "Demos", "File.zip", "") + pytest.fail("No exception raised when using an invalid parameter") + except SherlockImportProjectZipArchiveSingleModeError as e: + assert str(e) == ( + "Import zipped project archive error: Directory of the destination file is invalid." + ) + + if project._is_connection_up(): + try: + project.import_project_zip_archive_single_mode( + "Tutorial Project", "Demos", "Missing Archive File.zip", "New Tutorial Project" + ) + pytest.fail("No exception raised when using an invalid parameter") + except Exception as e: + assert type(e) == SherlockImportProjectZipArchiveSingleModeError + + def clean_up_after_add(project, project_name): if project_name is not None: project.delete_project(project_name)