Back to index

salome-kernel  6.5.0
salome_notebook.py
Go to the documentation of this file.
00001 #  -*- coding: iso-8859-1 -*-
00002 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00003 #
00004 # This library is free software; you can redistribute it and/or
00005 # modify it under the terms of the GNU Lesser General Public
00006 # License as published by the Free Software Foundation; either
00007 # version 2.1 of the License.
00008 #
00009 # This library is distributed in the hope that it will be useful,
00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 # Lesser General Public License for more details.
00013 #
00014 # You should have received a copy of the GNU Lesser General Public
00015 # License along with this library; if not, write to the Free Software
00016 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00017 #
00018 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00019 #
00020 
00021 #  File   : salome_notebook.py
00022 #  Author : Roman NIKOLAEV, Open CASCADE S.A.S.
00023 #  Module : SALOME
00024 #  $Header:
00025 #
00026 """
00027 Module salome_notebook gives access to Salome Notebook.
00028 """
00029 
00030 import salome
00031 
00032 class PseudoStudyForNoteBook(object):
00033     
00034     def __init__(self, **kwargs):
00035         self.kwargs = kwargs
00036         pass
00037     
00038     def GetVariableNames(self):
00039         return self.kwargs.keys()
00040     
00041     def IsVariable(self, variableName):
00042         return variableName in self.kwargs
00043     
00044     def IsReal(self, variableName):
00045         val = self.kwargs[variableName]
00046         try:
00047             float(val)
00048             return True
00049         except:
00050             pass
00051         return False
00052     
00053     IsInteger = IsReal
00054     IsBoolean = IsReal
00055     
00056     def IsString(self, variableName):
00057         return not self.IsReal(variableName)
00058     
00059     def GetString(self, variableName):
00060         return self.kwargs[variableName]
00061     
00062     def GetReal(self, variableName):
00063         return float(self.kwargs[variableName])
00064     
00065     GetInteger = GetReal
00066     GetBoolean = GetReal
00067     
00068     pass
00069 
00070 class NoteBook:
00071     
00072     def __init__(self, Study):
00073         self.myStudy = Study
00074     
00075     def set(self, variableName, variable):
00076         """
00077         Create (or modify) variable with name "variableName" 
00078         and value equal "theValue".
00079         """
00080         if type(variable) == float:
00081             self.myStudy.SetReal(variableName, variable)
00082             
00083         elif type(variable) == int:
00084             self.myStudy.SetInteger(variableName, variable)
00085             
00086         elif type(variable) == bool:
00087             self.myStudy.SetBoolean(variableName, variable)
00088             
00089         elif type(variable) == str:
00090             self.myStudy.SetString(variableName, variable)
00091             
00092     def get(self, variableName):
00093         """
00094         Return value of the variable with name "variableName".
00095         """
00096         aResult = None
00097         if self.myStudy.IsVariable(variableName):
00098             
00099             if self.myStudy.IsReal(variableName):
00100                 aResult = self.myStudy.GetReal(variableName)
00101 
00102             elif self.myStudy.IsInteger(variableName):
00103                 aResult = self.myStudy.GetInteger(variableName)
00104 
00105             elif self.myStudy.IsBoolean(variableName):
00106                 aResult = self.myStudy.GetBoolean(variableName)
00107 
00108             elif self.myStudy.IsString(variableName):
00109                 aResult = self.myStudy.GetString(variableName)
00110                 aResult_orig = aResult
00111                 l = self.myStudy.GetVariableNames()
00112                 l.remove(variableName)
00113                 # --
00114                 # To avoid the smallest strings to be replaced first,
00115                 # the list is sorted by decreasing lengths
00116                 # --
00117                 l.sort(key=str.__len__)
00118                 l.reverse()
00119                 for name in l:
00120                     if aResult.find(name) >= 0:
00121                         val = self.get(name)
00122                         aResult = aResult.replace(name, "%s"%(val))
00123                         pass
00124                     pass
00125                 try:
00126                     aResult = eval(aResult)
00127                 except Exception, e:
00128                     msg = str(e)
00129                     msg += "\n"
00130                     msg += "A problem occurs while parsing "
00131                     msg += "the variable %s "%(variableName.__repr__())
00132                     msg += "with value %s ..."%(aResult_orig.__repr__())
00133                     msg += "\n"
00134                     msg += "Please, check your notebook !"
00135                     raise Exception(msg)
00136                 pass
00137                 
00138         return aResult
00139     
00140     def isVariable(self, variableName): 
00141         """
00142         Return true if variable with name "variableName" 
00143         exists in the study, otherwise return false.
00144         """
00145         return self.myStudy.IsVariable(variableName)
00146 
00147     def setAs(self, variableName, typ):
00148         value = self.get(variableName)
00149         value = float(typ(value))
00150         self.myStudy.SetStringAsDouble(variableName, value)
00151         return
00152     
00153     def setAsReal(self, variableName):
00154         self.setAs(variableName, float)
00155         return
00156     
00157     def setAsInteger(self, variableName):
00158         self.setAs(variableName, int)
00159         return
00160     
00161     def setAsBool(self, variableName):
00162         self.setAs(variableName, bool)
00163         return
00164     
00165     def check(self):
00166         for variableName in self.myStudy.GetVariableNames():
00167             self.get(variableName)
00168             pass
00169         return
00170     
00171     pass
00172 
00173 def checkThisNoteBook(**kwargs):
00174     study = PseudoStudyForNoteBook(**kwargs)
00175     note_book = NoteBook(study)
00176     note_book.check()
00177     return
00178 
00179 notebook = NoteBook(salome.myStudy)