Skip to content

Add plugins.py module with load_plugin_on_server helper #1600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ansys/dpf/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
config_options
)
from ansys.dpf.core import help
from ansys.dpf.core import plugins
from ansys.dpf.core.core import (
BaseService,
load_library,
Expand Down
77 changes: 77 additions & 0 deletions src/ansys/dpf/core/plugins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""
Python DPF plugins utilities
============================

Contains the utilities specific to installing and using Python DPF plugins.
"""
import os.path
import importlib.metadata as importlib_metadata

import ansys.dpf.core as dpf
from ansys.dpf.core import server as server_module


def load_plugin_on_server(plugin, server=None, symbol="load_operators"):
"""Load a DPF Python plugin on the global or given DPF server.

Parameters
----------
plugin:
DPF Python plugin to load.
server:
DPF server to load the plugin onto.
symbol:
Name of the function recording the operators in the plugin.
"""
server = server_module.get_or_create_server(server)
plugin_name = plugin.split("-")[-1]
tmp_folder = dpf.make_tmp_dir_server(server=server)

Check warning on line 28 in src/ansys/dpf/core/plugins.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/plugins.py#L26-L28

Added lines #L26 - L28 were not covered by tests

# Get the path to the plugin from the package installation
if len([p for p in importlib_metadata.files(plugin) if "__init__.py" in str(p)]) > 0:
file_path = [p for p in importlib_metadata.files(plugin) if "__init__.py" in str(p)][0]
plugin_path = str(os.path.dirname(file_path.locate()))

Check warning on line 33 in src/ansys/dpf/core/plugins.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/plugins.py#L31-L33

Added lines #L31 - L33 were not covered by tests
# For some reason the "locate()" function returns a path with src doubled
plugin_path = plugin_path.replace("src"+os.path.sep+"src", "src")
elif len([p for p in importlib_metadata.files(plugin) if ".pth" in str(p)]) > 0:
path_file = [p for p in importlib_metadata.files(plugin) if ".pth" in str(p)][0].locate()
with open(path_file, "r") as file:
plugin_path = file.readline()[:-1]
plugin_path = os.path.join(plugin_path, "ansys", "dpf", "plugins", plugin_name)

Check warning on line 40 in src/ansys/dpf/core/plugins.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/plugins.py#L35-L40

Added lines #L35 - L40 were not covered by tests
else:
raise ModuleNotFoundError(f"Could not locate files for plugin {plugin}")

Check warning on line 42 in src/ansys/dpf/core/plugins.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/plugins.py#L42

Added line #L42 was not covered by tests

target_plugin_path = dpf.path_utilities.join(tmp_folder, "ansys", "dpf", "plugins", plugin_name)
target_xml_path = dpf.path_utilities.join(tmp_folder, "ansys", "dpf", "plugins")

Check warning on line 45 in src/ansys/dpf/core/plugins.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/plugins.py#L44-L45

Added lines #L44 - L45 were not covered by tests

# Upload python files
_ = dpf.upload_files_in_folder(

Check warning on line 48 in src/ansys/dpf/core/plugins.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/plugins.py#L48

Added line #L48 was not covered by tests
target_plugin_path,
plugin_path,
specific_extension=".py",
server=server,
)

# upload zip files (site-packages)
_ = dpf.upload_files_in_folder(

Check warning on line 56 in src/ansys/dpf/core/plugins.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/plugins.py#L56

Added line #L56 was not covered by tests
target_plugin_path,
plugin_path,
specific_extension=".zip",
server=server,
)

# Upload xml file for the plugin
_ = dpf.upload_files_in_folder(

Check warning on line 64 in src/ansys/dpf/core/plugins.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/plugins.py#L64

Added line #L64 was not covered by tests
target_xml_path,
os.path.join(plugin_path, os.pardir),
specific_extension=".xml",
server=server,
)

# Load the plugin on the server
dpf.load_library(

Check warning on line 72 in src/ansys/dpf/core/plugins.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/plugins.py#L72

Added line #L72 was not covered by tests
filename=target_plugin_path,
name=f"py_{plugin_name}",
symbol=symbol,
server=server,
)
Loading