Skip to content

Commit 4819c28

Browse files
authored
New API: importProjectZipArchiveSingleMode() (#233)
1 parent 50c3426 commit 4819c28

File tree

3 files changed

+145
-11
lines changed

3 files changed

+145
-11
lines changed

src/ansys/sherlock/core/errors.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,3 +972,15 @@ def __init__(self, message):
972972
def __str__(self):
973973
"""Format error message."""
974974
return f"Import zipped project archive error: {self.message}"
975+
976+
977+
class SherlockImportProjectZipArchiveSingleModeError(Exception):
978+
"""Contains the error raised when a .zip project archive cannot be imported."""
979+
980+
def __init__(self, message):
981+
"""Initialize error message."""
982+
self.message = message
983+
984+
def __str__(self):
985+
"""Format error message."""
986+
return f"Import zipped project archive error: {self.message}"

src/ansys/sherlock/core/project.py

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
SherlockImportIpc2581Error,
2222
SherlockImportODBError,
2323
SherlockImportProjectZipArchiveError,
24+
SherlockImportProjectZipArchiveSingleModeError,
2425
SherlockListCCAsError,
2526
SherlockListStrainMapsError,
2627
SherlockListThermalMapsError,
@@ -1468,14 +1469,86 @@ def import_project_zip_archive(self, project, category, archive_file):
14681469

14691470
response = self.stub.importProjectZipArchive(request)
14701471

1471-
return_code = response.returnCode
1472+
if response.value == -1:
1473+
raise SherlockImportProjectZipArchiveError(message=response.message)
14721474

1473-
if return_code.value == -1:
1474-
raise SherlockImportProjectZipArchiveError(message=return_code.message)
1475+
except SherlockImportProjectZipArchiveError as e:
1476+
LOG.error(str(e))
1477+
raise e
14751478

1476-
return return_code.value
1479+
return response.value
14771480

1478-
except SherlockImportProjectZipArchiveError as e:
1479-
for error in e.str_itr():
1480-
LOG.error(error)
1481+
def import_project_zip_archive_single_mode(
1482+
self, project, category, archive_file, destination_file_directory
1483+
):
1484+
"""
1485+
Import a zipped project archive -- single project mode.
1486+
1487+
Parameters
1488+
----------
1489+
project : str
1490+
Name of the Sherlock project.
1491+
category : str
1492+
Sherlock project category.
1493+
archive_file : str
1494+
Full path to the .zip archive file containing the project data.
1495+
destination_file_directory : str
1496+
Directory in which the Sherlock project folder will be created.
1497+
Returns
1498+
-------
1499+
int
1500+
Status code of the response. 0 for success.
1501+
Examples
1502+
--------
1503+
>>> from ansys.sherlock.core.launcher import launch_sherlock
1504+
>>> sherlock = launch_sherlock()
1505+
>>> sherlock.project.import_project_zip_archive_single_mode("Tutorial Project",
1506+
"Demos",
1507+
"Tutorial Project.zip",
1508+
"New Tutorial Project")
1509+
"""
1510+
try:
1511+
if project == "":
1512+
raise SherlockImportProjectZipArchiveSingleModeError(
1513+
message="Project name is invalid."
1514+
)
1515+
1516+
if category == "":
1517+
raise SherlockImportProjectZipArchiveSingleModeError(
1518+
message="Project category is invalid."
1519+
)
1520+
1521+
if archive_file == "":
1522+
raise SherlockImportProjectZipArchiveSingleModeError(
1523+
message="Archive file path is invalid."
1524+
)
1525+
1526+
if destination_file_directory == "":
1527+
raise SherlockImportProjectZipArchiveSingleModeError(
1528+
message="Directory of the destination file is invalid."
1529+
)
1530+
1531+
if not self._is_connection_up():
1532+
LOG.error("There is no connection to a gRPC service.")
1533+
return
1534+
1535+
request = SherlockProjectService_pb2.ImportProjectZipSingleModeRequest(
1536+
destFileDir=destination_file_directory
1537+
)
1538+
1539+
# Add the other properties to the request
1540+
projZipProperty = request.projZipRequest
1541+
projZipProperty.project = project
1542+
projZipProperty.category = category
1543+
projZipProperty.archiveFile = archive_file
1544+
1545+
response = self.stub.importProjectZipArchiveSingleMode(request)
1546+
1547+
if response.value == -1:
1548+
raise SherlockImportProjectZipArchiveSingleModeError(message=response.message)
1549+
1550+
except SherlockImportProjectZipArchiveSingleModeError as e:
1551+
LOG.error(str(e))
14811552
raise e
1553+
1554+
return response.value

tests/test_project.py

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
SherlockImportIpc2581Error,
1919
SherlockImportODBError,
2020
SherlockImportProjectZipArchiveError,
21+
SherlockImportProjectZipArchiveSingleModeError,
2122
SherlockListCCAsError,
2223
SherlockListStrainMapsError,
2324
SherlockListThermalMapsError,
@@ -48,6 +49,8 @@ def test_all():
4849
helper_test_delete_project(project)
4950
helper_test_import_odb_archive(project)
5051
helper_test_import_ipc2581_archive(project)
52+
helper_test_import_project_zip_archive(project)
53+
helper_test_import_project_zip_archive_single_mode(project)
5154
helper_test_generate_project_report(project)
5255
helper_test_list_ccas(project)
5356
helper_test_add_cca(project)
@@ -2337,28 +2340,74 @@ def helper_test_import_project_zip_archive(project):
23372340
project.import_project_zip_archive("", "Demos", "Tutorial Project.zip")
23382341
pytest.fail("No exception raised when using an invalid parameter")
23392342
except SherlockImportProjectZipArchiveError as e:
2340-
assert str(e) == "Import zipped project archive error: Project name is required."
2343+
assert str(e) == "Import zipped project archive error: Project name is invalid."
23412344

23422345
try:
23432346
project.import_project_zip_archive("Tutorial Project", "", "Tutorial Project.zip")
23442347
pytest.fail("No exception raised when using an invalid parameter")
23452348
except SherlockImportProjectZipArchiveError as e:
2346-
assert str(e) == "Import zipped project archive error: Project category is required."
2349+
assert str(e) == "Import zipped project archive error: Project category is invalid."
23472350

23482351
try:
23492352
project.import_project_zip_archive("Tutorial Project", "Demos", "")
23502353
pytest.fail("No exception raised when using an invalid parameter")
23512354
except SherlockImportProjectZipArchiveError as e:
2352-
assert str(e) == "Import zipped project archive error: Archive file path is required."
2355+
assert str(e) == "Import zipped project archive error: Archive file path is invalid."
23532356

23542357
if project._is_connection_up():
23552358
try:
2356-
project.import_ipc2581_archive("Tutorial Project", "Demos", "Missing Archive File.zip")
2359+
project.import_project_zip_archive(
2360+
"Tutorial Project", "Demos", "Missing Archive File.zip"
2361+
)
23572362
pytest.fail("No exception raised when using an invalid parameter")
23582363
except Exception as e:
23592364
assert type(e) == SherlockImportProjectZipArchiveError
23602365

23612366

2367+
def helper_test_import_project_zip_archive_single_mode(project):
2368+
"""Test import_project_zip_archive_single_mode API"""
2369+
try:
2370+
project.import_project_zip_archive_single_mode(
2371+
"", "Demos", "Tutorial Project.zip", "New Tutorial Project"
2372+
)
2373+
pytest.fail("No exception raised when using an invalid parameter")
2374+
except SherlockImportProjectZipArchiveSingleModeError as e:
2375+
assert str(e) == "Import zipped project archive error: Project name is invalid."
2376+
2377+
try:
2378+
project.import_project_zip_archive_single_mode(
2379+
"Tutorial Project", "", "Tutorial Project.zip", "New Tutorial Project"
2380+
)
2381+
pytest.fail("No exception raised when using an invalid parameter")
2382+
except SherlockImportProjectZipArchiveSingleModeError as e:
2383+
assert str(e) == "Import zipped project archive error: Project category is invalid."
2384+
2385+
try:
2386+
project.import_project_zip_archive_single_mode(
2387+
"Tutorial Project", "Demos", "", "New Tutorial Project"
2388+
)
2389+
pytest.fail("No exception raised when using an invalid parameter")
2390+
except SherlockImportProjectZipArchiveSingleModeError as e:
2391+
assert str(e) == "Import zipped project archive error: Archive file path is invalid."
2392+
2393+
try:
2394+
project.import_project_zip_archive_single_mode("Tutorial Project", "Demos", "File.zip", "")
2395+
pytest.fail("No exception raised when using an invalid parameter")
2396+
except SherlockImportProjectZipArchiveSingleModeError as e:
2397+
assert str(e) == (
2398+
"Import zipped project archive error: Directory of the destination file is invalid."
2399+
)
2400+
2401+
if project._is_connection_up():
2402+
try:
2403+
project.import_project_zip_archive_single_mode(
2404+
"Tutorial Project", "Demos", "Missing Archive File.zip", "New Tutorial Project"
2405+
)
2406+
pytest.fail("No exception raised when using an invalid parameter")
2407+
except Exception as e:
2408+
assert type(e) == SherlockImportProjectZipArchiveSingleModeError
2409+
2410+
23622411
def clean_up_after_add(project, project_name):
23632412
if project_name is not None:
23642413
project.delete_project(project_name)

0 commit comments

Comments
 (0)