def __generate_set_method_chunks(self, array, method_request): index = -1 while index < len(array): chunk = PdmObject_pb2.PdmObjectSetterChunk() if index is -1: chunk.set_request.CopyFrom(PdmObject_pb2.PdmObjectSetterRequest( request=method_request, data_count=len(array))) index += 1 else: actual_chunk_size = min(len(array) - index + 1, self.__chunk_size) if isinstance(array[0], float): chunk.CopyFrom( PdmObject_pb2.PdmObjectSetterChunk(doubles=PdmObject_pb2.DoubleArray(data=array[index:index + actual_chunk_size]))) elif isinstance(array[0], int): chunk.CopyFrom( PdmObject_pb2.PdmObjectSetterChunk(ints=PdmObject_pb2.IntArray(data=array[index:index + actual_chunk_size]))) elif isinstance(array[0], str): chunk.CopyFrom( PdmObject_pb2.PdmObjectSetterChunk(strings=PdmObject_pb2.StringArray(data=array[index:index + actual_chunk_size]))) else: raise Exception("Wrong data type for set method") index += actual_chunk_size yield chunk # Final empty message to signal completion chunk = PdmObject_pb2.PdmObjectSetterChunk() yield chunk
def _call_pdm_method(self, method_name, **kwargs): pb2_params = PdmObject_pb2.PdmObject(class_keyword=method_name) for key, value in kwargs.items(): pb2_params.parameters[snake_to_camel(key)] = self.__convert_to_grpc_value(value) request = PdmObject_pb2.PdmObjectMethodRequest( object=self._pb2_object, method=method_name, params=pb2_params) pb2_object = self._pdm_object_stub.CallPdmObjectMethod(request) child_class_definition = class_from_keyword(pb2_object.class_keyword) if child_class_definition is None: return None pdm_object = child_class_definition(pb2_object=pb2_object, channel=self.channel()) return pdm_object
def _call_set_method(self, method_name, values): method_request = PdmObject_pb2.PdmObjectGetterRequest( object=self._pb2_object, method=method_name) request_iterator = self.__generate_set_method_chunks(values, method_request) reply = self._pdm_object_stub.CallPdmObjectSetter(request_iterator) if reply.accepted_value_count < len(values): raise IndexError
def ancestor(self, class_keyword): """Find the first ancestor that matches the provided class_keyword Arguments: class_keyword[str]: A class keyword matching the type of class wanted """ request = PdmObject_pb2.PdmParentObjectRequest( object=self._pb2_object, parent_keyword=class_keyword) return PdmObject(self._pdm_object_stub.GetAncestorPdmObject(request), self._channel, self._project)
def children(self, child_field): """Get a list of all direct project tree children inside the provided child_field Arguments: child_field[str]: A field name Returns: A list of PdmObjects inside the child_field """ request = PdmObject_pb2.PdmChildObjectRequest(object=self._pb2_object, child_field=child_field) object_list = self._pdm_object_stub.GetChildPdmObjects(request).objects child_list = [] for pdm_object in object_list: child_list.append(PdmObject(pdm_object, self._channel)) return child_list
def children(self, child_field, class_definition=PdmObject): """Get a list of all direct project tree children inside the provided child_field Arguments: child_field[str]: A field name Returns: A list of PdmObjects inside the child_field """ request = PdmObject_pb2.PdmChildObjectRequest(object=self._pb2_object, child_field=child_field) try: object_list = self._pdm_object_stub.GetChildPdmObjects(request).objects return self.__from_pb2_to_pdm_objects(object_list, class_definition) except grpc.RpcError as e: if e.code() == grpc.StatusCode.NOT_FOUND: return [] raise e
def descendants(self, class_keyword): """Get a list of all project tree descendants matching the class keyword Arguments: class_keyword[str]: A class keyword matching the type of class wanted Returns: A list of PdmObjects matching the keyword provided """ request = PdmObject_pb2.PdmDescendantObjectRequest( object=self._pb2_object, child_keyword=class_keyword) object_list = self._pdm_object_stub.GetDescendantPdmObjects( request).objects child_list = [] for pdm_object in object_list: child_list.append(PdmObject(pdm_object, self._channel)) return child_list
def descendants(self, class_definition): """Get a list of all project tree descendants matching the class keyword Arguments: class_definition[class]: A class definition matching the type of class wanted Returns: A list of PdmObjects matching the class_definition """ assert(inspect.isclass(class_definition)) class_keyword = class_definition.__name__ try: request = PdmObject_pb2.PdmDescendantObjectRequest( object=self._pb2_object, child_keyword=class_keyword) object_list = self._pdm_object_stub.GetDescendantPdmObjects( request).objects return self.__from_pb2_to_pdm_objects(object_list, class_definition) except grpc.RpcError as e: if e.code() == grpc.StatusCode.NOT_FOUND: return [] # Valid empty result raise e
def __custom_init__(self, pb2_object, channel): self.__warnings = [] self.__chunk_size = 8160 self._channel = channel # Create stubs if self._channel: self._pdm_object_stub = PdmObject_pb2_grpc.PdmObjectServiceStub(self._channel) self._commands = CmdRpc.CommandsStub(self._channel) if pb2_object is not None: # Copy parameters from ResInsight assert(not isinstance(pb2_object, PdmObject)) self._pb2_object = pb2_object for camel_keyword in self._pb2_object.parameters: snake_keyword = camel_to_snake(camel_keyword) setattr(self, snake_keyword, self.__get_grpc_value(camel_keyword)) else: # Copy parameters from PdmObject defaults self._pb2_object = PdmObject_pb2.PdmObject(class_keyword=self.__class__.__name__) self.__copy_to_pb2()
def ancestor(self, class_definition): """Find the first ancestor that matches the provided class_keyword Arguments: class_definition[class]: A class definition matching the type of class wanted """ assert(inspect.isclass(class_definition)) class_keyword = class_definition.__name__ request = PdmObject_pb2.PdmParentObjectRequest( object=self._pb2_object, parent_keyword=class_keyword) try: pb2_object = self._pdm_object_stub.GetAncestorPdmObject(request) child_class_definition = class_from_keyword(pb2_object.class_keyword) if child_class_definition is None: child_class_definition = class_definition pdm_object = child_class_definition(pb2_object=pb2_object, channel=self.channel()) return pdm_object except grpc.RpcError as e: if e.code() == grpc.StatusCode.NOT_FOUND: return None raise e
def _call_get_method_async(self, method_name): request = PdmObject_pb2.PdmObjectGetterRequest(object=self._pb2_object, method=method_name) for chunk in self._pdm_object_stub.CallPdmObjectGetter(request): yield chunk
def _call_pdm_method(self, method_name, **kwargs): pb2_params = PdmObject_pb2.PdmObject(class_keyword=method_name) for key, value in kwargs.items(): pb2_params.parameters[snake_to_camel(key)] = self.__convert_to_grpc_value(value) request = PdmObject_pb2.PdmObjectMethodRequest(object=self._pb2_object, method=method_name, params=pb2_params) return self._pdm_object_stub.CallPdmObjectMethod(request)