Package brisa :: Package utils :: Module properties
[hide private]
[frames] | no frames]

Source Code for Module brisa.utils.properties

 1  # Licensed under the MIT license 
 2  # http://opensource.org/licenses/mit-license.php or see LICENSE file. 
 3  # Copyright 2007-2008 Brisa Team <brisa-develop@garage.maemo.org> 
 4   
 5  """ Facilities for python properties generation. 
 6  """ 
 7   
 8   
9 -def gen_property_with_default(name, fget=None, fset=None, doc=""):
10 """ Generates a property of a name either with a default fget or a default 11 fset. 12 13 @param name: property name 14 @param fget: default fget 15 @param fset: default fset 16 @param doc: documentation for the property 17 18 @type name: string 19 @type fget: callable or None 20 @type fset: callable or None 21 @type doc: string 22 """ 23 if fget == None and fset == None: 24 raise NotImplementedError("fget or fset must be not null") 25 26 internal_name = '%s%s' % ("_prop_", name) 27 28 def getter(self): 29 if not internal_name in dir(self): 30 setattr(self, internal_name, "") 31 return getattr(self, internal_name)
32 33 def setter(self, value): 34 return setattr(self, internal_name, value) 35 36 if fget is None: 37 return property(getter, fset, doc=doc) 38 39 return property(fget, setter, doc=doc) 40 41
42 -def gen_property_of_type(name, _type, doc=""):
43 """ Generates a type-forced property associated with a name. Provides type 44 checking on the setter (coherence between value to be set and the type 45 specified). 46 47 @param name: property name 48 @param _type: force type 49 @param doc: documentation for the property 50 51 @type name: string 52 @type _type: type 53 @type doc: string 54 """ 55 internal_name = '%s%s' % ("_prop_", name) 56 57 def getter(self): 58 return getattr(self, internal_name)
59 60 def setter(self, value): 61 if isinstance(value, _type): 62 return setattr(self, internal_name, value) 63 else: 64 raise TypeError(("invalid type '%s' for property %s:" 65 "%s is required.") % 66 (type(value).__name__, name, type(_type).__name__)) 67 68 return property(getter, setter, doc=doc) 69