Salome HOME
CCAR: add a trace in beginService and endService even in no debug mode to give cpu...
[modules/kernel.git] / src / KERNEL_PY / iparameters.py
1 #  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 #  Copyright (C) 2003-2007  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 import salome
23 import string
24 import SALOME
25 import SALOMEDS
26 import SALOME_Session_idl
27
28 PT_INTEGER = 0
29 PT_REAL = 1
30 PT_BOOLEAN = 2
31 PT_STRING = 3
32 PT_REALARRAY = 4
33 PT_INTARRAY = 5
34 PT_STRARRAY = 6
35
36 _AP_LISTS_LIST_ = "AP_LISTS_LIST"
37 _AP_ENTRIES_LIST_ = "AP_ENTRIES_LIST"
38 _AP_PROPERTIES_LIST_ = "AP_PROPERTIES_LIST"
39 _AP_DUMP_PYTHON_ = "AP_DUMP_PYTHON"
40
41 vp_session = None
42
43 def getSession():
44     global vp_session
45     if vp_session is None:
46         vp_session = salome.naming_service.Resolve("/Kernel/Session")
47         vp_session = vp_session._narrow(SALOME.Session)
48         pass
49     return vp_session 
50
51 class IParameters:
52     """
53     Interface IParameters was designed to provide a common way to set up
54     named parameters. The interface is initialized by AttributeParameter that
55     is used as a container of stored data.
56     The interface supports 3 types of named parameters:
57     1. Named list - a named list consists of string values.
58        One can append value to list (method 'append'), get a number of values
59        in the list (method 'nbValues'), get all values of the given list
60        (method 'getValues') and get names of all created named lists.
61     2. Parameters - that is a set of named string values associated with some named
62        entity. Parameter consists of tree elements: entity name, a parameter name
63        and a parameter value. Thus for one named entity there are an arbitrary number
64        of pair 'name parameter : value'.
65        One can add a new parameter to entry (method 'setParameter'), get a value of
66        a given named parameter of the given entry (method 'getParameter'), get a number
67        of parameters of the given entry (method 'getNbParameters'), get all names of
68        parameters for the given entry (method 'getAllParameterNames'), get all
69        values of parameters for the entry (method 'getAllParameterValues') and get all
70        stored entries (method 'getEntries')
71     3. Property - a property has a name and a string value.
72        One can set property (method 'setProperty'), getProperty (method 'getProperty') and
73        get a list of all stored properties (method 'getProperties').
74
75     Note:   
76           Methods not mentioned above are private and is not supposed to be used
77           by module's developers.   
78        
79     """
80     def __init__(self, attributeParameter):
81         """Initializes the instance"""
82         self._ap = attributeParameter
83         pass
84
85     def append(self, listName, value):
86         """Appends a value to the named list"""
87         if self._ap is None: return -1
88         v = []
89         if self._ap.IsSet(listName, PT_STRARRAY) == 0:
90             if self._ap.IsSet(_AP_LISTS_LIST_, PT_STRARRAY) == 0: self._ap.SetStrArray(_AP_LISTS_LIST_, v);
91             if listName != _AP_ENTRIES_LIST_ and listName != _AP_PROPERTIES_LIST_:
92                 self.append(_AP_LISTS_LIST_, listName)
93                 pass
94             self._ap.SetStrArray(listName, v)
95             pass
96         
97         v = self._ap.GetStrArray(listName)
98         v.append(value)
99         self._ap.SetStrArray(listName, v)
100         return (len(v)-1)
101     
102     def nbValues(self, listName):
103         """Returns a number of values in the named list"""
104         if self._ap is None: return -1
105         if self._ap.IsSet(listName, PT_STRARRAY) == 0: return 0
106         v = self._ap.GetStrArray(listName)
107         return len(v)
108
109     def getValues(self, listName):
110         """Returns a list of values in the named list"""
111         v = []
112         if self._ap is None: return v
113         if self._ap.IsSet(listName, PT_STRARRAY) == 0: return v
114         return self._ap.GetStrArray(listName)
115
116     def getLists(self):
117         """Returns a list of named lists' names"""
118         v = []
119         if self._ap is None: return v
120         if self._ap.IsSet(_AP_LISTS_LIST_, PT_STRARRAY) == 0: return v
121         return self._ap.GetStrArray(_AP_LISTS_LIST_)
122
123     def setParameter(self, entry, parameterName, value):
124         """Sets a value of the named parameter for the entry"""
125         if self._ap is None: return
126         v = []
127         if self._ap.IsSet(entry, PT_STRARRAY) ==0: 
128             self.append(_AP_ENTRIES_LIST_, entry) #Add the entry to the internal list of entries
129             self._ap.SetStrArray(entry, v)
130             pass
131         
132         v = self._ap.GetStrArray(entry)
133         v.append(parameterName)
134         v.append(value)
135         self._ap.SetStrArray(entry, v)
136         pass
137
138     def getParameter(self, entry, parameterName):
139         """Returns a value of the named parameter for the entry"""
140         if self._ap is None: return ""
141         if self._ap.IsSet(entry, PT_STRARRAY) == 0: return ""
142         v = self._ap.GetStrArray(entry)
143         length = len(v);
144         i = 0
145         while i<length:
146             if v[i] == parameterName: return v[i+1]
147             i+=1
148             pass
149         
150         return ""
151
152     def getAllParameterNames(self, entry):
153         """Returns all parameter names of the given entry"""
154         v = []
155         names = []
156         if self._ap is None: return v
157         if self._ap.IsSet(entry, PT_STRARRAY) == 0: return v
158         v = self._ap.GetStrArray(entry)
159         length = len(v)
160         i = 0
161         while i<length:
162             names.append(v[i])
163             i+=2
164             pass
165         
166         return names
167
168     def getAllParameterValues(self, entry):
169         """Returns all parameter values of the given entry"""
170         v = []
171         values = []
172         if self._ap is None: return v
173         if self._ap.IsSet(entry, PT_STRARRAY) == 0: return v
174         v = self._ap.GetStrArray(entry)
175         length = len(v)
176         i = 1
177         while i<length:
178             values.append(v[i]+1)
179             i+=2
180             pass
181         
182         return values
183
184     def getNbParameters(self, entry):
185         """Returns a number of parameters of the entry"""
186         if self._ap is None: return -1
187         if self._ap.IsSet(entry, PT_STRARRAY) == 0: return -1
188         return len(self._ap.GetStrArray(entry))/2
189
190     def getEntries(self):
191         """Returns all entries"""
192         v = []
193         if self._ap is None: return v
194         if self._ap.IsSet(_AP_ENTRIES_LIST_, PT_STRARRAY) == 0: return v
195         return self._ap.GetStrArray(_AP_ENTRIES_LIST_)
196
197     def setProperty(self, name, value):
198         """Sets a property value"""
199         if self._ap is None: return
200         if self._ap.IsSet(name, PT_STRING) == 0: 
201             self.append(_AP_PROPERTIES_LIST_, name) #Add the property to the internal list of properties
202             pass
203         self._ap.SetString(name, value)
204         pass
205
206     def getProperty(self, name):
207         """Returns a value of the named property"""
208         if self._ap is None: return ""
209         if self._ap.IsSet(name, PT_STRING) == 0: return ""
210         return self._ap.GetString(name)
211
212     def getProperties(self):
213         """Returns all propetries"""
214         v = []
215         if self._ap is None: return v
216         if self._ap.IsSet(_AP_PROPERTIES_LIST_, PT_STRARRAY) == 0: return v
217         return self._ap.GetStrArray(_AP_PROPERTIES_LIST_)
218
219     def parseValue(self, value, separator, fromEnd):
220         """Breaks a value string in two parts which is divided by separator."""
221         v = []
222         pos = - 1
223         if fromEnd == 1: pos = value.rfind(separator)
224         else: pos = value.find(separator)
225
226         if pos < 0: 
227             v.append(value)
228             return v
229         
230         part1 = value[0:pos]
231         part2 = value[pos+1:len(value)]
232         v.append(part1)
233         v.append(part2)
234         return v
235
236     def setDumpPython(self, isDumping):
237         """Enables/Disables the dumping to Python"""
238         if self._ap is None: return
239         _ap.SetBool(_AP_DUMP_PYTHON_, isDumping)
240         pass
241
242     def isDumpPython(self):
243         """Returns whether there is the dumping to Python"""
244         if self._ap is None: return 0
245         if self._ap.IsSet(_AP_DUMP_PYTHON_, PT_BOOLEAN) == 0: return 0
246         return self._ap.GetBool(_AP_DUMP_PYTHON_)
247
248     pass