Skip to content
78 changes: 51 additions & 27 deletions src/ansys/dpf/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@

"""Core."""

from __future__ import annotations

import logging
import os
from pathlib import Path
from pathlib import Path, PurePosixPath, PureWindowsPath
from typing import TYPE_CHECKING
import warnings
import weakref

from ansys.dpf.core import errors, misc, server as server_module

if TYPE_CHECKING: # pragma: noqa
from ansys.dpf.core import AnyServerType
from ansys.dpf.core.check_version import server_meet_version, version_requires
from ansys.dpf.core.runtime_config import (
RuntimeClientConfig,
Expand Down Expand Up @@ -61,26 +67,31 @@
CONFIGURATION = "release"


def load_library(filename, name="", symbol="LoadOperators", server=None, generate_operators=False):
"""Dynamically load an operators library for dpf.core.
def load_library(
filename: str | Path,
name: str = None,
symbol: str = "LoadOperators",
server: AnyServerType = None,
generate_operators: bool = False,
):
"""Load a DPF plugin (a binary library of operators).

Code containing this library's operators is generated in
ansys.dpf.core.operators
Set `generate_operators=True` to also make the operators available in the current
installation of `ansys-dpf-core`.

Parameters
----------
filename : str or os.PathLike
Filename of the operator library.

name : str, optional
Library name. Probably optional

server : server.DPFServer, optional
Server with channel connected to the remote or local instance. When
``None``, attempts to use the global server.

generate_operators : bool, optional
Whether operators code generation should be done or not (default is False).
filename:
Filename or path to the operator library.
name:
Name to give the plugin once loaded. Defaults to the name of the library file.
symbol:
The name of the entrypoint of the plugin, which is the function recording the operators.
server:
Server to load the plugin onto. Defaults to the global server.
generate_operators:
Whether to generate the Python modules for the operators of the library.
This updates the ansys.dpf.core.operators package of the current installation.

Examples
--------
Expand Down Expand Up @@ -385,22 +396,29 @@ def make_tmp_dir_server(self):
else:
return self._api_tmp_dir.tmp_dir_get_dir()

def load_library(self, file_path, name="", symbol="LoadOperators", generate_operators=False):
def load_library(
self,
file_path: str | Path,
name: str = None,
symbol: str = "LoadOperators",
generate_operators: bool = False,
):
"""Dynamically load an operators library for dpf.core.

Code containing this library's operators is generated in
ansys.dpf.core.operators

Parameters
----------
file_path : str or os.PathLike
file_path of the operator library.

name : str, optional
Library name. Probably optional

generate_operators : bool, optional
Whether operators code generation should be done or not (default is False).
file_path:
Path to the DPF plugin file holding a library of operators.
name:
Name to give the plugin once loaded. Defaults to the name of the library file.
symbol:
The name of the entrypoint of the plugin, which is the function recording the operators.
generate_operators:
Whether to generate the Python modules for the operators of the library.
This updates the ansys.dpf.core.operators package of the current installation.

Examples
--------
Expand All @@ -412,7 +430,13 @@ def load_library(self, file_path, name="", symbol="LoadOperators", generate_oper
>>> # base.load_library('meshOperatorsCore.dll', 'mesh_operators')

"""
file_path = str(file_path)
if name is None:
name = Path(file_path).name
file_path = str(
PurePosixPath(file_path)
if self.server_info["os"] == "posix"
else PureWindowsPath(file_path)
)
if self._server().has_client():
self._internal_obj = self._api.data_processing_load_library_on_client(
sLibraryKey=name,
Expand Down
11 changes: 9 additions & 2 deletions src/ansys/dpf/core/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

"""

from __future__ import annotations

import os.path
from pathlib import Path

Expand All @@ -39,13 +41,18 @@
from ansys.dpf.core import server as server_module


def load_plugin_on_server(plugin, server=None, symbol="load_operators", generate_operators=False):
def load_plugin_on_server(
plugin: str | Path,
server: dpf.AnyServerType = None,
symbol: str = "load_operators",
generate_operators: bool = False,
):
"""Load a DPF Python plugin on the global or given DPF server.

Parameters
----------
plugin:
DPF Python plugin to load.
Path to the DPF Python plugin to load.
server:
DPF server to load the plugin onto.
symbol:
Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ def return_ds(server=None):
return return_ds


DEFAULT_ANSYS_PATH = core._global_server().ansys_path

SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_11_0 = meets_version(
get_server_version(core._global_server()), "11.0"
)
Expand Down
11 changes: 11 additions & 0 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

from ansys.dpf import core as dpf
from ansys.dpf.core import examples
import conftest
from conftest import SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_5_0


Expand Down Expand Up @@ -134,3 +135,13 @@ def test_vtk(server_type, tmpdir):
# print(e)
# pass
# assert os.path.exists(tmp_path)


def test_load_library_default_name(config_server_type):
xml_path = Path(conftest.DEFAULT_ANSYS_PATH) / "dpf" / "utilities" / "DpfCustomDefined.xml"
server_context = dpf.server_context.ServerContext(xml_path=str(xml_path))
print(server_context)
server = dpf.start_local_server(config=config_server_type, context=server_context)
print(server.plugins)
# TODO: fix use of custom XML at server startup. The above should only show grpc loaded
# https://github.com/ansys/pydpf-core/issues/2666
Loading