Skip to content

Commit

Permalink
New API: importProjectZipArchive() (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
ansnfernand authored Feb 27, 2024
1 parent fa060f9 commit 50c3426
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/ansys/sherlock/core/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
56 changes: 56 additions & 0 deletions src/ansys/sherlock/core/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
SherlockGenerateProjectReportError,
SherlockImportIpc2581Error,
SherlockImportODBError,
SherlockImportProjectZipArchiveError,
SherlockListCCAsError,
SherlockListStrainMapsError,
SherlockListThermalMapsError,
Expand Down Expand Up @@ -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
29 changes: 29 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
SherlockGenerateProjectReportError,
SherlockImportIpc2581Error,
SherlockImportODBError,
SherlockImportProjectZipArchiveError,
SherlockListCCAsError,
SherlockListStrainMapsError,
SherlockListThermalMapsError,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 50c3426

Please sign in to comment.