From 6a5aab8b9b9216ef2bb76e7b718b729979df00c3 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Fri, 26 Aug 2022 12:55:07 +0530 Subject: [PATCH 1/5] Session upload and download methods. --- src/ansys/fluent/core/session.py | 87 ++++++++++++++++++++++++++++++++ tests/test_uploader.py | 23 +++++++++ 2 files changed, 110 insertions(+) create mode 100644 tests/test_uploader.py diff --git a/src/ansys/fluent/core/session.py b/src/ansys/fluent/core/session.py index d163763b4db0..18513323b422 100644 --- a/src/ansys/fluent/core/session.py +++ b/src/ansys/fluent/core/session.py @@ -65,6 +65,7 @@ class _BaseSession: def __init__(self, fluent_connection: _FluentConnection): self.fluent_connection = fluent_connection self.scheme_eval = self.fluent_connection.scheme_eval + self._uploader = None @classmethod def create_from_server_info_file( @@ -170,6 +171,18 @@ def __dir__(self): ) ) + def _upload(self, file_path: str, remote_file_name: str = None): + """Uploads a file on the server.""" + if not self._uploader: + self._uploader = _Uploader(self.fluent_connection._remote_instance) + return self._uploader.upload(file_path, remote_file_name) + + def _download(self, file_name: str, local_file_path: str = None): + """Downloads a file from the server.""" + if not self._uploader: + self._uploader = _Uploader(self.fluent_connection._remote_instance) + return self._uploader.download(file_name, local_file_path) + class Session: """Instantiates a Fluent connection. This is a deprecated class. This has @@ -239,6 +252,8 @@ def __init__( self._datamodel_service_tui, self._settings_service ) + self._uploader = None + @classmethod def create_from_server_info_file( cls, @@ -319,6 +334,18 @@ def __dir__(self): ) ) + def _upload(self, file_path: str, remote_file_name: str = None): + """Uploads a file on the server.""" + if not self._uploader: + self._uploader = _Uploader(self.fluent_connection._remote_instance) + return self._uploader.upload(file_path, remote_file_name) + + def _download(self, file_name: str, local_file_path: str = None): + """Downloads a file from the server.""" + if not self._uploader: + self._uploader = _Uploader(self.fluent_connection._remote_instance) + return self._uploader.download(file_name, local_file_path) + class Solver: def __init__( self, tui_service: DatamodelService_TUI, settings_service: SettingsService @@ -348,3 +375,63 @@ def root(self): if self._settings_root is None: self._settings_root = settings_get_root(flproxy=self._settings_service) return self._settings_root + + +class _Uploader: + """Instantiates a file uploader and downloader to have a seamless file + reading / writing in the cloud particularly in Ansys lab . Here we are + exposing upload and download methods on session objects. These would be no- + ops if PyPIM is not configured or not authorized with the appropriate + service. This will be used for internal purpose only. + + Attributes + ---------- + pim_instance: PIM instance + Instance of PIM which supports upload server services. + + file_service: Client instance + Instance of Client which supports upload and download methods. + + Methods + ------- + upload( + file_path, remote_file_name + ) + Upload a file to the server. + + download( + file_name, local_file_path + ) + Download a file from the server. + """ + + def __init__(self, pim_instance): + self.pim_instance = pim_instance + + try: + upload_server = self.pim_instance.services["http-simple-upload-server"] + except AttributeError: + print("\nPIM is not installed or not authorized.\n") + except KeyError: + self.file_service = None + else: + from simple_upload_server.client import Client + + self.file_service = Client( + token="token", url=upload_server.uri, headers=upload_server.headers + ) + + def upload(self, file_path: str, remote_file_name: str = None): + """Uploads a file on the server.""" + import os + + expanded_file_path = os.path.expandvars(file_path) + upload_file_name = remote_file_name or os.path.basename(expanded_file_path) + self.file_service.upload_file(expanded_file_path, upload_file_name) + + def download(self, file_name: str, local_file_path: str = None): + """Downloads a file from the server.""" + if self.file_service.file_exist(file_name): + self.file_service.download_file(file_name, local_file_path) + else: + raise FileNotFoundError("Uploaded remote file name is different.") diff --git a/tests/test_uploader.py b/tests/test_uploader.py new file mode 100644 index 000000000000..f63d89ae2323 --- /dev/null +++ b/tests/test_uploader.py @@ -0,0 +1,23 @@ +import pytest + +from ansys.fluent.core import launch_fluent + + +def test_base_session(): + base_session = launch_fluent(cleanup_on_exit=False, mode="meshing") + with pytest.raises(Exception) as e_info: + base_session._upload( + "D:/PythonPractise/RealWithUnits/ConversionTable.py", "testNow.py" + ) + base_session._download("testNow.py", "D:/PythonPractise/RealWithUnits") + base_session.exit() + + +def test_session(): + session = launch_fluent(cleanup_on_exit=False, meshing_mode=True) + with pytest.raises(Exception) as e_info: + session._upload( + "D:/PythonPractise/RealWithUnits/ConversionTable.py", "testNow.py" + ) + session._download("testNow.py", "D:/PythonPractise/RealWithUnits") + session.exit() From 8088454af1493c4da11fefc50961f5ecce03f6b7 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Fri, 26 Aug 2022 13:35:22 +0530 Subject: [PATCH 2/5] Changed the file path and added LOG.error --- src/ansys/fluent/core/session.py | 2 +- tests/test_uploader.py | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/ansys/fluent/core/session.py b/src/ansys/fluent/core/session.py index 18513323b422..b22cd5b23c7e 100644 --- a/src/ansys/fluent/core/session.py +++ b/src/ansys/fluent/core/session.py @@ -411,7 +411,7 @@ def __init__(self, pim_instance): try: upload_server = self.pim_instance.services["http-simple-upload-server"] except AttributeError: - print("\nPIM is not installed or not authorized.\n") + LOG.error("PIM is not installed or not authorized.") except KeyError: self.file_service = None else: diff --git a/tests/test_uploader.py b/tests/test_uploader.py index f63d89ae2323..7c4772b1f5d9 100644 --- a/tests/test_uploader.py +++ b/tests/test_uploader.py @@ -1,5 +1,6 @@ import pytest +import ansys.fluent.core as pyfluent from ansys.fluent.core import launch_fluent @@ -7,9 +8,9 @@ def test_base_session(): base_session = launch_fluent(cleanup_on_exit=False, mode="meshing") with pytest.raises(Exception) as e_info: base_session._upload( - "D:/PythonPractise/RealWithUnits/ConversionTable.py", "testNow.py" + pyfluent.EXAMPLES_PATH + "/mixing_elbow.py", "test_upload_download.py" ) - base_session._download("testNow.py", "D:/PythonPractise/RealWithUnits") + base_session._download("test_upload_download.py", pyfluent.EXAMPLES_PATH) base_session.exit() @@ -17,7 +18,7 @@ def test_session(): session = launch_fluent(cleanup_on_exit=False, meshing_mode=True) with pytest.raises(Exception) as e_info: session._upload( - "D:/PythonPractise/RealWithUnits/ConversionTable.py", "testNow.py" + pyfluent.EXAMPLES_PATH + "/mixing_elbow.py", "test_upload_download.py" ) - session._download("testNow.py", "D:/PythonPractise/RealWithUnits") + session._download("test_upload_download.py", pyfluent.EXAMPLES_PATH) session.exit() From 374dc7ff9436eef6e739a4c20818c5dd0c9dd535 Mon Sep 17 00:00:00 2001 From: Prithwish Mukherjee Date: Fri, 26 Aug 2022 14:28:44 +0530 Subject: [PATCH 3/5] Used pytest.fixtures in test --- tests/test_uploader.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/test_uploader.py b/tests/test_uploader.py index 7c4772b1f5d9..67fd8d894d0e 100644 --- a/tests/test_uploader.py +++ b/tests/test_uploader.py @@ -1,11 +1,10 @@ import pytest import ansys.fluent.core as pyfluent -from ansys.fluent.core import launch_fluent -def test_base_session(): - base_session = launch_fluent(cleanup_on_exit=False, mode="meshing") +def test_base_session(new_mesh_session): + base_session = new_mesh_session with pytest.raises(Exception) as e_info: base_session._upload( pyfluent.EXAMPLES_PATH + "/mixing_elbow.py", "test_upload_download.py" @@ -14,8 +13,8 @@ def test_base_session(): base_session.exit() -def test_session(): - session = launch_fluent(cleanup_on_exit=False, meshing_mode=True) +def test_session(new_mesh_session): + session = new_mesh_session with pytest.raises(Exception) as e_info: session._upload( pyfluent.EXAMPLES_PATH + "/mixing_elbow.py", "test_upload_download.py" From dbf5664199cec585187e014129dbf645f20d080c Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Fri, 26 Aug 2022 16:28:50 +0530 Subject: [PATCH 4/5] Restructured session.py and added pytest.fixture --- src/ansys/fluent/core/session.py | 3 +-- tests/test_uploader.py | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ansys/fluent/core/session.py b/src/ansys/fluent/core/session.py index b22cd5b23c7e..5b030a0947ed 100644 --- a/src/ansys/fluent/core/session.py +++ b/src/ansys/fluent/core/session.py @@ -1,6 +1,7 @@ """Module containing class encapsulating Fluent connection and the Base Session.""" import json +import os from typing import Any import warnings @@ -423,8 +424,6 @@ def __init__(self, pim_instance): def upload(self, file_path: str, remote_file_name: str = None): """Uploads a file on the server.""" - import os - expanded_file_path = os.path.expandvars(file_path) upload_file_name = remote_file_name or os.path.basename(expanded_file_path) self.file_service.upload_file(expanded_file_path, upload_file_name) diff --git a/tests/test_uploader.py b/tests/test_uploader.py index 67fd8d894d0e..399db603909b 100644 --- a/tests/test_uploader.py +++ b/tests/test_uploader.py @@ -1,4 +1,5 @@ import pytest +from util.meshing_workflow import new_mesh_session # noqa: F401 import ansys.fluent.core as pyfluent From d2d781738068893f1b9964e2c47018a94dd9de33 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar Date: Mon, 29 Aug 2022 12:12:13 +0530 Subject: [PATCH 5/5] Assertion is raised for individual method and FileNotFoundError message is changed --- src/ansys/fluent/core/session.py | 2 +- tests/test_uploader.py | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/ansys/fluent/core/session.py b/src/ansys/fluent/core/session.py index 5b030a0947ed..24c8f349e1d5 100644 --- a/src/ansys/fluent/core/session.py +++ b/src/ansys/fluent/core/session.py @@ -433,4 +433,4 @@ def download(self, file_name: str, local_file_path: str = None): if self.file_service.file_exist(file_name): self.file_service.download_file(file_name, local_file_path) else: - raise FileNotFoundError("Uploaded remote file name is different.") + raise FileNotFoundError("Remote file does not exist.") diff --git a/tests/test_uploader.py b/tests/test_uploader.py index 399db603909b..006fe843a176 100644 --- a/tests/test_uploader.py +++ b/tests/test_uploader.py @@ -4,21 +4,33 @@ import ansys.fluent.core as pyfluent -def test_base_session(new_mesh_session): +def test_base_session_upload(new_mesh_session): base_session = new_mesh_session - with pytest.raises(Exception) as e_info: + with pytest.raises(AttributeError) as e_info: base_session._upload( pyfluent.EXAMPLES_PATH + "/mixing_elbow.py", "test_upload_download.py" ) + base_session.exit() + + +def test_base_session_download(new_mesh_session): + base_session = new_mesh_session + with pytest.raises(AttributeError) as e_info: base_session._download("test_upload_download.py", pyfluent.EXAMPLES_PATH) base_session.exit() -def test_session(new_mesh_session): +def test_session_upload(new_mesh_session): session = new_mesh_session - with pytest.raises(Exception) as e_info: + with pytest.raises(AttributeError) as e_info: session._upload( pyfluent.EXAMPLES_PATH + "/mixing_elbow.py", "test_upload_download.py" ) + session.exit() + + +def test_session_download(new_mesh_session): + session = new_mesh_session + with pytest.raises(AttributeError) as e_info: session._download("test_upload_download.py", pyfluent.EXAMPLES_PATH) session.exit()