-
Notifications
You must be signed in to change notification settings - Fork 25
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
# 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())) | ||
# 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) | ||
else: | ||
raise ModuleNotFoundError(f"Could not locate files for plugin {plugin}") | ||
|
||
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") | ||
|
||
# Upload python files | ||
_ = dpf.upload_files_in_folder( | ||
target_plugin_path, | ||
plugin_path, | ||
specific_extension=".py", | ||
server=server, | ||
) | ||
|
||
# upload zip files (site-packages) | ||
_ = dpf.upload_files_in_folder( | ||
target_plugin_path, | ||
plugin_path, | ||
specific_extension=".zip", | ||
server=server, | ||
) | ||
|
||
# Upload xml file for the plugin | ||
_ = dpf.upload_files_in_folder( | ||
target_xml_path, | ||
os.path.join(plugin_path, os.pardir), | ||
specific_extension=".xml", | ||
server=server, | ||
) | ||
|
||
# Load the plugin on the server | ||
dpf.load_library( | ||
filename=target_plugin_path, | ||
name=f"py_{plugin_name}", | ||
symbol=symbol, | ||
server=server, | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.