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