Skip to content

Commit bc43b4d

Browse files
authored
Examples/refactor custom plugin examples (#1946)
* Create examples.download_average_filter_plugin * Create examples.download_gltf_plugin * Create examples.download_easy_statistics * Apply suggestions from code review * Add testing
1 parent effc7f8 commit bc43b4d

File tree

5 files changed

+162
-66
lines changed

5 files changed

+162
-66
lines changed

examples/08-python-operators/00-wrapping_numpy_capabilities.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,9 @@
5656
#
5757
# Download and display the Python script.
5858

59-
from ansys.dpf.core import examples
60-
59+
from ansys.dpf.core.examples import download_easy_statistics
6160

62-
GITHUB_SOURCE_URL = (
63-
"https://github.com/ansys/pydpf-core/raw/master/doc/source/examples/07-python-operators/plugins"
64-
)
65-
EXAMPLE_FILE = GITHUB_SOURCE_URL + "/easy_statistics.py"
66-
operator_file_path = examples.downloads._retrieve_file(
67-
EXAMPLE_FILE, "easy_statistics.py", "python_plugins"
68-
)
61+
operator_file_path = download_easy_statistics()
6962

7063
with open(operator_file_path, "r") as f:
7164
for line in f.readlines():

examples/08-python-operators/01-package_python_operators.py

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,35 +54,9 @@
5454
#
5555
# Download the ``average_filter_plugin`` plug-in package that has already been
5656
# created for you.
57-
58-
import os
59-
6057
from ansys.dpf.core import examples
6158

62-
63-
print("\033[1m average_filter_plugin")
64-
file_list = [
65-
"average_filter_plugin/__init__.py",
66-
"average_filter_plugin/operators.py",
67-
"average_filter_plugin/operators_loader.py",
68-
"average_filter_plugin/common.py",
69-
]
70-
plugin_folder = None
71-
GITHUB_SOURCE_URL = (
72-
"https://github.com/ansys/pydpf-core/raw/"
73-
"master/doc/source/examples/07-python-operators/plugins/"
74-
)
75-
76-
for file in file_list:
77-
EXAMPLE_FILE = GITHUB_SOURCE_URL + file
78-
operator_file_path = examples.downloads._retrieve_file(EXAMPLE_FILE, file, "python_plugins")
79-
plugin_folder = os.path.dirname(operator_file_path)
80-
print(f"\033[1m {file}:\n \033[0m")
81-
with open(operator_file_path, "r") as f:
82-
for line in f.readlines():
83-
print("\t\t\t" + line)
84-
print("\n\n")
85-
59+
plugin_folder = examples.download_average_filter_plugin()
8660

8761
###############################################################################
8862
# Load the plug-in package

examples/08-python-operators/02-python_operators_with_dependencies.py

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -61,37 +61,8 @@
6161

6262
from ansys.dpf.core import examples
6363

64-
65-
print("\033[1m gltf_plugin")
66-
file_list = [
67-
"gltf_plugin/__init__.py",
68-
"gltf_plugin/operators.py",
69-
"gltf_plugin/operators_loader.py",
70-
"gltf_plugin/requirements.txt",
71-
"gltf_plugin/gltf_export.py",
72-
"gltf_plugin/texture.png",
73-
"gltf_plugin.xml",
74-
]
75-
plugin_path = None
64+
plugin_path = examples.download_gltf_plugin()
7665
folder_root = os.path.join(os.getcwd().rsplit("pydpf-core", 1)[0], "pydpf-core")
77-
GITHUB_SOURCE_URL = (
78-
"https://github.com/ansys/pydpf-core/raw/"
79-
"master/doc/source/examples/07-python-operators/plugins/"
80-
)
81-
82-
for file in file_list:
83-
EXAMPLE_FILE = GITHUB_SOURCE_URL + file
84-
operator_file_path = examples.downloads._retrieve_file(EXAMPLE_FILE, file, "python_plugins")
85-
print(f"\033[1m {file}\n \033[0m")
86-
if (
87-
os.path.splitext(file)[1] == ".py" or os.path.splitext(file)[1] == ".xml"
88-
) and file != "gltf_plugin/gltf_export.py":
89-
with open(operator_file_path, "r") as f:
90-
for line in f.readlines():
91-
print("\t\t\t" + line)
92-
print("\n\n")
93-
if plugin_path is None:
94-
plugin_path = os.path.dirname(operator_file_path)
9566

9667
# %%
9768
# To add third-party modules as dependencies to a plug-in package, you must

src/ansys/dpf/core/examples/downloads.py

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,17 @@
2828
import os
2929
import urllib.request
3030
import warnings
31+
from typing import Union
32+
3133
from ansys.dpf.core.examples.examples import find_files
3234

3335
EXAMPLE_REPO = "https://github.com/ansys/example-data/raw/master/"
3436

37+
GITHUB_SOURCE_URL = (
38+
"https://github.com/ansys/pydpf-core/raw/"
39+
"master/doc/source/examples/07-python-operators/plugins/"
40+
)
41+
3542

3643
def delete_downloads(verbose=True):
3744
"""Delete all downloaded examples to free space or update the files"""
@@ -1993,3 +2000,142 @@ def find_distributed_msup_folder(
19932000
return_local_path,
19942001
)
19952002
return os.path.dirname(path)
2003+
2004+
2005+
def download_average_filter_plugin(
2006+
should_upload: bool = True, server=None, return_local_path=False
2007+
) -> Union[str, None]:
2008+
"""Make the plugin available server side, if the server is remote the plugin is uploaded
2009+
server side. Returns the path of the plugin folder.
2010+
2011+
Parameters
2012+
----------
2013+
should_upload:
2014+
Whether the file should be uploaded server side when the server is remote.
2015+
server:
2016+
Server with channel connected to the remote or local instance. When
2017+
``None``, attempts to use the global server.
2018+
return_local_path:
2019+
If ``True``, the local path is returned as is, without uploading, nor searching
2020+
for mounted volumes.
2021+
2022+
Returns
2023+
-------
2024+
str
2025+
Path to the plugin folder.
2026+
2027+
Examples
2028+
--------
2029+
2030+
>>> from ansys.dpf.core import examples
2031+
>>> path = examples.download_average_filter_plugin()
2032+
2033+
"""
2034+
file_list = [
2035+
"average_filter_plugin/__init__.py",
2036+
"average_filter_plugin/operators.py",
2037+
"average_filter_plugin/operators_loader.py",
2038+
"average_filter_plugin/common.py",
2039+
]
2040+
return _retrieve_plugin(
2041+
file_list=file_list,
2042+
should_upload=should_upload,
2043+
server=server,
2044+
return_local_path=return_local_path,
2045+
)
2046+
2047+
2048+
def download_gltf_plugin(
2049+
should_upload: bool = True, server=None, return_local_path=False
2050+
) -> Union[str, None]:
2051+
"""Make the plugin available server side, if the server is remote the plugin is uploaded
2052+
server side. Returns the path of the plugin folder.
2053+
2054+
Parameters
2055+
----------
2056+
should_upload:
2057+
Whether the file should be uploaded server side when the server is remote.
2058+
server:
2059+
Server with channel connected to the remote or local instance. When
2060+
``None``, attempts to use the global server.
2061+
return_local_path:
2062+
If ``True``, the local path is returned as is, without uploading, nor searching
2063+
for mounted volumes.
2064+
2065+
Returns
2066+
-------
2067+
str
2068+
Path to the plugin folder.
2069+
2070+
Examples
2071+
--------
2072+
2073+
>>> from ansys.dpf.core import examples
2074+
>>> path = examples.download_gltf_plugin()
2075+
2076+
"""
2077+
file_list = [
2078+
"gltf_plugin.xml",
2079+
"gltf_plugin/__init__.py",
2080+
"gltf_plugin/operators.py",
2081+
"gltf_plugin/operators_loader.py",
2082+
"gltf_plugin/requirements.txt",
2083+
"gltf_plugin/gltf_export.py",
2084+
"gltf_plugin/texture.png",
2085+
]
2086+
return _retrieve_plugin(
2087+
file_list=file_list,
2088+
should_upload=should_upload,
2089+
server=server,
2090+
return_local_path=return_local_path,
2091+
)
2092+
2093+
2094+
def download_easy_statistics(
2095+
should_upload: bool = True, server=None, return_local_path=False
2096+
) -> Union[str, None]:
2097+
"""Make the plugin available server side, if the server is remote the plugin is uploaded
2098+
server side. Returns the path of the plugin folder.
2099+
2100+
Parameters
2101+
----------
2102+
should_upload:
2103+
Whether the file should be uploaded server side when the server is remote.
2104+
server:
2105+
Server with channel connected to the remote or local instance. When
2106+
``None``, attempts to use the global server.
2107+
return_local_path:
2108+
If ``True``, the local path is returned as is, without uploading, nor searching
2109+
for mounted volumes.
2110+
2111+
Returns
2112+
-------
2113+
str
2114+
Path to the plugin folder.
2115+
2116+
Examples
2117+
--------
2118+
2119+
>>> from ansys.dpf.core import examples
2120+
>>> path = examples.download_easy_statistics()
2121+
2122+
"""
2123+
file_name = "easy_statistics.py"
2124+
EXAMPLE_FILE = GITHUB_SOURCE_URL + file_name
2125+
operator_file_path = _retrieve_file(
2126+
EXAMPLE_FILE, filename=file_name, directory="python_plugins"
2127+
)
2128+
return find_files(operator_file_path, should_upload, server, return_local_path)
2129+
2130+
2131+
def _retrieve_plugin(
2132+
file_list: list[str], should_upload: bool = True, server=None, return_local_path=False
2133+
) -> Union[str, None]:
2134+
path = None
2135+
for file in file_list:
2136+
EXAMPLE_FILE = GITHUB_SOURCE_URL + file
2137+
operator_file_path = _retrieve_file(EXAMPLE_FILE, file, directory="python_plugins")
2138+
path = os.path.dirname(
2139+
find_files(operator_file_path, should_upload, server, return_local_path)
2140+
)
2141+
return path

tests/test_examples.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,15 @@ def test_get_example_required_minimum_dpf_version(tmp_path):
194194
p = tmp_path / "test_example_version_1.py"
195195
p.write_text(example_header)
196196
assert examples.get_example_required_minimum_dpf_version(p) == "0.0"
197+
198+
199+
def test_download_easy_statistics():
200+
assert os.path.exists(examples.download_easy_statistics(return_local_path=True))
201+
202+
203+
def test_download_average_filter_plugin():
204+
assert os.path.exists(examples.download_average_filter_plugin(return_local_path=True))
205+
206+
207+
def test_download_gltf_plugin():
208+
assert os.path.exists(examples.download_gltf_plugin(return_local_path=True))

0 commit comments

Comments
 (0)