2019-09-23 04:50:33 -05:00
|
|
|
# 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
|
2019-06-10 13:42:20 -05:00
|
|
|
|
|
|
|
|
|
|
|
class PdmObject:
|
2019-09-23 04:50:33 -05:00
|
|
|
"""
|
|
|
|
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):
|
2019-09-23 04:50:33 -05:00
|
|
|
self._pb2_object = pb2_object
|
|
|
|
self._channel = channel
|
|
|
|
self._pdm_object_stub = PdmObject_pb2_grpc.PdmObjectServiceStub(
|
|
|
|
self._channel)
|
|
|
|
self._commands = CmdRpc.CommandsStub(channel)
|
|
|
|
|
2019-09-23 08:26:49 -05:00
|
|
|
def pb2_object(self):
|
|
|
|
""" Private method"""
|
|
|
|
return self._pb2_object
|
|
|
|
|
|
|
|
def channel(self):
|
|
|
|
""" Private method"""
|
|
|
|
return self._channel
|
|
|
|
|
2019-06-10 13:42:20 -05:00
|
|
|
def address(self):
|
2019-07-31 04:02:42 -05:00
|
|
|
"""Get the unique address of the PdmObject
|
2019-09-23 04:50:33 -05:00
|
|
|
|
2019-07-31 04:02:42 -05:00
|
|
|
Returns:
|
|
|
|
A 64-bit unsigned integer address
|
|
|
|
"""
|
|
|
|
|
2019-09-23 04:50:33 -05:00
|
|
|
return self._pb2_object.address
|
2019-06-10 13:42:20 -05:00
|
|
|
|
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"""
|
2019-09-23 04:50:33 -05:00
|
|
|
return self._pb2_object.class_keyword
|
2019-06-10 13:42:20 -05:00
|
|
|
|
|
|
|
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 = []
|
2019-09-23 04:50:33 -05:00
|
|
|
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-23 04:50:33 -05:00
|
|
|
|
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"""
|
2019-09-23 04:50:33 -05:00
|
|
|
print("Class Keyword: " + self.class_keyword())
|
2019-07-30 08:26:38 -05:00
|
|
|
for keyword in self.keywords():
|
2019-09-23 04:50:33 -05:00
|
|
|
print(keyword + " [" + type(self.get_value(keyword)).__name__ +
|
|
|
|
"]: " + str(self.get_value(keyword)))
|
2019-07-30 08:26:38 -05:00
|
|
|
|
2019-09-19 06:48:03 -05:00
|
|
|
def __to_value(self, value):
|
2019-06-10 13:42:20 -05:00
|
|
|
if value.lower() == 'false':
|
2019-09-23 04:50:33 -05:00
|
|
|
return False
|
|
|
|
if value.lower() == 'true':
|
2019-06-10 13:42:20 -05:00
|
|
|
return True
|
2019-09-23 04:50:33 -05:00
|
|
|
try:
|
|
|
|
int_val = int(value)
|
|
|
|
return int_val
|
|
|
|
except ValueError:
|
2019-06-10 13:42:20 -05:00
|
|
|
try:
|
2019-09-23 04:50:33 -05:00
|
|
|
float_val = float(value)
|
|
|
|
return float_val
|
2019-06-10 13:42:20 -05:00
|
|
|
except ValueError:
|
2019-09-23 04:50:33 -05:00
|
|
|
# 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):
|
2019-06-10 13:42:20 -05:00
|
|
|
if isinstance(value, bool):
|
|
|
|
if value:
|
2019-07-18 09:01:11 -05:00
|
|
|
return "true"
|
2019-09-23 04:50:33 -05:00
|
|
|
return "false"
|
|
|
|
if isinstance(value, list):
|
2019-09-19 06:48:03 -05:00
|
|
|
list_of_strings = []
|
2019-07-18 09:01:11 -05:00
|
|
|
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) + "]"
|
2019-09-23 04:50:33 -05:00
|
|
|
return str(value)
|
2019-07-18 09:01:11 -05:00
|
|
|
|
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-09-23 04:50:33 -05:00
|
|
|
|
2019-07-31 04:02:42 -05:00
|
|
|
Returns:
|
|
|
|
The value of the parameter. Can be int, str or list.
|
|
|
|
"""
|
2019-09-23 04:50:33 -05:00
|
|
|
value = self._pb2_object.parameters[keyword]
|
2019-09-19 06:48:03 -05:00
|
|
|
return self.__to_value(value)
|
2019-07-18 09:01:11 -05:00
|
|
|
|
|
|
|
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(", ")
|
2019-07-18 09:01:11 -05:00
|
|
|
values = []
|
|
|
|
for string in strings:
|
2019-09-19 06:48:03 -05:00
|
|
|
values.append(self.__to_value(string))
|
2019-07-18 09:01:11 -05:00
|
|
|
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.
|
|
|
|
"""
|
2019-09-23 04:50:33 -05:00
|
|
|
self._pb2_object.parameters[keyword] = self.__from_value(value)
|
2019-06-10 13:42:20 -05:00
|
|
|
|
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
|
|
|
|
"""
|
2019-09-23 04:50:33 -05:00
|
|
|
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 = []
|
2019-09-23 04:50:33 -05:00
|
|
|
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
|
|
|
"""
|
2019-09-23 04:50:33 -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 = []
|
2019-09-23 04:50:33 -05:00
|
|
|
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
|
|
|
"""
|
2019-09-23 04:50:33 -05:00
|
|
|
request = PdmObject_pb2.PdmParentObjectRequest(
|
|
|
|
object=self._pb2_object, parent_keyword=class_keyword)
|
|
|
|
return PdmObject(self._pdm_object_stub.GetAncestorPdmObject(request),
|
|
|
|
self._channel)
|
2019-06-10 13:42:20 -05:00
|
|
|
|
|
|
|
def update(self):
|
2019-07-31 04:02:42 -05:00
|
|
|
"""Sync all fields from the Python Object to ResInsight"""
|
2019-09-23 04:50:33 -05:00
|
|
|
self._pdm_object_stub.UpdateExistingPdmObject(self._pb2_object)
|