diff --git a/src/ansys/dpf/core/core.py b/src/ansys/dpf/core/core.py index b479cdb0e3f..a6ea976acc7 100644 --- a/src/ansys/dpf/core/core.py +++ b/src/ansys/dpf/core/core.py @@ -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, @@ -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 -------- @@ -385,7 +396,13 @@ 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 @@ -393,14 +410,15 @@ def load_library(self, file_path, name="", symbol="LoadOperators", generate_oper 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 -------- @@ -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, diff --git a/src/ansys/dpf/core/plugins.py b/src/ansys/dpf/core/plugins.py index 28ac0f57a74..5b5ff3c7262 100644 --- a/src/ansys/dpf/core/plugins.py +++ b/src/ansys/dpf/core/plugins.py @@ -27,6 +27,8 @@ """ +from __future__ import annotations + import os.path from pathlib import Path @@ -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: diff --git a/tests/conftest.py b/tests/conftest.py index f241ee23aa4..f8d7541736c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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" ) diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 506e5520392..a341381a4d6 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -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 @@ -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