Salome HOME
updated copyright message
[modules/kernel.git] / src / KERNEL_PY / salome_notebook.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2023  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:
33
34     def __init__(self, **kwargs):
35         self.kwargs = kwargs
36         pass
37
38     def GetVariableNames(self):
39         return list(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 Exception:
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, theIsEnablePublish=True):
73         if theIsEnablePublish:
74             self.myStudy = salome.myStudy
75         else:
76             self.myStudy = PseudoStudyForNoteBook()
77
78     def set(self, variableName, variable):
79         """
80         Create (or modify) variable with name "variableName"
81         and value equal "theValue".
82         """
83         if isinstance(variable, float):
84             self.myStudy.SetReal(variableName, variable)
85
86         elif isinstance(variable, int):
87             self.myStudy.SetInteger(variableName, variable)
88
89         elif isinstance(variable, bool):
90             self.myStudy.SetBoolean(variableName, variable)
91
92         elif isinstance(variable, str):
93             self.myStudy.SetString(variableName, variable)
94
95     def get(self, variableName):
96         """
97         Return value of the variable with name "variableName".
98         """
99         aResult = None
100         if self.myStudy.IsVariable(variableName):
101
102             if self.myStudy.IsReal(variableName):
103                 aResult = self.myStudy.GetReal(variableName)
104
105             elif self.myStudy.IsInteger(variableName):
106                 aResult = self.myStudy.GetInteger(variableName)
107
108             elif self.myStudy.IsBoolean(variableName):
109                 aResult = self.myStudy.GetBoolean(variableName)
110
111             elif self.myStudy.IsString(variableName):
112                 aResult = self.myStudy.GetString(variableName)
113                 aResult_orig = aResult
114                 list_of_variables = self.myStudy.GetVariableNames()
115                 list_of_variables.remove(variableName)
116                 # --
117                 # To avoid the smallest strings to be replaced first,
118                 # the list is sorted by decreasing lengths
119                 # --
120                 list_of_variables.sort(key=str.__len__)
121                 list_of_variables.reverse()
122                 for name in list_of_variables:
123                     if aResult.find(name) >= 0:
124                         val = self.get(name)
125                         aResult = aResult.replace(name, "%s" % (val))
126                         pass
127                     pass
128                 try:
129                     aResult = eval(aResult)
130                 except Exception as e:
131                     msg = str(e)
132                     msg += "\n"
133                     msg += "A problem occurs while parsing "
134                     msg += "the variable %s " % (variableName.__repr__())
135                     msg += "with value %s ..." % (aResult_orig.__repr__())
136                     msg += "\n"
137                     msg += "Please, check your notebook !"
138                     raise Exception(msg)
139                 pass
140
141         return aResult
142
143     def isVariable(self, variableName):
144         """
145         Return true if variable with name "variableName"
146         exists in the study, otherwise return false.
147         """
148         return self.myStudy.IsVariable(variableName)
149
150     def setAs(self, variableName, typ):
151         value = self.get(variableName)
152         value = float(typ(value))
153         self.myStudy.SetStringAsDouble(variableName, value)
154         return
155
156     def setAsReal(self, variableName):
157         self.setAs(variableName, float)
158         return
159
160     def setAsInteger(self, variableName):
161         self.setAs(variableName, int)
162         return
163
164     def setAsBool(self, variableName):
165         self.setAs(variableName, bool)
166         return
167
168     def check(self):
169         for variableName in self.myStudy.GetVariableNames():
170             self.get(variableName)
171             pass
172         return
173
174     pass
175
176
177 def checkThisNoteBook(**kwargs):
178     note_book = NoteBook(False)
179     note_book.check()
180     return
181
182
183 notebook = NoteBook()