Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New API: importProjectZipArchive() #230

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}"
62 changes: 62 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,64 @@ 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 required.")

if category == "":
raise SherlockImportProjectZipArchiveError(message="Project category is required.")

if archive_file == "":
raise SherlockImportProjectZipArchiveError(message="Archive file path is required.")

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.addThermalMaps(request)

return_code = response.returnCode

if return_code.value == -1:
if return_code.message == "":
raise SherlockImportProjectZipArchiveError(response.errors)

raise SherlockAddThermalMapsError(message=return_code.message)

return return_code.value

except SherlockAddThermalMapsError as e:
for error in e.str_itr():
LOG.error(error)
raise e
30 changes: 30 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 @@ -47,6 +48,7 @@ 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_generate_project_report(project)
helper_test_list_ccas(project)
helper_test_add_cca(project)
Expand Down Expand Up @@ -2330,6 +2332,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("Missing Archive File.zip", True, True)
pytest.fail("No exception raised when using an invalid parameter")
except Exception as e:
assert type(e) == SherlockImportIpc2581Error


def clean_up_after_add(project, project_name):
if project_name is not None:
project.delete_project(project_name)
Expand Down
Loading