From 467ba81d8d6426a28e396768518e15395513a17e Mon Sep 17 00:00:00 2001 From: asl Date: Fri, 20 Nov 2009 07:52:37 +0000 Subject: [PATCH] Base python interface for notebook --- src/KERNEL_PY/salome_notebook.py | 172 +++++++++++++++++-------------- 1 file changed, 94 insertions(+), 78 deletions(-) diff --git a/src/KERNEL_PY/salome_notebook.py b/src/KERNEL_PY/salome_notebook.py index 92e49c991..246247e18 100644 --- a/src/KERNEL_PY/salome_notebook.py +++ b/src/KERNEL_PY/salome_notebook.py @@ -29,6 +29,7 @@ Module salome_notebook gives access to Salome Notebook. """ import salome +import SALOME class PseudoStudyForNoteBook(object): @@ -71,105 +72,120 @@ class PseudoStudyForNoteBook(object): class NoteBook: def __init__(self, Study): - self.myStudy = Study - + self.myNotebook = Study.GetNotebook() + + def getNotebook( self ): + return self.myNotebook + + def update( self ): + self.myNotebook.Update() + def set(self, variableName, variable): """ Create (or modify) variable with name "variableName" and value equal "theValue". """ + + aParam = self.myNotebook.Param(variableName) if type(variable) == float: - self.myStudy.SetReal(variableName, variable) - - elif type(variable) == int: - self.myStudy.SetInteger(variableName, variable) + if aParam == None: + self.myNotebook.AddValue(variableName, variable) + else: + aParam.SetReal(variable) + + elif type(variable) == str: + if aParam == None: + self.myStudy.AddExpr(variableName, variable) + else: + aParam.SetExpr(variable) - elif type(variable) == bool: - self.myStudy.SetBoolean(variableName, variable) + #elif type(variable) == int: + # self.myStudy.SetInteger(variableName, variable) - elif type(variable) == str: - self.myStudy.SetString(variableName, variable) + #elif type(variable) == bool: + # self.myStudy.SetBoolean(variableName, variable) def get(self, variableName): """ Return value of the variable with name "variableName". """ aResult = None - if self.myStudy.IsVariable(variableName): - - if self.myStudy.IsReal(variableName): - aResult = self.myStudy.GetReal(variableName) + aParam = self.myNotebook.Param(variableName) + if aParam != None: - elif self.myStudy.IsInteger(variableName): - aResult = self.myStudy.GetInteger(variableName) + if aParam.GetType() == SALOME.TReal: + aResult = aParam.AsReal() - elif self.myStudy.IsBoolean(variableName): - aResult = self.myStudy.GetBoolean(variableName) + #elif self.myStudy.IsInteger(variableName): + # aResult = self.myStudy.GetInteger(variableName) - elif self.myStudy.IsString(variableName): - aResult = self.myStudy.GetString(variableName) - aResult_orig = aResult - l = self.myStudy.GetVariableNames() - l.remove(variableName) - # -- - # To avoid the smallest strings to be replaced first, - # the list is sorted by decreasing lengths - # -- - l.sort(key=str.__len__) - l.reverse() - for name in l: - if aResult.find(name) >= 0: - val = self.get(name) - aResult = aResult.replace(name, "%s"%(val)) - pass - pass - try: - aResult = eval(aResult) - except Exception, e: - msg = str(e) - msg += "\n" - msg += "A problem occurs while parsing " - msg += "the variable %s "%(variableName.__repr__()) - msg += "with value %s ..."%(aResult_orig.__repr__()) - msg += "\n" - msg += "Please, check your notebook !" - raise Exception(msg) + #elif self.myStudy.IsBoolean(variableName): + # aResult = self.myStudy.GetBoolean(variableName) + + #elif self.myStudy.IsString(variableName): + # aResult = self.myStudy.GetString(variableName) + # aResult_orig = aResult + # l = self.myStudy.GetVariableNames() + # l.remove(variableName) + # # -- + # # To avoid the smallest strings to be replaced first, + # # the list is sorted by decreasing lengths + # # -- + # l.sort(key=str.__len__) + # l.reverse() + # for name in l: + # if aResult.find(name) >= 0: + # val = self.get(name) + # aResult = aResult.replace(name, "%s"%(val)) + # pass + # pass + # try: + # aResult = eval(aResult) + # except Exception, e: + # msg = str(e) + # msg += "\n" + # msg += "A problem occurs while parsing " + # msg += "the variable %s "%(variableName.__repr__()) + # msg += "with value %s ..."%(aResult_orig.__repr__()) + # msg += "\n" + # msg += "Please, check your notebook !" + # raise Exception(msg) pass return aResult - def isVariable(self, variableName): - """ - Return true if variable with name "variableName" - exists in the study, otherwise return false. - """ - return self.myStudy.IsVariable(variableName) - - def setAs(self, variableName, typ): - value = self.get(variableName) - value = float(typ(value)) - self.myStudy.SetStringAsDouble(variableName, value) - return - - def setAsReal(self, variableName): - self.setAs(variableName, float) - return - - def setAsInteger(self, variableName): - self.setAs(variableName, int) - return - - def setAsBool(self, variableName): - self.setAs(variableName, bool) - return - - def check(self): - for variableName in self.myStudy.GetVariableNames(): - self.get(variableName) - pass - return - - pass +# def isVariable(self, variableName): +# """ +# Return true if variable with name "variableName" +# exists in the study, otherwise return false. +# """ +# return self.myStudy.IsVariable(variableName) +# +# def setAs(self, variableName, typ): +# value = self.get(variableName) +# value = float(typ(value)) +# self.myStudy.SetStringAsDouble(variableName, value) +# return +# +# def setAsReal(self, variableName): +# self.setAs(variableName, float) +# return +# +# def setAsInteger(self, variableName): +# self.setAs(variableName, int) +# return +# +# def setAsBool(self, variableName): +# self.setAs(variableName, bool) +# return +# +# def check(self): +# for variableName in self.myStudy.GetVariableNames(): +# self.get(variableName) +# pass +# return +# +# pass def checkThisNoteBook(**kwargs): study = PseudoStudyForNoteBook(**kwargs) -- 2.39.2