Skip to content

Commit a4bdba9

Browse files
committed
fix python plugins
1 parent 477a2f7 commit a4bdba9

File tree

5 files changed

+55
-13
lines changed

5 files changed

+55
-13
lines changed

ansys/dpf/core/custom_operator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
operator_specification,
2020
dpf_operator,
2121
collection,
22+
AvailableServerContexts,
2223
)
2324
from ansys.dpf.core._custom_operators_helpers import __operator_main__, functions_registry, \
2425
external_operator_api, _type_to_output_method, _type_to_input_method
@@ -46,7 +47,7 @@ def record_operator(operator_type, *args) -> None:
4647
operator = operator_type
4748
if dpf.SERVER is None:
4849
settings.set_server_configuration(server_factory.ServerConfig(None, False))
49-
server.start_local_server()
50+
server.start_local_server(context=AvailableServerContexts.premium)
5051
if len(args) == 2:
5152
external_operator_api.external_operator_record_with_abstract_core_and_wrapper(
5253
operator._call_back(),

ansys/dpf/core/server.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from ansys.dpf.core.server_factory import ServerConfig, ServerFactory, CommunicationProtocols
2020
from ansys.dpf.core.server_types import DPF_DEFAULT_PORT, LOCALHOST, RUNNING_DOCKER
21+
from ansys.dpf.core import server_context
2122

2223

2324
def shutdown_global_server():
@@ -129,7 +130,8 @@ def start_local_server(
129130
docker_config=RUNNING_DOCKER,
130131
timeout=20.,
131132
config=None,
132-
use_pypim_by_default=True
133+
use_pypim_by_default=True,
134+
context=None
133135
):
134136
"""Start a new local DPF server at a given port and IP address.
135137
@@ -168,6 +170,10 @@ def start_local_server(
168170
use_pypim_by_default: bool, optional
169171
Whether to use PyPIM functionalities by default when a PyPIM environment is detected.
170172
Defaults to True.
173+
context: ServerContext, optional
174+
Defines the settings that will be used to load DPF's plugins.
175+
A DPF xml file can be used to list the plugins and set up variables. Default is
176+
`server_context.SERVER_CONTEXT`.
171177
172178
Returns
173179
-------
@@ -207,6 +213,9 @@ def start_local_server(
207213
else:
208214
docker_config.use_docker = False
209215

216+
if context is None:
217+
context = server_context.SERVER_CONTEXT
218+
210219
server = None
211220
n_attempts = 3
212221
timed_out = False
@@ -221,11 +230,11 @@ def start_local_server(
221230
server = server_type(
222231
ansys_path, ip, port, as_global=as_global, launch_server=True,
223232
load_operators=load_operators, docker_config=docker_config, timeout=timeout,
224-
use_pypim=use_pypim)
233+
use_pypim=use_pypim, context=context)
225234
else:
226235
server = server_type(
227236
ansys_path, as_global=as_global,
228-
load_operators=load_operators, timeout=timeout)
237+
load_operators=load_operators, timeout=timeout, context=context)
229238
break
230239
except errors.InvalidPortError: # allow socket in use errors
231240
port += 1
@@ -249,7 +258,14 @@ def start_local_server(
249258
return server
250259

251260

252-
def connect_to_server(ip=LOCALHOST, port=DPF_DEFAULT_PORT, as_global=True, timeout=5, config=None):
261+
def connect_to_server(
262+
ip=LOCALHOST,
263+
port=DPF_DEFAULT_PORT,
264+
as_global=True,
265+
timeout=5,
266+
config=None,
267+
context=None,
268+
):
253269
"""Connect to an existing DPF server.
254270
255271
This method sets the global default channel that is then used for the
@@ -273,6 +289,10 @@ def connect_to_server(ip=LOCALHOST, port=DPF_DEFAULT_PORT, as_global=True, timeo
273289
passes, the connection fails.
274290
config: ServerConfig, optional
275291
Manages the type of server connection to use.
292+
context: ServerContext, optional
293+
Defines the settings that will be used to load DPF's plugins.
294+
A DPF xml file can be used to list the plugins and set up variables. Default is
295+
`server_context.SERVER_CONTEXT`.
276296
277297
Examples
278298
--------
@@ -293,17 +313,20 @@ def connect_to_server(ip=LOCALHOST, port=DPF_DEFAULT_PORT, as_global=True, timeo
293313
>>> #unspecified_server = dpf.connect_to_server(as_global=False)
294314
295315
"""
296-
316+
if context is None:
317+
context = server_context.SERVER_CONTEXT
297318
def connect():
298319
server_init_signature = inspect.signature(server_type.__init__)
299320
if "ip" in server_init_signature.parameters.keys() \
300321
and "port" in server_init_signature.parameters.keys():
301322
server = server_type(
302-
ip=ip, port=port, as_global=as_global, launch_server=False
323+
ip=ip, port=port, as_global=as_global, launch_server=False,
324+
context=context
303325
)
304326
else:
305327
server = server_type(
306-
as_global=as_global
328+
as_global=as_global,
329+
context=context
307330
)
308331
dpf.core._server_instances.append(weakref.ref(server))
309332
return server

ansys/dpf/core/server_types.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@ def __init__(self,
614614
docker_config=RUNNING_DOCKER,
615615
use_pypim=True,
616616
num_connection_tryouts=3,
617+
context=server_context.SERVER_CONTEXT
617618
):
618619
# Load DPFClientAPI
619620
from ansys.dpf.core.misc import is_pypim_configured
@@ -652,7 +653,7 @@ def __init__(self,
652653
self._create_shutdown_funcs()
653654
self._check_first_call(num_connection_tryouts)
654655
try:
655-
self.apply_context(server_context.SERVER_CONTEXT)
656+
self.apply_context(context)
656657
except errors.DpfVersionNotSupported:
657658
pass
658659
self.set_as_global(as_global=as_global)
@@ -801,7 +802,9 @@ def __init__(self,
801802
ansys_path=None,
802803
as_global=True,
803804
load_operators=True,
804-
timeout=None):
805+
timeout=None,
806+
context=server_context.SERVER_CONTEXT
807+
):
805808
# Load DPFClientAPI
806809
super().__init__(ansys_path=ansys_path, load_operators=load_operators)
807810
# Load DataProcessingCore
@@ -817,7 +820,7 @@ def __init__(self,
817820
f"Unable to locate the following file: {path}")
818821
raise e
819822
try:
820-
self.apply_context(server_context.SERVER_CONTEXT)
823+
self.apply_context(context)
821824
except errors.DpfVersionNotSupported:
822825
self._base_service.initialize_with_context(
823826
server_context.AvailableServerContexts.premium
@@ -911,6 +914,7 @@ def __init__(
911914
launch_server=True,
912915
docker_config=RUNNING_DOCKER,
913916
use_pypim=True,
917+
context=server_context.SERVER_CONTEXT
914918
):
915919
"""Start the DPF server."""
916920
# Use ansys.grpc.dpf
@@ -964,7 +968,7 @@ def __init__(
964968

965969
check_ansys_grpc_dpf_version(self, timeout)
966970
try:
967-
self.apply_context(server_context.SERVER_CONTEXT)
971+
self.apply_context(context)
968972
except errors.DpfVersionNotSupported:
969973
pass
970974
self.set_as_global(as_global=as_global)

ansys/dpf/core/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from ansys.dpf.core.misc import module_exists
99
from ansys.dpf.core import misc
1010
from ansys.dpf.core.server import set_server_configuration # noqa: F401
11+
from ansys.dpf.core.server_context import set_default_server_context # noqa: F401
1112
from ansys.dpf.core.server_factory import ServerConfig # noqa: F401
1213
from ansys.dpf.core import core
1314

tests/test_service.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,19 @@ def revert():
462462

463463

464464
@pytest.mark.order(1)
465+
@pytest.mark.skipif(os.environ.get("ANSYS_DPF_ACCEPT_LA", None) is None,
466+
reason="Tests ANSYS_DPF_ACCEPT_LA")
467+
@conftest.raises_for_servers_version_under("6.0")
468+
def test_license_agr(config_server_type, set_context_back_to_premium):
469+
init_val = os.environ["ANSYS_DPF_ACCEPT_LA"]
470+
del os.environ["ANSYS_DPF_ACCEPT_LA"]
471+
with pytest.raises(dpf.core.errors.DPFServerException):
472+
rst = examples.find_static_rst() # starts a server
473+
os.environ["ANSYS_DPF_ACCEPT_LA"] = init_val
474+
assert "static" in examples.find_static_rst()
475+
476+
477+
@pytest.mark.order(2)
465478
@pytest.mark.skipif(running_docker or not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_5_0,
466479
reason="AWP ROOT is not set with Docker")
467480
@conftest.raises_for_servers_version_under("6.0")
@@ -493,7 +506,7 @@ def test_apply_context(set_context_back_to_premium):
493506
assert dpf.core.SERVER.context == dpf.core.AvailableServerContexts.premium
494507

495508

496-
@pytest.mark.order(2)
509+
@pytest.mark.order(3)
497510
@pytest.mark.skipif(not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_5_0,
498511
reason="not supported")
499512
@conftest.raises_for_servers_version_under("6.0")

0 commit comments

Comments
 (0)