Files
ResInsight/ApplicationCode/GrpcInterface/Python/rips/Project.py
2019-07-15 12:30:31 +02:00

116 lines
3.1 KiB
Python

import grpc
import os
import sys
from .Case import Case
from .Commands import Commands
from .PdmObject import PdmObject
from .View import View
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'generated'))
from Empty_pb2 import Empty
import Project_pb2
import Project_pb2_grpc
class Project (PdmObject):
"""ResInsight project. Not intended to be created separately.
Automatically created and assigned to Instance.
"""
def __init__(self, channel):
self.channel = channel
self.project = Project_pb2_grpc.ProjectStub(channel)
PdmObject.__init__(self, self.project.GetPdmObject(Empty()), self.channel)
def open(self, path):
"""Open a new project from the given path
Arguments:
path(str): path to project file
"""
Commands(self.channel).openProject(path)
return self
def close(self):
"""Close the current project (and open new blank project)"""
Commands(self.channel).closeProject()
def selectedCases(self):
"""Get a list of all cases selected in the project tree
Returns:
A list of rips Case objects
"""
caseInfos = self.project.GetSelectedCases(Empty())
cases = []
for caseInfo in caseInfos.data:
cases.append(Case(self.channel, caseInfo.id))
return cases
def cases(self):
"""Get a list of all cases in the project
Returns:
A list of rips Case objects
"""
try:
caseInfos = self.project.GetAllCases(Empty())
cases = []
for caseInfo in caseInfos.data:
cases.append(Case(self.channel, caseInfo.id))
return cases
except grpc.RpcError as e:
if e.code() == grpc.StatusCode.NOT_FOUND:
return []
else:
print("ERROR: ", e)
return []
def case(self, id):
"""Get a specific case from the provided case Id
Arguments:
id(int): case id
Returns:
A rips Case object
"""
try:
case = Case(self.channel, id)
return case
except grpc.RpcError as e:
return None
def loadCase(self, path):
"""Load a new case from the given file path
Arguments:
path(str): file path to case
Returns:
A rips Case object
"""
return Commands(self.channel).loadCase(path)
def views(self):
"""Get a list of views belonging to a project"""
pdmObjects = self.descendants("ReservoirView")
viewList = []
for pdmObject in pdmObjects:
viewList.append(View(pdmObject))
return viewList
def view(self, id):
"""Get a particular view belonging to a case by providing view id
Arguments:
id(int): view id
Returns: a view object
"""
views = self.views()
for viewObject in views:
if viewObject.id == id:
return viewObject
return None