Clean : (Grégory Starck) clean of some getattr code, bis.
[shinken.git] / shinken / property.py
blobc714fc4aeeb12782e84d9100fc5bb59a1d7c1cb3
1 # -*- mode: python ; coding: utf-8 -*-
3 # Copyright (C) 2010:
4 # Hartmut Goebel, h.goebel@goebel-consult.de
5 # Gabes Jean, naparuba@gmail.com
6 # Gerhard Lausser, Gerhard.Lausser@consol.de
8 # This file is part of Shinken.
10 # Shinken is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU Affero General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
15 # Shinken is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU Affero General Public License for more details.
20 # You should have received a copy of the GNU Affero General Public License
21 # along with Shinken. If not, see <http://www.gnu.org/licenses/>.
22 """
23 """
25 from shinken.util import to_bool, to_float, to_split, to_char, to_int
27 __author__ = "Hartmut Goebel <h.goebel@goebel-consult.de>"
28 __copyright__ = "Copyright 2010 by Hartmut Goebel <h.goebel@goebel-consult.de>"
29 __licence__ = "GNU Affero General Public License version 3 (AGPL v3)"
32 FULL_STATUS = 'full_status'
33 CHECK_RESULT = 'check_result'
36 class Property(object):
37 """
38 Baseclass of all properties.
40 Same semantic for all subclasses (except UnusedProp): The property
41 is required if, and only if, the default value is `None`.
44 """
45 def __init__(self, default='ididnotsetdefault', class_inherit=[],
46 unmanaged=False, help='', no_slots=False,
47 fill_brok=[], conf_send_preparation=None,
48 brok_transformation=None,retention=False,to_send=False,
49 override=False,managed=True):
50 """
51 `default`: default value to be used if this property is not set.
52 If default is None, this property is requied.
54 `class_inherit`: List of 2-tuples, (Service, 'blabla') : must
55 set this property to the Service class with name
56 blabla. if (Service, None): must set this property
57 to the Service class with same name
58 `unmanaged`: ....
59 `help`: usage text
60 `no_slots`: do not take this property for __slots__
62 `fill_brok`: if set, send to broker. There are two categories:
63 FULL_STATUS for initial and update status,
64 CHECK_RESULT for check results
66 Only for the inital call:
68 conf_send_preparation: if set, will pass the property to this
69 function. It's used to 'flatten' some dangerous
70 properties like realms that are too 'linked' to
71 be send like that.
73 brok_transformation: if set, will call the function with the
74 value of the property the major times it will be
75 to flatten the data (like realm_name instead of
76 the realm object).
78 override : for scheduler, if the property must override the
79 value of the configuration we send
81 managed : property that is managed in Nagios but not in Shinken
83 """
84 self.default = default
85 self.has_default = (default != 'ididnotsetdefault')
86 self.required = default is 'ididnotsetdefault'
87 self.class_inherit = class_inherit
88 self.help = help or ''
89 self.unmanaged = unmanaged
90 self.no_slots = no_slots
91 self.fill_brok = fill_brok
92 self.conf_send_preparation = conf_send_preparation
93 self.brok_transformation = brok_transformation
94 self.retention = retention
95 self.to_send = to_send
96 self.override = override
97 self.managed = managed
98 self.unused = False
101 class UnusedProp(Property):
103 A unused Property. These are typically used by Nagios but
104 no longer usefull/used by Shinken.
106 This is just to warn the user that the option he use is no more used
107 in Shinken.
109 def __init__(self, text=None):
111 Since this property is not used, there is no use for other
112 parameters than `text`.
114 `text` a some usage text if present, will print it to explain
115 why it's no more useful
117 if text is None:
118 text = ("This parameter is no longer useful in the "
119 "Shinken architecture.")
120 self.text = text
121 self.has_default = False
122 self.class_inherit = []
123 self.unused = True
124 self.managed = True
127 _boolean_states = {'1': True, 'yes': True, 'true': True, 'on': True,
128 '0': False, 'no': False, 'false': False, 'off': False}
131 class BoolProp(Property):
133 A Boolean Property.
135 Boolean values are currently case insensitively defined as 0,
136 false, no, off for False, and 1, true, yes, on for True).
138 # @staticmethod
139 def pythonize(self, val):
140 return _boolean_states[val.lower()]
143 class IntegerProp(Property):
144 # @staticmethod
145 def pythonize(self, val):
146 return to_int(val)
149 class FloatProp(Property):
150 # @staticmethod
151 def pythonize(self, val):
152 return to_float(val)
155 class CharProp(Property):
156 # @staticmethod
157 def pythonize(self, val):
158 return to_char(val)
161 class StringProp(Property):
162 # @staticmethod
163 def pythonize(self, val):
164 return val
167 class ListProp(Property):
168 # @staticmethod
169 def pythonize(self, val):
170 return to_split(val)