1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2010-2021 CEA/DEN, EDF R&D, OPEN CASCADE
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 ## \defgroup datamodeler datamodeler
23 # \details Helper for modeling user data
27 __date__ ="$15 avr. 2010 19:44:17$"
29 from .uiexception import DevelException
31 # Most usable class types
32 TypeString= "".__class__
34 TypeInteger = __ref_integer.__class__
36 TypeDouble = __ref_double.__class__
38 TypeList = __ref_list.__class__
40 TypeDictionnary = __ref_dict.__class__
42 # There is no control to do for these attributes. They are attributes for the
43 # class management and not data of the model.
44 UNCHECKED_ATTRIBUTES = [
51 ## This class is a placeholder for modeling data. An object based on this class
52 # (particular instance or specialized derived class) can defined attributes with
53 # the following properties:
54 # - a type : the class or the type of the attribute. Setting an attribute to
55 # a value whose type is not the specified type raises an exception.
56 # - a range : a list of the possible values for the attribute. Setting an
57 # attribute to a value not in the range raises an exception
58 # - a default: the default value of an attribute when an instance is created
59 # - a void flag: the attribute can be authorized to be None or not using this
60 # flag. Setting an attribute to a None value while the flag is not set to
61 # True raises an exception. By default, a None value is not allowed.
63 # These properties are dictionnaries mapping the attribute name to its
64 # associated value for the property.
65 # \n A typical usage is to derived this class in a specialized form where the
66 # attributes names and there properties are defined in the constructor. See
67 # use cases at the end of this file.
68 # \ingroup datamodeler
71 This class is a placeholder for modeling data. An object based on this class
72 (particular instance or specialized derived class) can defined attributes with
73 the following properties:
75 - a type : the class or the type of the attribute. Setting an attribute to
76 a value whose type is not the specified type raises an exception.
77 - a range : a list of the possible values for the attribute. Setting an
78 attribute to a value not in the range raises an exception
79 - a default: the default value of an attribute when an instance is created
80 - a void flag: the attribute can be authorized to be None or not using this
81 flag. Setting an attribute to a None value while the flag is not set to
82 True raises an exception. By default, a None value is not allowed.
84 These properties are dictionnaries mapping the attribute name to its
85 associated value for the property.
87 A typical usage is to derived this class in a specialized form where the
88 attributes names and there properties are defined in the constructor. See
89 use cases at the end of this file.
92 def __init__(self, typemap=None, rangemap=None, defaultmap=None, voidmap=None):
94 self._rangemap = {} # possible values
95 self._defaultmap = {} # defaults values
96 self._voidmap = {} # None values are allowed
98 if typemap is not None:
99 self._typemap.update(typemap)
100 if rangemap is not None:
101 self._rangemap.update(rangemap)
102 if voidmap is not None:
103 self._voidmap.update(voidmap)
105 # Default initialization (if any)
106 if defaultmap is not None:
107 self._defaultmap.update(defaultmap)
108 for name in self._defaultmap:
109 self.__setattr__(name,self._defaultmap[name])
111 ## %A None argument means that no entry is created in the associated maps.
112 def addAttribute(self, name, a_type=None, a_range=None, default=None, void=None):
114 A None argument means that no entry is created in the associated maps.
116 self._typemap[name] = a_type
118 if a_range is not None:
119 self._rangemap[name] = a_range
122 self._voidmap[name] = void
124 if (not void) and (default is None):
127 self.__setattr__(name,default)
129 def __setattr__(self, name, val):
130 if name in UNCHECKED_ATTRIBUTES:
131 object.__setattr__(self, name, val)
135 if name == "_typemap":
136 print("WARNING WARNING WARNING : changing value of _typemap by ",val)
138 if name not in self._typemap:
139 raise DevelException("The class "+str(self.__class__)+" has no attribute "+str(name))
142 if not self.__isVoidAllowed(name):
143 raise DevelException("The attribute "+str(name)+" can't be None")
145 # We can stop here and set the value to None
146 self.__dict__[name] = None
149 if self.__isNotValidType(name,val):
150 raise DevelException("The attribute "+str(name)+" must be an instance of "+str(self._typemap[name]))
152 if self.__isNotValidRange(name,val):
153 raise DevelException("The attribute "+str(name)+" must be a value in :"+str(self._rangemap[name]))
155 self.__dict__[name] = val
157 def __getattribute__(self, name):
158 if name in UNCHECKED_ATTRIBUTES:
159 return object.__getattribute__(self, name)
161 if name in DataModeler.__dict__:
162 return object.__getattribute__(self, name)
164 if name not in self._typemap:
165 raise DevelException("The class has no attribute "+str(name))
166 # The attribute coulb be requested while it has not been created yet (for
167 # example if we did't call the setter before).
168 if name not in self.__dict__.keys():
171 return object.__getattribute__(self, name)
173 def __isNotValidType(self, name, val):
175 ( self._typemap[name] is not None) and
176 ( not isinstance(val,self._typemap[name]) ) )
180 def __isNotValidRange(self, name, val):
182 ( self._rangemap is not None) and
183 ( name in self._rangemap ) and
184 ( self._rangemap[name] is not None ) and
185 ( val not in self._rangemap[name] ) )
189 def __isVoidAllowed(self,name):
191 ( self._voidmap is not None) and
192 ( name in self._voidmap ) and
193 ( self._voidmap[name] is True ) )
198 print("DATAMODELER ["+str(self.__class__)+"]: self._typemap.keys() = "+str(list(self._typemap.keys())))
204 # ==============================================================================
205 # Basic use cases and unit tests
206 # ==============================================================================
210 typemap["stringdata"] = TypeString
211 typemap["integerdata"] = TypeInteger
212 typemap["anydata"] = None # can be anything
214 data = DataModeler(typemap)
218 data.stringdata = sdata
219 data.integerdata = idata
222 data.anydata = "any value"
225 print(data.integerdata)
228 def TEST_addAttribute():
230 typemap["stringdata"] = TypeString
231 typemap["integerdata"] = TypeInteger
232 data = DataModeler(typemap)
233 data.stringdata = "a string value"
244 if data.myAttr != ref_value:
248 except Exception as e:
253 data.myAttr = "bad type value"
255 except Exception as e:
259 def TEST_badAttributeName():
261 map["stringdata"] = TypeString
262 map["integerdata"] = TypeInteger
264 data = DataModeler(map)
266 # this should raise an exception
270 except Exception as e:
271 print("OK : "+str(e))
274 def TEST_badAttributeType():
276 map["stringdata"] = TypeString
277 map["integerdata"] = TypeInteger
279 data = DataModeler(map)
280 # this should raise an exception
284 except Exception as e:
285 print("OK : "+str(e))
288 def TEST_badAttributeRange():
290 map["stringdata"] = TypeString
291 map["integerdata"] = TypeInteger
295 range["integerdata"] = [1,ref_integervalue,7]
297 data = DataModeler(map,range)
298 # this should not raise an exception
300 data.integerdata = ref_integervalue
301 data.stringdata = "anything (no restriction has been defined)"
302 except Exception as e:
306 # this should raise an exception
308 data.integerdata = 9999 # a value not in the range
310 except Exception as e:
314 def TEST_voidAttributeAllowed():
316 map["stringdata"] = TypeString
317 map["integerdata"] = TypeInteger
320 voidmap["stringdata"] = True
322 data = DataModeler(typemap=map,voidmap=voidmap)
324 # this should not raise an exception
325 data.stringdata = None
326 print(data.stringdata)
327 except Exception as e:
332 # this should raise an exception
333 data.integerdata = None
335 except Exception as e:
339 def TEST_defaultValues():
341 typemap["stringdata"] = TypeString
342 typemap["integerdata"] = TypeInteger
344 ref_value = "my initial value"
346 defaultmap["stringdata"] = ref_value
348 data = DataModeler(typemap=typemap,defaultmap=defaultmap)
349 print(data.stringdata)
350 if data.stringdata != ref_value:
355 if __name__ == "__main__":
356 from .unittester import run
357 run("salome/kernel/datamodeler","TEST_usecase")
358 run("salome/kernel/datamodeler","TEST_addAttribute")
359 run("salome/kernel/datamodeler","TEST_badAttributeName")
360 run("salome/kernel/datamodeler","TEST_badAttributeType")
361 run("salome/kernel/datamodeler","TEST_badAttributeRange")
362 run("salome/kernel/datamodeler","TEST_voidAttributeAllowed")
363 run("salome/kernel/datamodeler","TEST_defaultValues")