Salome HOME
Merge branch 'V7_main'
[modules/kernel.git] / src / KERNEL_PY / salome_notebook.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
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.
8 #
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.
13 #
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
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 #  File   : salome_notebook.py
22 #  Author : Roman NIKOLAEV, Open CASCADE S.A.S.
23 #  Module : SALOME
24 #  $Header:
25 #
26 """
27 Module salome_notebook gives access to Salome Notebook.
28 """
29
30 import salome
31
32 class PseudoStudyForNoteBook(object):
33     
34     def __init__(self, **kwargs):
35         self.kwargs = kwargs
36         pass
37     
38     def GetVariableNames(self):
39         return self.kwargs.keys()
40     
41     def IsVariable(self, variableName):
42         return variableName in self.kwargs
43     
44     def IsReal(self, variableName):
45         val = self.kwargs[variableName]
46         try:
47             float(val)
48             return True
49         except:
50             pass
51         return False
52     
53     IsInteger = IsReal
54     IsBoolean = IsReal
55     
56     def IsString(self, variableName):
57         return not self.IsReal(variableName)
58     
59     def GetString(self, variableName):
60         return self.kwargs[variableName]
61     
62     def GetReal(self, variableName):
63         return float(self.kwargs[variableName])
64     
65     GetInteger = GetReal
66     GetBoolean = GetReal
67     
68     pass
69
70 class NoteBook:
71     
72     def __init__(self, Study):
73         self.myStudy = Study
74     
75     def set(self, variableName, variable):
76         """
77         Create (or modify) variable with name "variableName" 
78         and value equal "theValue".
79         """
80         if type(variable) == float:
81             self.myStudy.SetReal(variableName, variable)
82             
83         elif type(variable) == int:
84             self.myStudy.SetInteger(variableName, variable)
85             
86         elif type(variable) == bool:
87             self.myStudy.SetBoolean(variableName, variable)
88             
89         elif type(variable) == str:
90             self.myStudy.SetString(variableName, variable)
91             
92     def get(self, variableName):
93         """
94         Return value of the variable with name "variableName".
95         """
96         aResult = None
97         if self.myStudy.IsVariable(variableName):
98             
99             if self.myStudy.IsReal(variableName):
100                 aResult = self.myStudy.GetReal(variableName)
101
102             elif self.myStudy.IsInteger(variableName):
103                 aResult = self.myStudy.GetInteger(variableName)
104
105             elif self.myStudy.IsBoolean(variableName):
106                 aResult = self.myStudy.GetBoolean(variableName)
107
108             elif self.myStudy.IsString(variableName):
109                 aResult = self.myStudy.GetString(variableName)
110                 aResult_orig = aResult
111                 l = self.myStudy.GetVariableNames()
112                 l.remove(variableName)
113                 # --
114                 # To avoid the smallest strings to be replaced first,
115                 # the list is sorted by decreasing lengths
116                 # --
117                 l.sort(key=str.__len__)
118                 l.reverse()
119                 for name in l:
120                     if aResult.find(name) >= 0:
121                         val = self.get(name)
122                         aResult = aResult.replace(name, "%s"%(val))
123                         pass
124                     pass
125                 try:
126                     aResult = eval(aResult)
127                 except Exception, e:
128                     msg = str(e)
129                     msg += "\n"
130                     msg += "A problem occurs while parsing "
131                     msg += "the variable %s "%(variableName.__repr__())
132                     msg += "with value %s ..."%(aResult_orig.__repr__())
133                     msg += "\n"
134                     msg += "Please, check your notebook !"
135                     raise Exception(msg)
136                 pass
137                 
138         return aResult
139     
140     def isVariable(self, variableName): 
141         """
142         Return true if variable with name "variableName" 
143         exists in the study, otherwise return false.
144         """
145         return self.myStudy.IsVariable(variableName)
146
147     def setAs(self, variableName, typ):
148         value = self.get(variableName)
149         value = float(typ(value))
150         self.myStudy.SetStringAsDouble(variableName, value)
151         return
152     
153     def setAsReal(self, variableName):
154         self.setAs(variableName, float)
155         return
156     
157     def setAsInteger(self, variableName):
158         self.setAs(variableName, int)
159         return
160     
161     def setAsBool(self, variableName):
162         self.setAs(variableName, bool)
163         return
164     
165     def check(self):
166         for variableName in self.myStudy.GetVariableNames():
167             self.get(variableName)
168             pass
169         return
170     
171     pass
172
173 def checkThisNoteBook(**kwargs):
174     study = PseudoStudyForNoteBook(**kwargs)
175     note_book = NoteBook(study)
176     note_book.check()
177     return
178
179 notebook = NoteBook(salome.myStudy)