Merge branch 'master' of ssh://shinken.git.sourceforge.net/gitroot/shinken/shinken
[shinken.git] / shinken / property.py
blob9802215f49c32506d840d3321e702e129666fc54
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
7 # Gregory Starck, g.starck@gmail.com
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/>.
23 """
24 """
26 from shinken.util import to_bool, to_float, to_split, to_char, to_int
28 __author__ = "Hartmut Goebel <h.goebel@goebel-consult.de>"
29 __copyright__ = "Copyright 2010 by Hartmut Goebel <h.goebel@goebel-consult.de>"
30 __licence__ = "GNU Affero General Public License version 3 (AGPL v3)"
33 FULL_STATUS = 'full_status'
34 CHECK_RESULT = 'check_result'
37 none_object = object()
40 class Property(object):
41 """
42 Baseclass of all properties.
44 Same semantic for all subclasses (except UnusedProp): The property
45 is required if, and only if, the default value is `None`.
48 """
49 def __init__(self, default=none_object, class_inherit=[],
50 unmanaged=False, help='', no_slots=False,
51 fill_brok=[], conf_send_preparation=None,
52 brok_transformation=None,retention=False,to_send=False,
53 override=False,managed=True):
54 """
55 `default`: default value to be used if this property is not set.
56 If default is None, this property is requied.
58 `class_inherit`: List of 2-tuples, (Service, 'blabla') : must
59 set this property to the Service class with name
60 blabla. if (Service, None): must set this property
61 to the Service class with same name
62 `unmanaged`: ....
63 `help`: usage text
64 `no_slots`: do not take this property for __slots__
66 `fill_brok`: if set, send to broker. There are two categories:
67 FULL_STATUS for initial and update status,
68 CHECK_RESULT for check results
70 Only for the inital call:
72 conf_send_preparation: if set, will pass the property to this
73 function. It's used to 'flatten' some dangerous
74 properties like realms that are too 'linked' to
75 be send like that.
77 brok_transformation: if set, will call the function with the
78 value of the property the major times it will be
79 to flatten the data (like realm_name instead of
80 the realm object).
82 override : for scheduler, if the property must override the
83 value of the configuration we send
85 managed : property that is managed in Nagios but not in Shinken
87 """
88 self.default = default
89 self.has_default = (default is not none_object)
90 self.required = not self.has_default
91 self.class_inherit = class_inherit
92 self.help = help or ''
93 self.unmanaged = unmanaged
94 self.no_slots = no_slots
95 self.fill_brok = fill_brok
96 self.conf_send_preparation = conf_send_preparation
97 self.brok_transformation = brok_transformation
98 self.retention = retention
99 self.to_send = to_send
100 self.override = override
101 self.managed = managed
102 self.unused = False
105 class UnusedProp(Property):
107 A unused Property. These are typically used by Nagios but
108 no longer usefull/used by Shinken.
110 This is just to warn the user that the option he use is no more used
111 in Shinken.
113 def __init__(self, text=None):
115 Since this property is not used, there is no use for other
116 parameters than `text`.
118 `text` a some usage text if present, will print it to explain
119 why it's no more useful
121 if text is None:
122 text = ("This parameter is no longer useful in the "
123 "Shinken architecture.")
124 self.text = text
125 self.has_default = False
126 self.class_inherit = []
127 self.unused = True
128 self.managed = True
131 _boolean_states = {'1': True, 'yes': True, 'true': True, 'on': True,
132 '0': False, 'no': False, 'false': False, 'off': False}
135 class BoolProp(Property):
137 A Boolean Property.
139 Boolean values are currently case insensitively defined as 0,
140 false, no, off for False, and 1, true, yes, on for True).
142 # @staticmethod
143 def pythonize(self, val):
144 return _boolean_states[val.lower()]
147 class IntegerProp(Property):
148 # @staticmethod
149 def pythonize(self, val):
150 return to_int(val)
153 class FloatProp(Property):
154 # @staticmethod
155 def pythonize(self, val):
156 return to_float(val)
159 class CharProp(Property):
160 # @staticmethod
161 def pythonize(self, val):
162 return to_char(val)
165 class StringProp(Property):
166 # @staticmethod
167 def pythonize(self, val):
168 return val
171 class ListProp(Property):
172 # @staticmethod
173 def pythonize(self, val):
174 return to_split(val)