From 50c3426b36743f7130980e61d8bc2a7f9ad73573 Mon Sep 17 00:00:00 2001 From: Nayane Fernandes <143632290+ansnfernand@users.noreply.github.com> Date: Tue, 27 Feb 2024 09:56:49 +0100 Subject: [PATCH] New API: importProjectZipArchive() (#231) --- src/ansys/sherlock/core/errors.py | 12 +++++++ src/ansys/sherlock/core/project.py | 56 ++++++++++++++++++++++++++++++ tests/test_project.py | 29 ++++++++++++++++ 3 files changed, 97 insertions(+) diff --git a/src/ansys/sherlock/core/errors.py b/src/ansys/sherlock/core/errors.py index aed67956d..f7d527c03 100644 --- a/src/ansys/sherlock/core/errors.py +++ b/src/ansys/sherlock/core/errors.py @@ -960,3 +960,15 @@ def str_itr(self): assert self.error_array is None return [f"Add thermal maps error: {self.message}"] + + +class SherlockImportProjectZipArchiveError(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 f512bbcb3..a9be6d48d 100644 --- a/src/ansys/sherlock/core/project.py +++ b/src/ansys/sherlock/core/project.py @@ -20,6 +20,7 @@ SherlockGenerateProjectReportError, SherlockImportIpc2581Error, SherlockImportODBError, + SherlockImportProjectZipArchiveError, SherlockListCCAsError, SherlockListStrainMapsError, SherlockListThermalMapsError, @@ -1423,3 +1424,58 @@ def add_thermal_maps(self, project, add_thermal_map_files): for error in e.str_itr(): LOG.error(error) raise e + + def import_project_zip_archive(self, project, category, archive_file): + """ + Import a zipped project archive -- multiple 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. + 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("Tutorial Project", "Demos", + "Tutorial Project.zip") + """ + try: + if project == "": + raise SherlockImportProjectZipArchiveError(message="Project name is invalid.") + + if category == "": + raise SherlockImportProjectZipArchiveError(message="Project category is invalid.") + + if archive_file == "": + raise SherlockImportProjectZipArchiveError(message="Archive file path is invalid.") + + if not self._is_connection_up(): + LOG.error("There is no connection to a gRPC service.") + return + + request = SherlockProjectService_pb2.ImportProjectZipRequest( + project=project, category=category, archiveFile=archive_file + ) + + response = self.stub.importProjectZipArchive(request) + + return_code = response.returnCode + + if return_code.value == -1: + raise SherlockImportProjectZipArchiveError(message=return_code.message) + + return return_code.value + + except SherlockImportProjectZipArchiveError as e: + for error in e.str_itr(): + LOG.error(error) + raise e diff --git a/tests/test_project.py b/tests/test_project.py index f6903aa5c..3d07b2640 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -17,6 +17,7 @@ SherlockGenerateProjectReportError, SherlockImportIpc2581Error, SherlockImportODBError, + SherlockImportProjectZipArchiveError, SherlockListCCAsError, SherlockListStrainMapsError, SherlockListThermalMapsError, @@ -2330,6 +2331,34 @@ def helper_test_update_thermal_maps(project): pytest.fail(str(e.str_itr())) +def helper_test_import_project_zip_archive(project): + """Test import_project_zip_archive API""" + try: + 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." + + 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." + + 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." + + if project._is_connection_up(): + try: + project.import_ipc2581_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 clean_up_after_add(project, project_name): if project_name is not None: project.delete_project(project_name)