1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2008 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
3 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 # File : salome_notebook.py
24 # Author : Roman NIKOLAEV, Open CASCADE S.A.S.
28 Module salome_notebook gives access to Salome Notebook.
33 class PseudoStudyForNoteBook(object):
35 def __init__(self, **kwargs):
39 def GetVariableNames(self):
40 return self.kwargs.keys()
42 def IsVariable(self, variableName):
43 return variableName in self.kwargs
45 def IsReal(self, variableName):
46 val = self.kwargs[variableName]
57 def IsString(self, variableName):
58 return not self.IsReal(variableName)
60 def GetString(self, variableName):
61 return self.kwargs[variableName]
63 def GetReal(self, variableName):
64 return float(self.kwargs[variableName])
73 def __init__(self, Study):
76 def set(self, variableName, variable):
78 Create (or modify) variable with name "variableName"
79 and value equal "theValue".
81 if type(variable) == float:
82 self.myStudy.SetReal(variableName, variable)
84 elif type(variable) == int:
85 self.myStudy.SetInteger(variableName, variable)
87 elif type(variable) == bool:
88 self.myStudy.SetBoolean(variableName, variable)
90 elif type(variable) == str:
91 self.myStudy.SetString(variableName, variable)
93 def get(self, variableName):
95 Return value of the variable with name "variableName".
98 if self.myStudy.IsVariable(variableName):
100 if self.myStudy.IsReal(variableName):
101 aResult = self.myStudy.GetReal(variableName)
103 elif self.myStudy.IsInteger(variableName):
104 aResult = self.myStudy.GetInteger(variableName)
106 elif self.myStudy.IsBoolean(variableName):
107 aResult = self.myStudy.GetBoolean(variableName)
109 elif self.myStudy.IsString(variableName):
110 aResult = self.myStudy.GetString(variableName)
111 aResult_orig = aResult
112 l = self.myStudy.GetVariableNames()
113 l.remove(variableName)
115 # To avoid the smallest strings to be replaced first,
116 # the list is sorted by decreasing lengths
118 l.sort(key=str.__len__)
121 if aResult.find(name) >= 0:
123 aResult = aResult.replace(name, "%s"%(val))
127 aResult = eval(aResult)
131 msg += "A problem occurs while parsing "
132 msg += "the variable %s "%(variableName.__repr__())
133 msg += "with value %s ..."%(aResult_orig.__repr__())
135 msg += "Please, check your notebook !"
141 def isVariable(self, variableName):
143 Return true if variable with name "variableName"
144 exists in the study, otherwise return false.
146 return self.myStudy.IsVariable(variableName)
148 def setAs(self, variableName, typ):
149 value = self.get(variableName)
150 value = float(typ(value))
151 self.myStudy.SetStringAsDouble(variableName, value)
154 def setAsReal(self, variableName):
155 self.setAs(variableName, float)
158 def setAsInteger(self, variableName):
159 self.setAs(variableName, int)
162 def setAsBool(self, variableName):
163 self.setAs(variableName, bool)
167 for variableName in self.myStudy.GetVariableNames():
168 self.get(variableName)
174 def checkThisNoteBook(**kwargs):
175 study = PseudoStudyForNoteBook(**kwargs)
176 note_book = NoteBook(study)
180 notebook = NoteBook(salome.myStudy)