diff --git a/src/ansys/dpf/core/core.py b/src/ansys/dpf/core/core.py index 2f7e0663358..639e9a4d298 100644 --- a/src/ansys/dpf/core/core.py +++ b/src/ansys/dpf/core/core.py @@ -499,6 +499,15 @@ def initialize_with_context(self, context): int(context.licensing_context_type), context.xml_path ) + def initialize(self): + """Initialize a DPF server without a context.""" + if self._server().has_client(): + self._api.data_processing_initialization_on_client( + self._server().client + ) + else: + self._api.data_processing_initialization() + @version_requires("6.0") def release_dpf(self): """Clears the available Operators and Releases licenses when necessary. diff --git a/src/ansys/dpf/core/server_context.py b/src/ansys/dpf/core/server_context.py index f43735132d0..3379100c016 100644 --- a/src/ansys/dpf/core/server_context.py +++ b/src/ansys/dpf/core/server_context.py @@ -21,6 +21,7 @@ class LicensingContextType(Enum): + none = 5 premium = 1 """Checks if at least one license increment exists and allows operators to block an increment.""" @@ -33,6 +34,9 @@ def __int__(self): @staticmethod def same_licensing_context(first, second): + if ((first == LicensingContextType.none and second != LicensingContextType.none) + or (first != LicensingContextType.none and second == LicensingContextType.none)): + return False if int(first) == int(LicensingContextType.entry) and int(second) != int( LicensingContextType.entry ): @@ -207,6 +211,7 @@ def __ne__(self, other): class AvailableServerContexts: """Defines available server contexts.""" + no_context = ServerContext(LicensingContextType.none, "") pre_defined_environment = ServerContext(0) """DataProcessingCore.xml that is next to DataProcessingCore.dll/libDataProcessingCore.so will be taken""" diff --git a/src/ansys/dpf/core/server_types.py b/src/ansys/dpf/core/server_types.py index 0d842e74c36..bc1088dc7ea 100644 --- a/src/ansys/dpf/core/server_types.py +++ b/src/ansys/dpf/core/server_types.py @@ -4,6 +4,7 @@ Contains the different kinds of servers available for the factory. """ +from __future__ import annotations import abc import io import os @@ -15,7 +16,8 @@ import traceback from threading import Thread, Lock from abc import ABC -from ctypes import * +import ctypes +from typing import TYPE_CHECKING, Union import psutil @@ -26,6 +28,9 @@ from ansys.dpf.core import server_context from ansys.dpf.gate import load_api, data_processing_grpcapi +if TYPE_CHECKING: + from ansys.dpf.core.server_factory import DockerConfig + import logging LOG = logging.getLogger(__name__) @@ -671,17 +676,17 @@ class GrpcServer(CServer): def __init__( self, - ansys_path=None, - ip=LOCALHOST, - port=DPF_DEFAULT_PORT, - timeout=10, - as_global=True, - load_operators=True, - launch_server=True, - docker_config=RUNNING_DOCKER, - use_pypim=True, - num_connection_tryouts=3, - context=server_context.SERVER_CONTEXT, + ansys_path: Union[str, None] = None, + ip: str = LOCALHOST, + port: str = DPF_DEFAULT_PORT, + timeout: float = 10., + as_global: bool = True, + load_operators: bool = True, + launch_server: bool = True, + docker_config: DockerConfig = RUNNING_DOCKER, + use_pypim: bool = True, + num_connection_tryouts: int = 3, + context: server_context.AvailableServerContexts = server_context.SERVER_CONTEXT, ): # Load DPFClientAPI from ansys.dpf.core.misc import is_pypim_configured @@ -733,11 +738,16 @@ def __init__( self.live = True self._create_shutdown_funcs() self._check_first_call(num_connection_tryouts) - try: - self._base_service.initialize_with_context(context) - self._context = context - except errors.DpfVersionNotSupported: - pass + if context: + if context == core.AvailableServerContexts.no_context: + self._base_service.initialize() + self._context = context + else: + try: + self._base_service.initialize_with_context(context) + self._context = context + except errors.DpfVersionNotSupported: + pass self.set_as_global(as_global=as_global) def _check_first_call(self, num_connection_tryouts): @@ -892,11 +902,11 @@ class InProcessServer(CServer): def __init__( self, - ansys_path=None, - as_global=True, - load_operators=True, - timeout=None, - context=server_context.SERVER_CONTEXT, + ansys_path: Union[str, None] = None, + as_global: bool = True, + load_operators: bool = True, + timeout: None = None, + context: server_context.AvailableServerContexts = server_context.SERVER_CONTEXT, ): # Load DPFClientAPI super().__init__(ansys_path=ansys_path, load_operators=load_operators) @@ -914,14 +924,19 @@ def __init__( f"Unable to locate the following file: {path}" ) raise e - try: - self.apply_context(context) - except errors.DpfVersionNotSupported: - self._base_service.initialize_with_context( - server_context.AvailableServerContexts.premium - ) - self._context = server_context.AvailableServerContexts.premium - pass + if context: + if context == core.AvailableServerContexts.no_context: + self._base_service.initialize() + self._context = context + else: + try: + self.apply_context(context) + except errors.DpfVersionNotSupported: + self._base_service.initialize_with_context( + server_context.AvailableServerContexts.premium + ) + self._context = server_context.AvailableServerContexts.premium + pass self.set_as_global(as_global=as_global) # Update the python os.environment if not os.name == "posix": @@ -974,19 +989,20 @@ def config(self): def get_system_path() -> str: """Return the current PATH environment variable value of the system.""" if not os.name == "posix": - windll.kernel32.GetEnvironmentVariableA.argtypes = (c_char_p, c_char_p, c_int) - windll.kernel32.GetEnvironmentVariableA.restype = c_int + ctypes.windll.kernel32.GetEnvironmentVariableA.argtypes = ( + ctypes.c_char_p, ctypes.c_char_p, ctypes.c_int + ) + ctypes.windll.kernel32.GetEnvironmentVariableA.restype = ctypes.c_int name = "PATH" b_name = name.encode("utf-8") size = 32767 - buffer = create_string_buffer(b"", size) - _ = windll.kernel32.GetEnvironmentVariableA(b_name, buffer, size) + buffer = ctypes.create_string_buffer(b"", size) + _ = ctypes.windll.kernel32.GetEnvironmentVariableA(b_name, buffer, size) return buffer.value.decode("utf-8") else: return sys.path - class LegacyGrpcServer(BaseServer): """Provides an instance of the DPF server using InProcess gRPC. Kept for backward-compatibility with dpf servers <0.5.0. @@ -1023,16 +1039,16 @@ class LegacyGrpcServer(BaseServer): def __init__( self, - ansys_path=None, - ip=LOCALHOST, - port=DPF_DEFAULT_PORT, - timeout=5, - as_global=True, - load_operators=True, - launch_server=True, - docker_config=RUNNING_DOCKER, - use_pypim=True, - context=server_context.SERVER_CONTEXT, + ansys_path: Union[str, None] = None, + ip: str = LOCALHOST, + port: str = DPF_DEFAULT_PORT, + timeout: float = 5., + as_global: bool = True, + load_operators: bool = True, + launch_server: bool = True, + docker_config: DockerConfig = RUNNING_DOCKER, + use_pypim: bool = True, + context: server_context.AvailableServerContexts = server_context.SERVER_CONTEXT, ): """Start the DPF server.""" # Use ansys.grpc.dpf @@ -1097,11 +1113,15 @@ def __init__( self._create_shutdown_funcs() check_ansys_grpc_dpf_version(self, timeout) - try: - self._base_service.initialize_with_context(context) - self._context = context - except errors.DpfVersionNotSupported: - pass + if context: + if context == core.AvailableServerContexts.no_context: + self._context = context + else: + try: + self._base_service.initialize_with_context(context) + self._context = context + except errors.DpfVersionNotSupported: + pass self.set_as_global(as_global=as_global) def _create_shutdown_funcs(self): diff --git a/tests/test_service.py b/tests/test_service.py index f3f62bd5d14..cc2bd4591cd 100644 --- a/tests/test_service.py +++ b/tests/test_service.py @@ -481,6 +481,21 @@ def test_context_environment_variable(reset_context_environment_variable): continue +@pytest.mark.skipif( + not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_6_0, + reason="Failures on Windows 231" +) +def test_server_without_context(remote_config_server_type): + """Tests starting a server without a no_context given.""" + server = dpf.core.start_local_server( + as_global=False, + config=remote_config_server_type, + context=dpf.core.AvailableServerContexts.no_context + ) + none_type = dpf.core.AvailableServerContexts.no_context.licensing_context_type + assert server.context.licensing_context_type == none_type + + @pytest.mark.order("last") @pytest.mark.skipif( running_docker or not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_5_0,