From affbe69fafc9c8af68b09bb54d1455ac508915dd Mon Sep 17 00:00:00 2001 From: Keith Hanson <122566160+anskhanson@users.noreply.github.com> Date: Thu, 16 May 2024 10:28:20 -0400 Subject: [PATCH 1/3] Added export project method and unit tests --- src/ansys/sherlock/core/errors.py | 12 ++++ src/ansys/sherlock/core/project.py | 96 ++++++++++++++++++++++++++++++ tests/test_project.py | 72 ++++++++++++++++++++++ 3 files changed, 180 insertions(+) diff --git a/src/ansys/sherlock/core/errors.py b/src/ansys/sherlock/core/errors.py index 9f285e6da..0f0058b0c 100644 --- a/src/ansys/sherlock/core/errors.py +++ b/src/ansys/sherlock/core/errors.py @@ -1025,3 +1025,15 @@ def __init__(self, message): def __str__(self): """Format error message.""" return f"Export net list error: {self.message}" + + +class SherlockExportProjectException(Exception): + """Contains the error raised when a project cannot be exported.""" + + def __init__(self, message): + """Initialize error message.""" + self.message = message + + def __str__(self): + """Format error message.""" + return f"Export Project error : {self.message}" diff --git a/src/ansys/sherlock/core/project.py b/src/ansys/sherlock/core/project.py index c5564d99a..ae3c942fe 100644 --- a/src/ansys/sherlock/core/project.py +++ b/src/ansys/sherlock/core/project.py @@ -17,6 +17,7 @@ SherlockAddStrainMapsError, SherlockAddThermalMapsError, SherlockDeleteProjectError, + SherlockExportProjectException, SherlockGenerateProjectReportError, SherlockImportIpc2581Error, SherlockImportODBError, @@ -1693,3 +1694,98 @@ def import_project_zip_archive_single_mode( raise e return response.value + + def export_project( + self, + project_name, + export_design_files, + export_result_files, + export_archive_results, + export_user_files, + export_log_files, + export_system_data, + export_file_dir, + export_file_name, + overwrite_existing_file, + ): + """ + Export a sherlock project. + + Parameters + ---------- + project_name:str + Name of the project being exported. + export_design_files: bool + Determines if design files should be exported. + export_result_files: bool + Determines if all analysis module result files should be exported. + export_archive_results: bool + Determines if all archive result files should be exported. + export_user_files: bool + Determines if user properties and custom data files should be exported. + export_log_files: bool + Determines if Sherlock console and application log files should be exported. + export_system_data: bool + Determines if system technical data should be exported. + export_file_dir: str + Destination of export file. + export_file_name: str + Name to be given to the exported file. + overwrite_existing_file: bool + Determines if exported file will overwrite a previously existing file. + Returns + ------- + int + Status code of the response. 0 for success. + Examples + -------- + >>> from ansys.sherlock.core.launcher import launch_sherlock + >>> sherlock = launch_sherlock() + >>> sherlock.project.export_project("Tutorial Project", + True, + True, + True, + True, + True, + True, + "C:/Path/To/Exported/Project", + "Exported_Project", + True) + """ + try: + if project_name == "": + raise SherlockExportProjectException(message="Project name is invalid") + + if export_file_dir == "": + raise SherlockExportProjectException(message="Export directory is invalid") + + if export_file_name == "": + raise SherlockExportProjectException(message="Export file name is invalid") + + if not self._is_connection_up(): + LOG.error("There is no connection to a gRPC service.") + return + + request = SherlockProjectService_pb2.ExportProjectRequest( + project=project_name, + exportDesignFiles=export_design_files, + exportResultFiles=export_result_files, + exportArchivedResults=export_archive_results, + exportUserFiles=export_user_files, + exportLogFiles=export_log_files, + exportSystemData=export_system_data, + exportFileDirectory=export_file_dir, + exportFileName=export_file_name, + overwriteExistingFile=overwrite_existing_file, + ) + + response = self.stub.exportProject(request) + + if response.value == -1: + raise SherlockExportProjectException(message=response.message) + + except SherlockExportProjectException as e: + LOG.error(str(e)) + raise e + + return response.value diff --git a/tests/test_project.py b/tests/test_project.py index 31e66d504..836bb84de 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -14,6 +14,7 @@ SherlockAddStrainMapsError, SherlockAddThermalMapsError, SherlockDeleteProjectError, + SherlockExportProjectException, SherlockGenerateProjectReportError, SherlockImportIpc2581Error, SherlockImportODBError, @@ -66,6 +67,8 @@ def test_all(): finally: clean_up_after_add(project, project_name) + helper_test_export_project(project) + def helper_test_delete_project(project): """Test delete_project API""" @@ -2969,5 +2972,74 @@ def clean_up_after_add(project, project_name): project.delete_project(project_name) +def helper_test_export_project(project): + """Test method for export project""" + try: + result = project.export_project( + project_name="", + export_design_files=True, + export_result_files=True, + export_archive_results=True, + export_user_files=True, + export_log_files=True, + export_system_data=True, + export_file_dir="", + export_file_name="", + overwrite_existing_file=True, + ) + except SherlockExportProjectException as e: + assert str(e) == "Export Project error : Project name is invalid" + + try: + result = project.export_project( + project_name="Tutorial Project", + export_design_files=True, + export_result_files=True, + export_archive_results=True, + export_user_files=True, + export_log_files=True, + export_system_data=True, + export_file_dir="", + export_file_name="", + overwrite_existing_file=True, + ) + except SherlockExportProjectException as e: + assert str(e) == "Export Project error : Export directory is invalid" + + try: + result = project.export_project( + project_name="Tutorial Project", + export_design_files=True, + export_result_files=True, + export_archive_results=True, + export_user_files=True, + export_log_files=True, + export_system_data=True, + export_file_dir="/Test/Dir", + export_file_name="", + overwrite_existing_file=True, + ) + except SherlockExportProjectException as e: + assert str(e) == "Export Project error : Export file name is invalid" + + if project._is_connection_up(): + try: + result = project.export_project( + project_name="", + export_design_files=True, + export_result_files=True, + export_archive_results=True, + export_user_files=True, + export_log_files=True, + export_system_data=True, + export_file_dir="/Test/Dir", + export_file_name="ExportedProject", + overwrite_existing_file=True, + ) + pytest.fail("No exception raised when using an invalid parameter") + except Exception as e: + assert type(e) == SherlockExportProjectException + + if __name__ == "__main__": test_all() From d738e1875eb6f2698d947ad4fc773127657ecf94 Mon Sep 17 00:00:00 2001 From: Keith Hanson <122566160+anskhanson@users.noreply.github.com> Date: Thu, 16 May 2024 13:45:14 -0400 Subject: [PATCH 2/3] Added version requirement --- src/ansys/sherlock/core/project.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ansys/sherlock/core/project.py b/src/ansys/sherlock/core/project.py index ae3c942fe..bc95de9cf 100644 --- a/src/ansys/sherlock/core/project.py +++ b/src/ansys/sherlock/core/project.py @@ -1711,6 +1711,8 @@ def export_project( """ Export a sherlock project. + Requires Sherlock Version: 25R1 + Parameters ---------- project_name:str From 271e40f8b8b20c6a428934ea79ef1b31a5daa83c Mon Sep 17 00:00:00 2001 From: Keith Hanson <122566160+anskhanson@users.noreply.github.com> Date: Tue, 21 May 2024 12:51:46 -0400 Subject: [PATCH 3/3] PR Feedback: Updated error name. Updated test to test happy path and delete file created. Updated documentation. --- src/ansys/sherlock/core/errors.py | 4 +-- src/ansys/sherlock/core/project.py | 36 ++++++++++++----------- tests/test_project.py | 47 +++++++++++++++++++++++------- 3 files changed, 57 insertions(+), 30 deletions(-) diff --git a/src/ansys/sherlock/core/errors.py b/src/ansys/sherlock/core/errors.py index 0f0058b0c..1962ff4b6 100644 --- a/src/ansys/sherlock/core/errors.py +++ b/src/ansys/sherlock/core/errors.py @@ -1027,7 +1027,7 @@ def __str__(self): return f"Export net list error: {self.message}" -class SherlockExportProjectException(Exception): +class SherlockExportProjectError(Exception): """Contains the error raised when a project cannot be exported.""" def __init__(self, message): @@ -1036,4 +1036,4 @@ def __init__(self, message): def __str__(self): """Format error message.""" - return f"Export Project error : {self.message}" + return f"Export project error : {self.message}" diff --git a/src/ansys/sherlock/core/project.py b/src/ansys/sherlock/core/project.py index bc95de9cf..f0c85320f 100644 --- a/src/ansys/sherlock/core/project.py +++ b/src/ansys/sherlock/core/project.py @@ -17,7 +17,7 @@ SherlockAddStrainMapsError, SherlockAddThermalMapsError, SherlockDeleteProjectError, - SherlockExportProjectException, + SherlockExportProjectError, SherlockGenerateProjectReportError, SherlockImportIpc2581Error, SherlockImportODBError, @@ -1711,34 +1711,36 @@ def export_project( """ Export a sherlock project. - Requires Sherlock Version: 25R1 + Requires Sherlock Version: 2025 R1 Parameters ---------- - project_name:str + project_name : str Name of the project being exported. - export_design_files: bool + export_design_files : bool Determines if design files should be exported. - export_result_files: bool + export_result_files : bool Determines if all analysis module result files should be exported. - export_archive_results: bool + export_archive_results : bool Determines if all archive result files should be exported. - export_user_files: bool + export_user_files : bool Determines if user properties and custom data files should be exported. - export_log_files: bool + export_log_files : bool Determines if Sherlock console and application log files should be exported. - export_system_data: bool + export_system_data : bool Determines if system technical data should be exported. - export_file_dir: str + export_file_dir : str Destination of export file. - export_file_name: str + export_file_name : str Name to be given to the exported file. - overwrite_existing_file: bool + overwrite_existing_file : bool Determines if exported file will overwrite a previously existing file. + Returns ------- int Status code of the response. 0 for success. + Examples -------- >>> from ansys.sherlock.core.launcher import launch_sherlock @@ -1756,13 +1758,13 @@ def export_project( """ try: if project_name == "": - raise SherlockExportProjectException(message="Project name is invalid") + raise SherlockExportProjectError(message="Project name is invalid") if export_file_dir == "": - raise SherlockExportProjectException(message="Export directory is invalid") + raise SherlockExportProjectError(message="Export directory is invalid") if export_file_name == "": - raise SherlockExportProjectException(message="Export file name is invalid") + raise SherlockExportProjectError(message="Export file name is invalid") if not self._is_connection_up(): LOG.error("There is no connection to a gRPC service.") @@ -1784,9 +1786,9 @@ def export_project( response = self.stub.exportProject(request) if response.value == -1: - raise SherlockExportProjectException(message=response.message) + raise SherlockExportProjectError(message=response.message) - except SherlockExportProjectException as e: + except SherlockExportProjectError as e: LOG.error(str(e)) raise e diff --git a/tests/test_project.py b/tests/test_project.py index 836bb84de..14085fadb 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -14,7 +14,7 @@ SherlockAddStrainMapsError, SherlockAddThermalMapsError, SherlockDeleteProjectError, - SherlockExportProjectException, + SherlockExportProjectError, SherlockGenerateProjectReportError, SherlockImportIpc2581Error, SherlockImportODBError, @@ -2983,12 +2983,12 @@ def helper_test_export_project(project): export_user_files=True, export_log_files=True, export_system_data=True, - export_file_dir="", - export_file_name="", + export_file_dir="/Test/Dir", + export_file_name="ExportedProject", overwrite_existing_file=True, ) - except SherlockExportProjectException as e: - assert str(e) == "Export Project error : Project name is invalid" + except SherlockExportProjectError as e: + assert str(e) == "Export project error : Project name is invalid" try: result = project.export_project( @@ -3000,11 +3000,11 @@ def helper_test_export_project(project): export_log_files=True, export_system_data=True, export_file_dir="", - export_file_name="", + export_file_name="ExportedProject", overwrite_existing_file=True, ) - except SherlockExportProjectException as e: - assert str(e) == "Export Project error : Export directory is invalid" + except SherlockExportProjectError as e: + assert str(e) == "Export project error : Export directory is invalid" try: result = project.export_project( @@ -3019,8 +3019,8 @@ def helper_test_export_project(project): export_file_name="", overwrite_existing_file=True, ) - except SherlockExportProjectException as e: - assert str(e) == "Export Project error : Export file name is invalid" + except SherlockExportProjectError as e: + assert str(e) == "Export project error : Export file name is invalid" if project._is_connection_up(): try: @@ -3038,7 +3038,32 @@ def helper_test_export_project(project): ) pytest.fail("No exception raised when using an invalid parameter") except Exception as e: - assert type(e) == SherlockExportProjectException + assert type(e) == SherlockExportProjectError + this_dir = os.path.dirname(os.path.realpath(__file__)) + output_file_name = "ExportedProject" + try: + result = project.export_project( + project_name="Tutorial Project", + export_design_files=True, + export_result_files=True, + export_archive_results=True, + export_user_files=True, + export_log_files=True, + export_system_data=True, + export_file_dir=this_dir, + export_file_name=output_file_name, + overwrite_existing_file=True, + ) + assert result == 0 + + # Clean up file + output_file = os.path.join(this_dir, output_file_name) + if os.path.exists(output_file): + os.remove(output_file) + else: + pytest.fail("Failed to generate export file.") + except SherlockExportProjectError as e: + pytest.fail(str(e)) if __name__ == "__main__":