Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/kernel.git] / src / KERNEL_PY / iparameters.py
1 # Copyright (C) 2005  OPEN CASCADE, CEA, EDF R&D, LEG
2 #           PRINCIPIA R&D, EADS CCR, Lip6, BV, CEDRAT
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either 
6 # version 2.1 of the License.
7
8 # This library is distributed in the hope that it will be useful 
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 # Lesser General Public License for more details.
12
13 # You should have received a copy of the GNU Lesser General Public  
14 # License along with this library; if not, write to the Free Software 
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18
19 import salome
20 import string
21 import SALOME
22 import SALOMEDS
23 import SALOME_Session_idl
24
25 PT_INTEGER = 0
26 PT_REAL = 1
27 PT_BOOLEAN = 2
28 PT_STRING = 3
29 PT_REALARRAY = 4
30 PT_INTARRAY = 5
31 PT_STRARRAY = 6
32
33 _AP_LISTS_LIST_ = "AP_LISTS_LIST"
34 _AP_ENTRIES_LIST_ = "AP_ENTRIES_LIST"
35 _AP_PROPERTIES_LIST_ = "AP_PROPERTIES_LIST"
36 _AP_DUMP_PYTHON_ = "AP_DUMP_PYTHON"
37
38 vp_session = None
39
40 def getSession():
41     global vp_session
42     if vp_session is None:
43         vp_session = salome.naming_service.Resolve("/Kernel/Session")
44         vp_session = vp_session._narrow(SALOME.Session)
45         pass
46     return vp_session 
47
48 class IParameters:
49     """
50     Interface IParameters was designed to provide a common way to set up
51     named parameters. The interface is initialized by AttributeParameter that
52     is used as a container of stored data.
53     The interface supports 3 types of named parameters:
54     1. Named list - a named list consists of string values.
55        One can append value to list (method 'append'), get a number of values
56        in the list (method 'nbValues'), get all values of the given list
57        (method 'getValues') and get names of all created named lists.
58     2. Parameters - that is a set of named string values associated with some named
59        entity. Parameter consists of tree elements: entity name, a parameter name
60        and a parameter value. Thus for one named entity there are an arbitrary number
61        of pair 'name parameter : value'.
62        One can add a new parameter to entry (method 'setParameter'), get a value of
63        a given named parameter of the given entry (method 'getParameter'), get a number
64        of parameters of the given entry (method 'getNbParameters'), get all names of
65        parameters for the given entry (method 'getAllParameterNames'), get all
66        values of parameters for the entry (method 'getAllParameterValues') and get all
67        stored entries (method 'getEntries')
68     3. Property - a property has a name and a string value.
69        One can set property (method 'setProperty'), getProperty (method 'getProperty') and
70        get a list of all stored properties (method 'getProperties').
71
72     Note:   
73           Methods not mentioned above are private and is not supposed to be used
74           by module's developers.   
75        
76     """
77     def __init__(self, attributeParameter):
78         """Initializes the instance"""
79         self._ap = attributeParameter
80         pass
81
82     def append(self, listName, value):
83         """Appends a value to the named list"""
84         if self._ap is None: return -1
85         v = []
86         if self._ap.IsSet(listName, PT_STRARRAY) == 0:
87             if self._ap.IsSet(_AP_LISTS_LIST_, PT_STRARRAY) == 0: self._ap.SetStrArray(_AP_LISTS_LIST_, v);
88             if listName != _AP_ENTRIES_LIST_ and listName != _AP_PROPERTIES_LIST_:
89                 self.append(_AP_LISTS_LIST_, listName)
90                 pass
91             self._ap.SetStrArray(listName, v)
92             pass
93         
94         v = self._ap.GetStrArray(listName)
95         v.append(value)
96         self._ap.SetStrArray(listName, v)
97         return (len(v)-1)
98     
99     def nbValues(self, listName):
100         """Returns a number of values in the named list"""
101         if self._ap is None: return -1
102         if self._ap.IsSet(listName, PT_STRARRAY) == 0: return 0
103         v = self._ap.GetStrArray(listName)
104         return len(v)
105
106     def getValues(self, listName):
107         """Returns a list of values in the named list"""
108         v = []
109         if self._ap is None: return v
110         if self._ap.IsSet(listName, PT_STRARRAY) == 0: return v
111         return self._ap.GetStrArray(listName)
112
113     def getLists(self):
114         """Returns a list of named lists' names"""
115         v = []
116         if self._ap is None: return v
117         if self._ap.IsSet(_AP_LISTS_LIST_, PT_STRARRAY) == 0: return v
118         return self._ap.GetStrArray(_AP_LISTS_LIST_)
119
120     def setParameter(self, entry, parameterName, value):
121         """Sets a value of the named parameter for the entry"""
122         if self._ap is None: return
123         v = []
124         if self._ap.IsSet(entry, PT_STRARRAY) ==0: 
125             self.append(_AP_ENTRIES_LIST_, entry) #Add the entry to the internal list of entries
126             self._ap.SetStrArray(entry, v)
127             pass
128         
129         v = self._ap.GetStrArray(entry)
130         v.append(parameterName)
131         v.append(value)
132         self._ap.SetStrArray(entry, v)
133         pass
134
135     def getParameter(self, entry, parameterName):
136         """Returns a value of the named parameter for the entry"""
137         if self._ap is None: return ""
138         if self._ap.IsSet(entry, PT_STRARRAY) == 0: return ""
139         v = self._ap.GetStrArray(entry)
140         length = len(v);
141         i = 0
142         while i<length:
143             if v[i] == parameterName: return v[i+1]
144             i+=1
145             pass
146         
147         return ""
148
149     def getAllParameterNames(self, entry):
150         """Returns all parameter names of the given entry"""
151         v = []
152         names = []
153         if self._ap is None: return v
154         if self._ap.IsSet(entry, PT_STRARRAY) == 0: return v
155         v = self._ap.GetStrArray(entry)
156         length = len(v)
157         i = 0
158         while i<length:
159             names.append(v[i])
160             i+=2
161             pass
162         
163         return names
164
165     def getAllParameterValues(self, entry):
166         """Returns all parameter values of the given entry"""
167         v = []
168         values = []
169         if self._ap is None: return v
170         if self._ap.IsSet(entry, PT_STRARRAY) == 0: return v
171         v = self._ap.GetStrArray(entry)
172         length = len(v)
173         i = 1
174         while i<length:
175             values.append(v[i]+1)
176             i+=2
177             pass
178         
179         return values
180
181     def getNbParameters(self, entry):
182         """Returns a number of parameters of the entry"""
183         if self._ap is None: return -1
184         if self._ap.IsSet(entry, PT_STRARRAY) == 0: return -1
185         return len(self._ap.GetStrArray(entry))/2
186
187     def getEntries(self):
188         """Returns all entries"""
189         v = []
190         if self._ap is None: return v
191         if self._ap.IsSet(_AP_ENTRIES_LIST_, PT_STRARRAY) == 0: return v
192         return self._ap.GetStrArray(_AP_ENTRIES_LIST_)
193
194     def setProperty(self, name, value):
195         """Sets a property value"""
196         if self._ap is None: return
197         if self._ap.IsSet(name, PT_STRING) == 0: 
198             self.append(_AP_PROPERTIES_LIST_, name) #Add the property to the internal list of properties
199             pass
200         self._ap.SetString(name, value)
201         pass
202
203     def getProperty(self, name):
204         """Returns a value of the named property"""
205         if self._ap is None: return ""
206         if self._ap.IsSet(name, PT_STRING) == 0: return ""
207         return self._ap.GetString(name)
208
209     def getProperties(self):
210         """Returns all propetries"""
211         v = []
212         if self._ap is None: return v
213         if self._ap.IsSet(_AP_PROPERTIES_LIST_, PT_STRARRAY) == 0: return v
214         return self._ap.GetStrArray(_AP_PROPERTIES_LIST_)
215
216     def parseValue(self, value, separator, fromEnd):
217         """Breaks a value string in two parts which is divided by separator."""
218         v = []
219         pos = - 1
220         if fromEnd == 1: pos = value.rfind(separator)
221         else: pos = value.find(separator)
222
223         if pos < 0: 
224             v.append(value)
225             return v
226         
227         part1 = value[0:pos]
228         part2 = value[pos+1:len(value)]
229         v.append(part1)
230         v.append(part2)
231         return v
232
233     def setDumpPython(self, isDumping):
234         """Enables/Disables the dumping to Python"""
235         if self._ap is None: return
236         _ap.SetBool(_AP_DUMP_PYTHON_, isDumping)
237         pass
238
239     def isDumpPython(self):
240         """Returns whether there is the dumping to Python"""
241         if self._ap is None: return 0
242         if self._ap.IsSet(_AP_DUMP_PYTHON_, PT_BOOLEAN) == 0: return 0
243         return self._ap.GetBool(_AP_DUMP_PYTHON_)
244
245     pass