11"""Interface to underlying gRPC Operator"""
2+ from textwrap import wrap
23import logging
34import grpc
45import functools
1112from ansys .dpf .core .inputs import Inputs
1213from ansys .dpf .core .outputs import Outputs
1314from ansys .dpf .core .mapping_types import map_types_to_python
14- from ansys .dpf .core .raw_operators import DPF_HTML_OPERATOR_DOCS
1515from ansys .dpf .core .errors import protect_grpc
1616
1717LOG = logging .getLogger (__name__ )
@@ -63,21 +63,17 @@ def __init__(self, name, channel=None):
6363 self ._description = None
6464 self .inputs = None
6565 self .outputs = None
66- try :
67- self .__send_init_request ()
6866
69- # add dynamic inputs
70- if len (self ._message .spec .map_input_pin_spec ) > 0 :
71- self .inputs = Inputs (self ._message .spec .map_input_pin_spec , self )
72- if len (self ._message .spec .map_output_pin_spec )!= 0 :
73- self .outputs = Outputs (self ._message .spec .map_output_pin_spec , self )
74- self ._description = self ._message .spec .description
67+ self .__send_init_request ()
7568
76- except grpc .RpcError as e :
77- if e .code () == grpc .StatusCode .INVALID_ARGUMENT :
78- raise ValueError (f'Invalid operator name "{ name } "' )
69+ # add dynamic inputs
70+ if len (self ._message .spec .map_input_pin_spec ) > 0 :
71+ self .inputs = Inputs (self ._message .spec .map_input_pin_spec , self )
72+ if len (self ._message .spec .map_output_pin_spec ) != 0 :
73+ self .outputs = Outputs (self ._message .spec .map_output_pin_spec , self )
74+ self ._description = self ._message .spec .description
7975
80- def _add_sub_res_operators (self , sub_results ):
76+ def _add_sub_res_operators (self , sub_results ):
8177 """Dynamically add operators instantiating for sub-results.
8278
8379 The new operators subresults are connected to the parent
@@ -218,19 +214,20 @@ def __del__(self):
218214
219215 def __str__ (self ):
220216 # return this repr and operator one level up
221- txt = f'DPF "{ self .name } " operator \n '
217+ txt = f'DPF "{ self .name } " Operator \n '
222218 if self ._description :
223- line = [' ' ,'description:' , self ._description ]
224- txt += '{:^3} {:^6} {:^15}' .format (* line )
225- txt += '\n '
219+ txt += ' Description:\n '
220+ txt += '\n ' .join (wrap (self ._description , initial_indent = ' ' ,
221+ subsequent_indent = ' ' ))
222+ txt += '\n \n '
226223 if self .inputs :
227- line = [' ' ,self .inputs . __str__ ( )]
228- txt += '{:^3} {:^21}' .format (* line )
229- txt += '\n '
224+ line = [' ' , str ( self .inputs )]
225+ txt += '{:^3} {:^21}' .format (* line )
226+ txt += '\n '
230227 if self .outputs :
231- line = [' ' ,self .outputs . __str__ ( )]
232- txt += '{:^3} {:^21}' .format (* line )
233- txt += '\n '
228+ line = [' ' , str ( self .outputs )]
229+ txt += '{:^3} {:^21}' .format (* line )
230+ txt += '\n '
234231
235232 return txt
236233
@@ -257,18 +254,20 @@ def _find_outputs_corresponding_pins(self, type_names, inpt, pin,
257254 elif python_name == "Any" :
258255 corresponding_pins .append (pin )
259256
257+ @protect_grpc
260258 def _sub_result_op (self , name ):
261259 op = Operator (name )
262- if self .inputs != None :
260+ if self .inputs is not None :
263261 for key in self .inputs ._connected_inputs :
264262 inpt = self .inputs ._connected_inputs [key ]
265263 if type (inpt ).__name__ == 'dict' :
266264 for keyout in inpt :
267265 op .connect (key ,inpt [keyout ],keyout )
268- else :
266+ else :
269267 op .connect (key ,inpt )
270268 return op
271269
270+ @protect_grpc
272271 def __send_init_request (self ):
273272 request = operator_pb2 .OperatorName ()
274273 request .name = self .name
@@ -277,12 +276,12 @@ def __send_init_request(self):
277276 def __mul__ (self , inpt ):
278277 if isinstance (inpt , Operator ):
279278 op = Operator ("dot" )
280- op .connect (0 ,self ,0 )
281- op .connect (1 ,inpt ,0 )
279+ op .connect (0 , self , 0 )
280+ op .connect (1 , inpt , 0 )
282281 elif isinstance (inpt , float ):
283282 op = Operator ("scale" )
284- op .connect (0 ,self ,0 )
285- op .connect (1 ,inpt )
283+ op .connect (0 , self , 0 )
284+ op .connect (1 , inpt )
286285 return op
287286
288287 def __truediv__ (self , inpt ):
0 commit comments