diff --git a/doc/changelog.d/346.documentation.md b/doc/changelog.d/346.documentation.md new file mode 100644 index 00000000..c394abf5 --- /dev/null +++ b/doc/changelog.d/346.documentation.md @@ -0,0 +1 @@ +Replace ExampleData with ansys-tools-common utility diff --git a/doc/source/user-guide.rst b/doc/source/user-guide.rst index 9025f8de..c2bbf707 100644 --- a/doc/source/user-guide.rst +++ b/doc/source/user-guide.rst @@ -152,7 +152,7 @@ This client script downloads all files with ``.out`` extensions from the server' wb.download_file("*.out") There is a special client method to upload a data file from the Ansys -`example-data `_ repository +`example-data `_ repository directly to the Workbench server. You should specify the path relative to the ``pyworkbench`` folder in the ``example-data`` repository: diff --git a/src/ansys/workbench/core/example_data.py b/src/ansys/workbench/core/example_data.py deleted file mode 100644 index cc9b0dfb..00000000 --- a/src/ansys/workbench/core/example_data.py +++ /dev/null @@ -1,98 +0,0 @@ -# Copyright (C) 2023 - 2026 ANSYS, Inc. and/or its affiliates. -# SPDX-License-Identifier: MIT -# -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - -"""Module for downloading example data from the ``example-data`` repository.""" - -import logging -import os -import shutil -import urllib.request - - -class ExampleData: - """Provides for downloading example data from the ``example-data`` repository.""" - - def _get_file_url(relative_file_path): # noqa: N805 - """Get the URL of the file in the ``example-data`` repository. - - Parameters - ---------- - filename : str - Name of the file. - dirname : str - Name of the directory containing the file. - - Returns - ------- - str - URL of the file in the ``example-data`` repository. - """ - return f"https://github.com/ansys/example-data/raw/master/pyworkbench/{relative_file_path}" - - def __retrieve_file(url, local_file_path): # noqa: N805 - """Download the file from the URL. - - Parameters - ---------- - url : str - URL of the file. - local_file_path : str - Local path to save the file to. - - Raises - ------ - Exception - If the URL is not accessible. - """ - logging.info(f"Downloading {url} from ``example-data`` repository ...") - - # Ignoring bandit error as we need to use urllib to download the file - with urllib.request.urlopen(url) as in_stream: # nosec: B310 - if in_stream.code != 200: - raise Exception("error getting the url, code " + str(in_stream.code)) - with open(local_file_path, "wb") as out_file: - shutil.copyfileobj(in_stream, out_file) - logging.info(f"Downloaded the file as {local_file_path}.") - - def download(relative_file_path, local_dir_path): # noqa: N805 - """Download the file from the ``example-data`` repository. - - Parameters - ---------- - relative_file_path : str - File path relative to the ``pyworkbench`` folder in the ``example-data`` repository. - local_dir_path : str - Local path to the directory to save the file to. - - Returns - ------- - str - Name of the downloaded file. - """ - url = ExampleData._get_file_url(relative_file_path) - downloaded_file_name = os.path.basename(relative_file_path) - local_file_path = os.path.join(local_dir_path, downloaded_file_name) - ExampleData.__retrieve_file(url, local_file_path) - return downloaded_file_name - - -__all__ = [] diff --git a/src/ansys/workbench/core/workbench_client.py b/src/ansys/workbench/core/workbench_client.py index ffb0c89a..7c22d34f 100644 --- a/src/ansys/workbench/core/workbench_client.py +++ b/src/ansys/workbench/core/workbench_client.py @@ -36,7 +36,7 @@ from ansys.api.workbench.v0 import workbench_pb2 as wb from ansys.api.workbench.v0.workbench_pb2_grpc import WorkbenchServiceStub from ansys.tools.common.cyberchannel import create_channel -from ansys.workbench.core.example_data import ExampleData +from ansys.tools.common.example_download import download_manager class WorkbenchClient: @@ -338,7 +338,11 @@ def upload_file_from_example_repo(self, relative_file_path, show_progress=True): if not self._is_connected(): logging.error("Workbench client is not yet connected to a server.") return - downloaded = ExampleData.download(relative_file_path, self.workdir) + dir, fn = os.path.split(relative_file_path) + dir = "pyworkbench/" + dir + downloaded = download_manager.download_file( + filename=fn, directory=dir, destination=self.workdir + ) self.upload_file(downloaded, show_progress=show_progress) def download_file(self, file_name, show_progress=True, target_dir=None): diff --git a/tests/test_example_data.py b/tests/test_example_data.py deleted file mode 100644 index d191b429..00000000 --- a/tests/test_example_data.py +++ /dev/null @@ -1,54 +0,0 @@ -# Copyright (C) 2023 - 2026 ANSYS, Inc. and/or its affiliates. -# SPDX-License-Identifier: MIT -# -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in all -# copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - -"""Tests for the example_data module.""" - -import pathlib - -import pytest - -from ansys.workbench.core.example_data import ExampleData - - -@pytest.fixture(scope="module") -def example_data(): - """Return example data.""" - file_name = "axisymmetric_model.agdb" - dir_name = "axisymmetric-rotor/agdb" - source_path = f"{dir_name}/{file_name}" - target_local_dir = "tests/assets/" - return source_path, target_local_dir - - -def test_get_file_url(example_data): - """Test get_file_url.""" - source_path, target_local_dir = example_data - url = ExampleData._get_file_url(relative_file_path=source_path) - assert url == f"https://github.com/ansys/example-data/raw/master/pyworkbench/{source_path}" - - -def test_download(example_data): - """Test download.""" - source_path, target_local_dir = example_data - local_file_name = ExampleData.download(source_path, target_local_dir) - local_file = pathlib.Path(target_local_dir) / local_file_name - assert local_file.exists() diff --git a/tests/test_workbench_client.py b/tests/test_workbench_client.py index f9ba9138..a8844b4f 100644 --- a/tests/test_workbench_client.py +++ b/tests/test_workbench_client.py @@ -213,7 +213,7 @@ def test_upload_file_from_example_repo(mock_workbench_service_stub): with patch("ansys.workbench.core.workbench_client.os.path.isfile", return_value=True): with patch( - "ansys.workbench.core.workbench_client.ExampleData.download", + "ansys.tools.common.example_download.download_manager.download_file", return_value="/tmp/axisymmetric_model.agdb", ): client.upload_file_from_example_repo(