* Fix : remove hook_late_configuration from BaseModule, if a module do not have,...
[shinken.git] / shinken / autoslots.py
blob7e4bd1753febf25553f90945f2757d112b5e9f33
1 #!/usr/bin/env python
3 # Copyright (C) 2009-2010 :
4 # Gabes Jean, naparuba@gmail.com
5 # Gerhard Lausser, Gerhard.Lausser@consol.de
6 # Gregory Starck, g.starck@gmail.com
7 # Hartmut Goebel, h.goebel@goebel-consult.de
9 # This file is part of Shinken.
11 # Shinken is free software: you can redistribute it and/or modify
12 # it under the terms of the GNU Affero General Public License as published by
13 # the Free Software Foundation, either version 3 of the License, or
14 # (at your option) any later version.
16 # Shinken is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU Affero General Public License for more details.
21 # You should have received a copy of the GNU Affero General Public License
22 # along with Shinken. If not, see <http://www.gnu.org/licenses/>.
24 """The AutoSlots Class is a MetaClass : it manage how other classes
25 are created (Classes, not instances of theses classes).
26 Here it's role is to create the __slots__ list of the class with
27 all properties of Class.properties and Class.running_properties
28 so we do not have to add manually all properties to the __slots__
29 list when we add a new entry"""
31 class AutoSlots(type):
32 # new is call when we create a new Class
33 # that have metaclass = AutoSlots
34 # CLS is AutoSlots
35 # name is s tring of the Class (like Service)
36 # bases are the Classes of which Class inherits (like SchedulingItem)
37 # dct is the new Class dict (like all method of Service)
38 # Some properties name are not alowed in __slots__ like 2d_coords of
39 # Host, so we must tag them in properties with no_slots
40 def __new__(cls, name, bases, dct):
41 # Thanks to Bertrand Mathieu to the set idea
42 slots = dct.get('__slots__', set())
43 # Now get properties from properties and running_properties
44 if 'properties' in dct:
45 props = dct['properties']
46 slots.update((p for p in props
47 if not props[p].no_slots))
48 if 'running_properties' in dct:
49 props = dct['running_properties']
50 slots.update((p for p in props
51 if not props[p].no_slots))
52 dct['__slots__'] = tuple(slots)
53 return type.__new__(cls, name, bases, dct)