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