5050from ansys .dpf .core import __version__ , errors , server_context , server_factory
5151from ansys .dpf .core ._version import min_server_version , server_to_ansys_version
5252from ansys .dpf .core .check_version import server_meet_version
53+ from ansys .dpf .core .server_context import AvailableServerContexts , ServerContext
5354from ansys .dpf .gate import data_processing_grpcapi , load_api
5455
5556if TYPE_CHECKING : # pragma: no cover
@@ -114,7 +115,11 @@ def _verify_ansys_path_is_valid(ansys_path, executable, path_in_install=None):
114115
115116
116117def _run_launch_server_process (
117- ip , port , ansys_path = None , docker_config = server_factory .RunningDockerConfig ()
118+ ip ,
119+ port ,
120+ ansys_path = None ,
121+ docker_config = server_factory .RunningDockerConfig (),
122+ context : ServerContext = None ,
118123):
119124 bShell = False
120125 if docker_config .use_docker :
@@ -127,13 +132,25 @@ def _run_launch_server_process(
127132 if os .name == "nt" :
128133 executable = "Ans.Dpf.Grpc.bat"
129134 run_cmd = f"{ executable } --address { ip } --port { port } "
135+ if context not in (
136+ None ,
137+ AvailableServerContexts .entry ,
138+ AvailableServerContexts .premium ,
139+ ):
140+ run_cmd += f" --context { int (context .licensing_context_type )} "
130141 else :
131142 executable = "./Ans.Dpf.Grpc.sh" # pragma: no cover
132143 run_cmd = [
133144 executable ,
134145 f"--address { ip } " ,
135146 f"--port { port } " ,
136147 ] # pragma: no cover
148+ if context not in (
149+ None ,
150+ AvailableServerContexts .entry ,
151+ AvailableServerContexts .premium ,
152+ ):
153+ run_cmd .append (f"--context { int (context .licensing_context_type )} " )
137154 path_in_install = load_api ._get_path_in_install (internal_folder = "bin" )
138155 dpf_run_dir = _verify_ansys_path_is_valid (ansys_path , executable , path_in_install )
139156
@@ -204,7 +221,9 @@ def read_stdout():
204221 raise RuntimeError (errstr )
205222
206223
207- def launch_dpf (ansys_path , ip = LOCALHOST , port = DPF_DEFAULT_PORT , timeout = 10 ):
224+ def launch_dpf (
225+ ansys_path , ip = LOCALHOST , port = DPF_DEFAULT_PORT , timeout = 10 , context : ServerContext = None
226+ ):
208227 """Launch Ansys DPF.
209228
210229 Parameters
@@ -222,9 +241,10 @@ def launch_dpf(ansys_path, ip=LOCALHOST, port=DPF_DEFAULT_PORT, timeout=10):
222241 Maximum number of seconds for the initialization attempt.
223242 The default is ``10``. Once the specified number of seconds
224243 passes, the connection fails.
225-
244+ context : , optional
245+ Context to apply to DPF server when launching it.
226246 """
227- process = _run_launch_server_process (ip , port , ansys_path )
247+ process = _run_launch_server_process (ip , port , ansys_path , context = context )
228248 lines = []
229249 current_errors = []
230250 _wait_and_check_server_connection (
@@ -766,7 +786,7 @@ def __init__(
766786 launch_server : bool = True ,
767787 docker_config : DockerConfig = RUNNING_DOCKER ,
768788 use_pypim : bool = True ,
769- context : server_context .AvailableServerContexts = server_context .SERVER_CONTEXT ,
789+ context : server_context .ServerContext = server_context .SERVER_CONTEXT ,
770790 ):
771791 # Load DPFClientAPI
772792 from ansys .dpf .core .misc import is_pypim_configured
@@ -808,7 +828,7 @@ def __init__(
808828 timeout = timeout ,
809829 )
810830 else :
811- launch_dpf (ansys_path , ip , port , timeout = timeout )
831+ launch_dpf (ansys_path , ip , port , timeout = timeout , context = context )
812832 self ._local_server = True
813833
814834 # store port and ip for later reference
@@ -820,15 +840,11 @@ def __init__(
820840 self ._create_shutdown_funcs ()
821841 self ._check_first_call (timeout = timeout - (time .time () - start_time )) # Pass remaining time
822842 if context :
823- if context == core . AvailableServerContexts . no_context :
824- self ._base_service .initialize ( )
843+ try :
844+ self ._base_service .initialize_with_context ( context )
825845 self ._context = context
826- else :
827- try :
828- self ._base_service .initialize_with_context (context )
829- self ._context = context
830- except errors .DpfVersionNotSupported :
831- pass
846+ except errors .DpfVersionNotSupported :
847+ pass
832848 self .set_as_global (as_global = as_global )
833849
834850 def _check_first_call (self , timeout : float ):
@@ -1039,18 +1055,14 @@ def __init__(
10391055 )
10401056 raise e
10411057 if context :
1042- if context == core .AvailableServerContexts .no_context :
1043- self ._base_service .initialize ()
1044- self ._context = context
1045- else :
1046- try :
1047- self .apply_context (context )
1048- except errors .DpfVersionNotSupported :
1049- self ._base_service .initialize_with_context (
1050- server_context .AvailableServerContexts .premium
1051- )
1052- self ._context = server_context .AvailableServerContexts .premium
1053- pass
1058+ try :
1059+ self .apply_context (context )
1060+ except errors .DpfVersionNotSupported :
1061+ self ._base_service .initialize_with_context (
1062+ server_context .AvailableServerContexts .premium
1063+ )
1064+ self ._context = server_context .AvailableServerContexts .premium
1065+ pass
10541066 self .set_as_global (as_global = as_global )
10551067 # Update the python os.environment
10561068 if not os .name == "posix" :
@@ -1201,7 +1213,7 @@ def __init__(
12011213 launch_server : bool = True ,
12021214 docker_config : DockerConfig = RUNNING_DOCKER ,
12031215 use_pypim : bool = True ,
1204- context : server_context .AvailableServerContexts = server_context .SERVER_CONTEXT ,
1216+ context : server_context .ServerContext = server_context .SERVER_CONTEXT ,
12051217 ):
12061218 """Start the DPF server."""
12071219 # Use ansys.grpc.dpf
@@ -1247,7 +1259,7 @@ def __init__(
12471259 timeout = timeout ,
12481260 )
12491261 else :
1250- launch_dpf (ansys_path , ip , port , timeout = timeout )
1262+ launch_dpf (ansys_path , ip , port , timeout = timeout , context = context )
12511263 self ._local_server = True
12521264 from ansys .dpf .core import misc , settings
12531265
@@ -1267,14 +1279,11 @@ def __init__(
12671279
12681280 check_ansys_grpc_dpf_version (self , timeout )
12691281 if context :
1270- if context == core .AvailableServerContexts .no_context :
1282+ try :
1283+ self ._base_service .initialize_with_context (context )
12711284 self ._context = context
1272- else :
1273- try :
1274- self ._base_service .initialize_with_context (context )
1275- self ._context = context
1276- except errors .DpfVersionNotSupported :
1277- pass
1285+ except errors .DpfVersionNotSupported :
1286+ pass
12781287 self .set_as_global (as_global = as_global )
12791288
12801289 def _create_shutdown_funcs (self ):
0 commit comments