Back to index

salome-kernel  6.5.0
iparameters.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 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00005 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00006 #
00007 # This library is free software; you can redistribute it and/or
00008 # modify it under the terms of the GNU Lesser General Public
00009 # License as published by the Free Software Foundation; either
00010 # version 2.1 of the License.
00011 #
00012 # This library is distributed in the hope that it will be useful,
00013 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 # Lesser General Public License for more details.
00016 #
00017 # You should have received a copy of the GNU Lesser General Public
00018 # License along with this library; if not, write to the Free Software
00019 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00020 #
00021 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00022 #
00023 
00024 import salome
00025 import string
00026 import SALOME
00027 import SALOMEDS
00028 import SALOME_Session_idl
00029 
00030 PT_INTEGER = 0
00031 PT_REAL = 1
00032 PT_BOOLEAN = 2
00033 PT_STRING = 3
00034 PT_REALARRAY = 4
00035 PT_INTARRAY = 5
00036 PT_STRARRAY = 6
00037 
00038 _AP_LISTS_LIST_ = "AP_LISTS_LIST"
00039 _AP_ENTRIES_LIST_ = "AP_ENTRIES_LIST"
00040 _AP_PROPERTIES_LIST_ = "AP_PROPERTIES_LIST"
00041 _AP_DUMP_PYTHON_ = "AP_DUMP_PYTHON"
00042 
00043 vp_session = None
00044 
00045 def getSession():
00046     global vp_session
00047     if vp_session is None:
00048         vp_session = salome.naming_service.Resolve("/Kernel/Session")
00049         vp_session = vp_session._narrow(SALOME.Session)
00050         pass
00051     return vp_session 
00052 
00053 class IParameters:
00054     """
00055     Interface IParameters was designed to provide a common way to set up
00056     named parameters. The interface is initialized by AttributeParameter that
00057     is used as a container of stored data.
00058     The interface supports 3 types of named parameters:
00059     1. Named list - a named list consists of string values.
00060        One can append value to list (method 'append'), get a number of values
00061        in the list (method 'nbValues'), get all values of the given list
00062        (method 'getValues') and get names of all created named lists.
00063     2. Parameters - that is a set of named string values associated with some named
00064        entity. Parameter consists of tree elements: entity name, a parameter name
00065        and a parameter value. Thus for one named entity there are an arbitrary number
00066        of pair 'name parameter : value'.
00067        One can add a new parameter to entry (method 'setParameter'), get a value of
00068        a given named parameter of the given entry (method 'getParameter'), get a number
00069        of parameters of the given entry (method 'getNbParameters'), get all names of
00070        parameters for the given entry (method 'getAllParameterNames'), get all
00071        values of parameters for the entry (method 'getAllParameterValues') and get all
00072        stored entries (method 'getEntries')
00073     3. Property - a property has a name and a string value.
00074        One can set property (method 'setProperty'), getProperty (method 'getProperty') and
00075        get a list of all stored properties (method 'getProperties').
00076 
00077     Note:   
00078           Methods not mentioned above are private and is not supposed to be used
00079           by module's developers.
00080        
00081     """
00082     def __init__(self, attributeParameter, clr=False):
00083         """Initializes the instance. If clr parameter is True, all IAPP attribute values are cleared."""
00084         self._ap = attributeParameter
00085         if ( clr ): self.clear()
00086         pass
00087        
00088     def clear(self):
00089         """Clear parameters"""
00090        self._ap.Clear()
00091 
00092     def append(self, listName, value):
00093         """Appends a value to the named list"""
00094         if self._ap is None: return -1
00095         v = []
00096         if self._ap.IsSet(listName, PT_STRARRAY) == 0:
00097             if self._ap.IsSet(_AP_LISTS_LIST_, PT_STRARRAY) == 0: self._ap.SetStrArray(_AP_LISTS_LIST_, v);
00098             if listName != _AP_ENTRIES_LIST_ and listName != _AP_PROPERTIES_LIST_:
00099                 self.append(_AP_LISTS_LIST_, listName)
00100                 pass
00101             self._ap.SetStrArray(listName, v)
00102             pass
00103         
00104         v = self._ap.GetStrArray(listName)
00105         v.append(value)
00106         self._ap.SetStrArray(listName, v)
00107         return (len(v)-1)
00108     
00109     def nbValues(self, listName):
00110         """Returns a number of values in the named list"""
00111         if self._ap is None: return -1
00112         if self._ap.IsSet(listName, PT_STRARRAY) == 0: return 0
00113         v = self._ap.GetStrArray(listName)
00114         return len(v)
00115 
00116     def getValues(self, listName):
00117         """Returns a list of values in the named list"""
00118         v = []
00119         if self._ap is None: return v
00120         if self._ap.IsSet(listName, PT_STRARRAY) == 0: return v
00121         return self._ap.GetStrArray(listName)
00122 
00123     def getLists(self):
00124         """Returns a list of named lists' names"""
00125         v = []
00126         if self._ap is None: return v
00127         if self._ap.IsSet(_AP_LISTS_LIST_, PT_STRARRAY) == 0: return v
00128         return self._ap.GetStrArray(_AP_LISTS_LIST_)
00129 
00130     def setParameter(self, entry, parameterName, value):
00131         """Sets a value of the named parameter for the entry"""
00132         if self._ap is None: return
00133         v = []
00134         if self._ap.IsSet(entry, PT_STRARRAY) ==0: 
00135             self.append(_AP_ENTRIES_LIST_, entry) #Add the entry to the internal list of entries
00136             self._ap.SetStrArray(entry, v)
00137             pass
00138         
00139         v = self._ap.GetStrArray(entry)
00140         v.append(parameterName)
00141         v.append(value)
00142         self._ap.SetStrArray(entry, v)
00143         pass
00144 
00145     def getParameter(self, entry, parameterName):
00146         """Returns a value of the named parameter for the entry"""
00147         if self._ap is None: return ""
00148         if self._ap.IsSet(entry, PT_STRARRAY) == 0: return ""
00149         v = self._ap.GetStrArray(entry)
00150         length = len(v);
00151         i = 0
00152         while i<length:
00153             if v[i] == parameterName: return v[i+1]
00154             i+=1
00155             pass
00156         
00157         return ""
00158 
00159     def getAllParameterNames(self, entry):
00160         """Returns all parameter names of the given entry"""
00161         v = []
00162         names = []
00163         if self._ap is None: return v
00164         if self._ap.IsSet(entry, PT_STRARRAY) == 0: return v
00165         v = self._ap.GetStrArray(entry)
00166         length = len(v)
00167         i = 0
00168         while i<length:
00169             names.append(v[i])
00170             i+=2
00171             pass
00172         
00173         return names
00174 
00175     def getAllParameterValues(self, entry):
00176         """Returns all parameter values of the given entry"""
00177         v = []
00178         values = []
00179         if self._ap is None: return v
00180         if self._ap.IsSet(entry, PT_STRARRAY) == 0: return v
00181         v = self._ap.GetStrArray(entry)
00182         length = len(v)
00183         i = 1
00184         while i<length:
00185             values.append(v[i]+1)
00186             i+=2
00187             pass
00188         
00189         return values
00190 
00191     def getNbParameters(self, entry):
00192         """Returns a number of parameters of the entry"""
00193         if self._ap is None: return -1
00194         if self._ap.IsSet(entry, PT_STRARRAY) == 0: return -1
00195         return len(self._ap.GetStrArray(entry))/2
00196 
00197     def getEntries(self):
00198         """Returns all entries"""
00199         v = []
00200         if self._ap is None: return v
00201         if self._ap.IsSet(_AP_ENTRIES_LIST_, PT_STRARRAY) == 0: return v
00202         return self._ap.GetStrArray(_AP_ENTRIES_LIST_)
00203 
00204     def setProperty(self, name, value):
00205         """Sets a property value"""
00206         if self._ap is None: return
00207         if self._ap.IsSet(name, PT_STRING) == 0: 
00208             self.append(_AP_PROPERTIES_LIST_, name) #Add the property to the internal list of properties
00209             pass
00210         self._ap.SetString(name, value)
00211         pass
00212 
00213     def getProperty(self, name):
00214         """Returns a value of the named property"""
00215         if self._ap is None: return ""
00216         if self._ap.IsSet(name, PT_STRING) == 0: return ""
00217         return self._ap.GetString(name)
00218 
00219     def getProperties(self):
00220         """Returns all propetries"""
00221         v = []
00222         if self._ap is None: return v
00223         if self._ap.IsSet(_AP_PROPERTIES_LIST_, PT_STRARRAY) == 0: return v
00224         return self._ap.GetStrArray(_AP_PROPERTIES_LIST_)
00225 
00226     def parseValue(self, value, separator, fromEnd):
00227         """Breaks a value string in two parts which is divided by separator."""
00228         v = []
00229         pos = - 1
00230         if fromEnd == 1: pos = value.rfind(separator)
00231         else: pos = value.find(separator)
00232 
00233         if pos < 0: 
00234             v.append(value)
00235             return v
00236         
00237         part1 = value[0:pos]
00238         part2 = value[pos+1:len(value)]
00239         v.append(part1)
00240         v.append(part2)
00241         return v
00242 
00243     def setDumpPython(self, isDumping):
00244         """Enables/Disables the dumping to Python"""
00245         if self._ap is None: return
00246         _ap.SetBool(_AP_DUMP_PYTHON_, isDumping)
00247         pass
00248 
00249     def isDumpPython(self):
00250         """Returns whether there is the dumping to Python"""
00251         if self._ap is None: return 0
00252         if self._ap.IsSet(_AP_DUMP_PYTHON_, PT_BOOLEAN) == 0: return 0
00253         return self._ap.GetBool(_AP_DUMP_PYTHON_)
00254 
00255     pass