Salome HOME
Porting to Python 2.6 - add coding page specification for Python scripts
[modules/kernel.git] / src / KERNEL_PY / salome_notebook.py
1 #! /usr/bin/python
2 #  -*- coding: iso-8859-1 -*-
3 #  Copyright (C) 2008  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5
6 #  This library is free software; you can redistribute it and/or 
7 #  modify it under the terms of the GNU Lesser General Public 
8 #  License as published by the Free Software Foundation; either 
9 #  version 2.1 of the License. 
10
11 #  This library is distributed in the hope that it will be useful, 
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 #  Lesser General Public License for more details. 
15
16 #  You should have received a copy of the GNU Lesser General Public 
17 #  License along with this library; if not, write to the Free Software 
18 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19
20 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #
22 #
23 #
24 #  File   : salome_notebook.py
25 #  Author : Roman NIKOLAEV, Open CASCADE S.A.S.
26 #  Module : SALOME
27 #  $Header:
28 """
29 Module salome_notebook gives access to Salome Notebook.
30 """
31
32 import salome
33
34 class PseudoStudyForNoteBook(object):
35     
36     def __init__(self, **kwargs):
37         self.kwargs = kwargs
38         pass
39     
40     def GetVariableNames(self):
41         return self.kwargs.keys()
42     
43     def IsVariable(self, variableName):
44         return variableName in self.kwargs
45     
46     def IsReal(self, variableName):
47         val = self.kwargs[variableName]
48         try:
49             float(val)
50             return True
51         except:
52             pass
53         return False
54     
55     IsInteger = IsReal
56     IsBoolean = IsReal
57     
58     def IsString(self, variableName):
59         return not self.IsReal(variableName)
60     
61     def GetString(self, variableName):
62         return self.kwargs[variableName]
63     
64     def GetReal(self, variableName):
65         return float(self.kwargs[variableName])
66     
67     GetInteger = GetReal
68     GetBoolean = GetReal
69     
70     pass
71
72 class NoteBook:
73     
74     def __init__(self, Study):
75         self.myStudy = Study
76     
77     def set(self, variableName, variable):
78         """
79         Create (or modify) variable with name "variableName" 
80         and value equal "theValue".
81         """
82         if type(variable) == float:
83             self.myStudy.SetReal(variableName, variable)
84             
85         elif type(variable) == int:
86             self.myStudy.SetInteger(variableName, variable)
87             
88         elif type(variable) == bool:
89             self.myStudy.SetBoolean(variableName, variable)
90             
91         elif type(variable) == str:
92             self.myStudy.SetString(variableName, variable)
93             
94     def get(self, variableName):
95         """
96         Return value of the variable with name "variableName".
97         """
98         aResult = None
99         if self.myStudy.IsVariable(variableName):
100             
101             if self.myStudy.IsReal(variableName):
102                 aResult = self.myStudy.GetReal(variableName)
103
104             elif self.myStudy.IsInteger(variableName):
105                 aResult = self.myStudy.GetInteger(variableName)
106
107             elif self.myStudy.IsBoolean(variableName):
108                 aResult = self.myStudy.GetBoolean(variableName)
109
110             elif self.myStudy.IsString(variableName):
111                 aResult = self.myStudy.GetString(variableName)
112                 aResult_orig = aResult
113                 l = self.myStudy.GetVariableNames()
114                 l.remove(variableName)
115                 # --
116                 # To avoid the smallest strings to be replaced first,
117                 # the list is sorted by decreasing lengths
118                 # --
119                 l.sort(key=str.__len__)
120                 l.reverse()
121                 for name in l:
122                     if aResult.find(name) >= 0:
123                         val = self.get(name)
124                         aResult = aResult.replace(name, "%s"%(val))
125                         pass
126                     pass
127                 try:
128                     aResult = eval(aResult)
129                 except Exception, e:
130                     msg = str(e)
131                     msg += "\n"
132                     msg += "A problem occurs while parsing "
133                     msg += "the variable %s "%(variableName.__repr__())
134                     msg += "with value %s ..."%(aResult_orig.__repr__())
135                     msg += "\n"
136                     msg += "Please, check your notebook !"
137                     raise Exception(msg)
138                 pass
139                 
140         return aResult
141     
142     def isVariable(self, variableName): 
143         """
144         Return true if variable with name "variableName" 
145         exists in the study, otherwise return false.
146         """
147         return self.myStudy.IsVariable(variableName)
148
149     def setAs(self, variableName, typ):
150         value = self.get(variableName)
151         value = float(typ(value))
152         self.myStudy.SetStringAsDouble(variableName, value)
153         return
154     
155     def setAsReal(self, variableName):
156         self.setAs(variableName, float)
157         return
158     
159     def setAsInteger(self, variableName):
160         self.setAs(variableName, int)
161         return
162     
163     def setAsBool(self, variableName):
164         self.setAs(variableName, bool)
165         return
166     
167     def check(self):
168         for variableName in self.myStudy.GetVariableNames():
169             self.get(variableName)
170             pass
171         return
172     
173     pass
174
175 def checkThisNoteBook(**kwargs):
176     study = PseudoStudyForNoteBook(**kwargs)
177     note_book = NoteBook(study)
178     note_book.check()
179     return
180
181 notebook = NoteBook(salome.myStudy)