ResInsight/ApplicationCode/GrpcInterface/Python/rips/pdmobject.py

162 lines
5.8 KiB
Python
Raw Normal View History

# pylint: disable=no-self-use
"""
ResInsight caf::PdmObject connection module
"""
import rips.generated.PdmObject_pb2 as PdmObject_pb2
import rips.generated.PdmObject_pb2_grpc as PdmObject_pb2_grpc
import rips.generated.Commands_pb2 as Cmd
import rips.generated.Commands_pb2_grpc as CmdRpc
class PdmObject:
"""
Generic ResInsight object. Corresponds to items in the Project Tree
"""
def _execute_command(self, **command_params):
return self._commands.Execute(Cmd.CommandParams(**command_params))
2019-09-19 06:48:03 -05:00
def __init__(self, pb2_object, channel):
self._pb2_object = pb2_object
self._channel = channel
self._pdm_object_stub = PdmObject_pb2_grpc.PdmObjectServiceStub(
self._channel)
self._commands = CmdRpc.CommandsStub(channel)
def address(self):
2019-07-31 04:02:42 -05:00
"""Get the unique address of the PdmObject
2019-07-31 04:02:42 -05:00
Returns:
A 64-bit unsigned integer address
"""
return self._pb2_object.address
2019-09-19 06:48:03 -05:00
def class_keyword(self):
2019-07-31 04:02:42 -05:00
"""Get the class keyword in the ResInsight Data Model for the given PdmObject"""
return self._pb2_object.class_keyword
def keywords(self):
2019-07-31 04:02:42 -05:00
"""Get a list of all parameter keywords available in the object"""
2019-09-19 06:48:03 -05:00
list_of_keywords = []
for keyword in self._pb2_object.parameters:
2019-09-19 06:48:03 -05:00
list_of_keywords.append(keyword)
return list_of_keywords
2019-09-19 06:48:03 -05:00
def print_object_info(self):
2019-07-31 04:02:42 -05:00
"""Print the structure and data content of the PdmObject"""
print("Class Keyword: " + self.class_keyword())
for keyword in self.keywords():
print(keyword + " [" + type(self.get_value(keyword)).__name__ +
"]: " + str(self.get_value(keyword)))
2019-09-19 06:48:03 -05:00
def __to_value(self, value):
if value.lower() == 'false':
return False
if value.lower() == 'true':
return True
try:
int_val = int(value)
return int_val
except ValueError:
try:
float_val = float(value)
return float_val
except ValueError:
# We may have a string. Strip internal start and end quotes
value = value.strip('\"')
if self.__islist(value):
return self.__makelist(value)
return value
2019-09-19 06:48:03 -05:00
def __from_value(self, value):
if isinstance(value, bool):
if value:
return "true"
return "false"
if isinstance(value, list):
2019-09-19 06:48:03 -05:00
list_of_strings = []
for val in value:
2019-09-19 06:48:03 -05:00
list_of_strings.append(self.__from_value('\"' + val + '\"'))
return "[" + ", ".join(list_of_strings) + "]"
return str(value)
2019-09-19 06:48:03 -05:00
def get_value(self, keyword):
2019-07-31 04:02:42 -05:00
"""Get the value associated with the provided keyword
Arguments:
keyword(str): A string containing the parameter keyword
2019-07-31 04:02:42 -05:00
Returns:
The value of the parameter. Can be int, str or list.
"""
value = self._pb2_object.parameters[keyword]
2019-09-19 06:48:03 -05:00
return self.__to_value(value)
def __islist(self, value):
return value.startswith("[") and value.endswith("]")
2019-09-19 06:48:03 -05:00
def __makelist(self, list_string):
list_string = list_string.lstrip("[")
list_string = list_string.rstrip("]")
strings = list_string.split(", ")
values = []
for string in strings:
2019-09-19 06:48:03 -05:00
values.append(self.__to_value(string))
return values
2019-09-19 06:48:03 -05:00
def set_value(self, keyword, value):
2019-07-31 04:02:42 -05:00
"""Set the value associated with the provided keyword
Arguments:
keyword(str): A string containing the parameter keyword
value(varying): A value matching the type of the parameter.
2019-09-19 06:48:03 -05:00
See keyword documentation and/or print_object_info() to find
2019-07-31 04:02:42 -05:00
the correct data type.
"""
self._pb2_object.parameters[keyword] = self.__from_value(value)
2019-09-19 06:48:03 -05:00
def descendants(self, class_keyword):
2019-07-31 04:02:42 -05:00
"""Get a list of all project tree descendants matching the class keyword
Arguments:
2019-09-19 06:48:03 -05:00
class_keyword[str]: A class keyword matching the type of class wanted
2019-07-31 04:02:42 -05:00
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
2019-09-19 06:48:03 -05:00
child_list = []
for pdm_object in object_list:
child_list.append(PdmObject(pdm_object, self._channel))
2019-09-19 06:48:03 -05:00
return child_list
def children(self, child_field):
"""Get a list of all direct project tree children inside the provided child_field
2019-07-31 04:02:42 -05:00
Arguments:
2019-09-19 06:48:03 -05:00
child_field[str]: A field name
2019-07-31 04:02:42 -05:00
Returns:
2019-09-19 06:48:03 -05:00
A list of PdmObjects inside the child_field
2019-07-31 04:02:42 -05:00
"""
request = PdmObject_pb2.PdmChildObjectRequest(object=self._pb2_object,
child_field=child_field)
object_list = self._pdm_object_stub.GetChildPdmObjects(request).objects
2019-09-19 06:48:03 -05:00
child_list = []
for pdm_object in object_list:
child_list.append(PdmObject(pdm_object, self._channel))
2019-09-19 06:48:03 -05:00
return child_list
def ancestor(self, class_keyword):
"""Find the first ancestor that matches the provided class_keyword
2019-07-31 04:02:42 -05:00
Arguments:
2019-09-19 06:48:03 -05:00
class_keyword[str]: A class keyword matching the type of class wanted
2019-07-31 04:02:42 -05:00
"""
request = PdmObject_pb2.PdmParentObjectRequest(
object=self._pb2_object, parent_keyword=class_keyword)
return PdmObject(self._pdm_object_stub.GetAncestorPdmObject(request),
self._channel)
def update(self):
2019-07-31 04:02:42 -05:00
"""Sync all fields from the Python Object to ResInsight"""
self._pdm_object_stub.UpdateExistingPdmObject(self._pb2_object)