1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2019 CEA/DEN, EDF R&D, OPEN CASCADE
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 # File : salome_notebook.py
22 # Author : Roman NIKOLAEV, Open CASCADE S.A.S.
27 Module salome_notebook gives access to Salome Notebook.
32 class PseudoStudyForNoteBook(object):
34 def __init__(self, **kwargs):
38 def GetVariableNames(self):
39 return list(self.kwargs.keys())
41 def IsVariable(self, variableName):
42 return variableName in self.kwargs
44 def IsReal(self, variableName):
45 val = self.kwargs[variableName]
56 def IsString(self, variableName):
57 return not self.IsReal(variableName)
59 def GetString(self, variableName):
60 return self.kwargs[variableName]
62 def GetReal(self, variableName):
63 return float(self.kwargs[variableName])
72 def __init__(self, theIsEnablePublish = True):
73 if theIsEnablePublish:
74 self.myStudy = salome.myStudy
76 self.myStudy = PseudoStudyForNoteBook()
78 def set(self, variableName, variable):
80 Create (or modify) variable with name "variableName"
81 and value equal "theValue".
83 if isinstance(variable, float):
84 self.myStudy.SetReal(variableName, variable)
86 elif isinstance(variable, int):
87 self.myStudy.SetInteger(variableName, variable)
89 elif isinstance(variable, bool):
90 self.myStudy.SetBoolean(variableName, variable)
92 elif isinstance(variable, str):
93 self.myStudy.SetString(variableName, variable)
95 def get(self, variableName):
97 Return value of the variable with name "variableName".
100 if self.myStudy.IsVariable(variableName):
102 if self.myStudy.IsReal(variableName):
103 aResult = self.myStudy.GetReal(variableName)
105 elif self.myStudy.IsInteger(variableName):
106 aResult = self.myStudy.GetInteger(variableName)
108 elif self.myStudy.IsBoolean(variableName):
109 aResult = self.myStudy.GetBoolean(variableName)
111 elif self.myStudy.IsString(variableName):
112 aResult = self.myStudy.GetString(variableName)
113 aResult_orig = aResult
114 l = self.myStudy.GetVariableNames()
115 l.remove(variableName)
117 # To avoid the smallest strings to be replaced first,
118 # the list is sorted by decreasing lengths
120 l.sort(key=str.__len__)
123 if aResult.find(name) >= 0:
125 aResult = aResult.replace(name, "%s"%(val))
129 aResult = eval(aResult)
130 except Exception as e:
133 msg += "A problem occurs while parsing "
134 msg += "the variable %s "%(variableName.__repr__())
135 msg += "with value %s ..."%(aResult_orig.__repr__())
137 msg += "Please, check your notebook !"
143 def isVariable(self, variableName):
145 Return true if variable with name "variableName"
146 exists in the study, otherwise return false.
148 return self.myStudy.IsVariable(variableName)
150 def setAs(self, variableName, typ):
151 value = self.get(variableName)
152 value = float(typ(value))
153 self.myStudy.SetStringAsDouble(variableName, value)
156 def setAsReal(self, variableName):
157 self.setAs(variableName, float)
160 def setAsInteger(self, variableName):
161 self.setAs(variableName, int)
164 def setAsBool(self, variableName):
165 self.setAs(variableName, bool)
169 for variableName in self.myStudy.GetVariableNames():
170 self.get(variableName)
176 def checkThisNoteBook(**kwargs):
177 note_book = NoteBook( False )
181 notebook = NoteBook()