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