Skip to content

Commit 50c3426

Browse files
authored
New API: importProjectZipArchive() (#231)
1 parent fa060f9 commit 50c3426

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

src/ansys/sherlock/core/errors.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,3 +960,15 @@ def str_itr(self):
960960

961961
assert self.error_array is None
962962
return [f"Add thermal maps error: {self.message}"]
963+
964+
965+
class SherlockImportProjectZipArchiveError(Exception):
966+
"""Contains the error raised when a .zip project archive cannot be imported."""
967+
968+
def __init__(self, message):
969+
"""Initialize error message."""
970+
self.message = message
971+
972+
def __str__(self):
973+
"""Format error message."""
974+
return f"Import zipped project archive error: {self.message}"

src/ansys/sherlock/core/project.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
SherlockGenerateProjectReportError,
2121
SherlockImportIpc2581Error,
2222
SherlockImportODBError,
23+
SherlockImportProjectZipArchiveError,
2324
SherlockListCCAsError,
2425
SherlockListStrainMapsError,
2526
SherlockListThermalMapsError,
@@ -1423,3 +1424,58 @@ def add_thermal_maps(self, project, add_thermal_map_files):
14231424
for error in e.str_itr():
14241425
LOG.error(error)
14251426
raise e
1427+
1428+
def import_project_zip_archive(self, project, category, archive_file):
1429+
"""
1430+
Import a zipped project archive -- multiple project mode.
1431+
1432+
Parameters
1433+
----------
1434+
project : str
1435+
Name of the Sherlock project.
1436+
category : str
1437+
Sherlock project category.
1438+
archive_file : str
1439+
Full path to the .zip archive file containing the project data.
1440+
Returns
1441+
-------
1442+
int
1443+
Status code of the response. 0 for success.
1444+
Examples
1445+
--------
1446+
>>> from ansys.sherlock.core.launcher import launch_sherlock
1447+
>>> sherlock = launch_sherlock()
1448+
>>> sherlock.project.import_project_zip_archive("Tutorial Project", "Demos",
1449+
"Tutorial Project.zip")
1450+
"""
1451+
try:
1452+
if project == "":
1453+
raise SherlockImportProjectZipArchiveError(message="Project name is invalid.")
1454+
1455+
if category == "":
1456+
raise SherlockImportProjectZipArchiveError(message="Project category is invalid.")
1457+
1458+
if archive_file == "":
1459+
raise SherlockImportProjectZipArchiveError(message="Archive file path is invalid.")
1460+
1461+
if not self._is_connection_up():
1462+
LOG.error("There is no connection to a gRPC service.")
1463+
return
1464+
1465+
request = SherlockProjectService_pb2.ImportProjectZipRequest(
1466+
project=project, category=category, archiveFile=archive_file
1467+
)
1468+
1469+
response = self.stub.importProjectZipArchive(request)
1470+
1471+
return_code = response.returnCode
1472+
1473+
if return_code.value == -1:
1474+
raise SherlockImportProjectZipArchiveError(message=return_code.message)
1475+
1476+
return return_code.value
1477+
1478+
except SherlockImportProjectZipArchiveError as e:
1479+
for error in e.str_itr():
1480+
LOG.error(error)
1481+
raise e

tests/test_project.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
SherlockGenerateProjectReportError,
1818
SherlockImportIpc2581Error,
1919
SherlockImportODBError,
20+
SherlockImportProjectZipArchiveError,
2021
SherlockListCCAsError,
2122
SherlockListStrainMapsError,
2223
SherlockListThermalMapsError,
@@ -2330,6 +2331,34 @@ def helper_test_update_thermal_maps(project):
23302331
pytest.fail(str(e.str_itr()))
23312332

23322333

2334+
def helper_test_import_project_zip_archive(project):
2335+
"""Test import_project_zip_archive API"""
2336+
try:
2337+
project.import_project_zip_archive("", "Demos", "Tutorial Project.zip")
2338+
pytest.fail("No exception raised when using an invalid parameter")
2339+
except SherlockImportProjectZipArchiveError as e:
2340+
assert str(e) == "Import zipped project archive error: Project name is required."
2341+
2342+
try:
2343+
project.import_project_zip_archive("Tutorial Project", "", "Tutorial Project.zip")
2344+
pytest.fail("No exception raised when using an invalid parameter")
2345+
except SherlockImportProjectZipArchiveError as e:
2346+
assert str(e) == "Import zipped project archive error: Project category is required."
2347+
2348+
try:
2349+
project.import_project_zip_archive("Tutorial Project", "Demos", "")
2350+
pytest.fail("No exception raised when using an invalid parameter")
2351+
except SherlockImportProjectZipArchiveError as e:
2352+
assert str(e) == "Import zipped project archive error: Archive file path is required."
2353+
2354+
if project._is_connection_up():
2355+
try:
2356+
project.import_ipc2581_archive("Tutorial Project", "Demos", "Missing Archive File.zip")
2357+
pytest.fail("No exception raised when using an invalid parameter")
2358+
except Exception as e:
2359+
assert type(e) == SherlockImportProjectZipArchiveError
2360+
2361+
23332362
def clean_up_after_add(project, project_name):
23342363
if project_name is not None:
23352364
project.delete_project(project_name)

0 commit comments

Comments
 (0)