Package elisa :: Package core :: Package utils :: Module classinit
[hide private]
[frames] | no frames]

Source Code for Module elisa.core.utils.classinit

 1  # -*- mode: python; coding: utf-8 -*- 
 2  # 
 3  # Copyright © 2005 Ian Bicking and contributors 
 4  # 
 5  # Licensed under the MIT license 
 6  # <http://www.opensource.org/licenses/mit-license.php> 
 7  # 
 8  # Written for Paste <http://pythonpaste.org> 
 9   
10  from elisa.core.utils import misc 
11   
12 -class ClassInitMeta(type):
13
14 - def __new__(meta, class_name, bases, new_attrs):
15 cls = type.__new__(meta, class_name, bases, new_attrs) 16 if (new_attrs.has_key('__classinit__') 17 and not isinstance(cls.__classinit__, staticmethod)): 18 setattr(cls, '__classinit__', 19 staticmethod(cls.__classinit__.im_func)) 20 if hasattr(cls, '__classinit__'): 21 cls.__classinit__(cls, new_attrs) 22 return cls
23
24 -def build_properties(cls, new_attrs):
25 """ 26 Given a class and a new set of attributes (as passed in by 27 __classinit__), create or modify properties based on functions 28 with special names ending in __get, __set, and __del. 29 """ 30 for name, value in new_attrs.items(): 31 if (name.endswith('__get') or name.endswith('__set') 32 or name.endswith('__del')): 33 base = name[:-5] 34 if hasattr(cls, base): 35 old_prop = getattr(cls, base) 36 if isinstance(old_prop, property): 37 attrs = {'fget': old_prop.fget, 38 'fset': old_prop.fset, 39 'fdel': old_prop.fdel, 40 'doc': old_prop.__doc__} 41 else: 42 attrs = {} 43 else: 44 attrs = {} 45 attrs['f' + name[-3:]] = value 46 if name.endswith('__get') and value.__doc__: 47 attrs['doc'] = value.__doc__ 48 new_prop = property(**attrs) 49 setattr(cls, base, new_prop) 50 51 name = new_attrs.get('name') 52 if not name: 53 name = cls.__name__ 54 cls.name = misc.un_camelify(name)
55