2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
30 """ Differents flags to characterize an attribute
33 # Attribute can be modified by the user
35 # Attribute is not modifiable by the user
37 # Attribute is not modifiable by the user during runtime
39 # Attribute is an access credential
42 # Attribute is a filter used to discover resources
46 class Attribute(object):
48 .. class:: Class Args :
50 An Attribute reflects a configuration parameter for
51 a particular resource. Attributes might be read only or
54 :param name: Name of the attribute
57 :param help: Attribute description
60 :param type: The type expected for the attribute value.
61 Should be one of Attribute.Types .
64 :param flags: Defines attribute behavior (i.e. whether it is read-only,
65 read and write, etc). This parameter should take its values from
66 Attribute.Flags. Flags values can be bitwised.
69 :param default: Default value of the attribute
70 :type default: depends on the type of attribute
72 :param allowed: List of values that the attribute can take.
73 This parameter is only meaningful for Enumerate type attributes.
76 :param range: (max, min) tuple with range of possible values for
78 This parameter is only meaningful for Integer or Double type
80 :type range: (int, int) or (float, float)
82 :param set_hook: Function that will be executed when ever a new
83 value is set for the attribute.
84 :type set_hook: function
87 def __init__(self, name, help, type = Types.String,
88 flags = Flags.NoFlags, default = None, allowed = None,
89 range = None, set_hook = None):
94 self._allowed = allowed
96 self._default = self._value = default
97 # callback to be invoked upon changing the
99 self.set_hook = set_hook
103 """ Returns the name of the attribute """
108 """ Returns the default value of the attribute """
113 """ Returns the type of the attribute """
118 """ Returns the help of the attribute """
123 """ Returns the flags of the attribute """
128 """ Returns the allowed value for this attribute """
133 """ Returns the range of the attribute """
136 def has_flag(self, flag):
137 """ Returns true if the attribute has the flag 'flag'
139 :param flag: Flag that need to be ckecked
142 return (self._flags & flag) == flag
145 """ Returns the value of the attribute """
148 def set_value(self, value):
149 """ Change the value of the attribute after checking the type """
152 if self.type == Types.Enumerate:
153 valid = value in self._allowed
155 if self.type in [Types.Double, Types.Integer] and self.range:
156 (min, max) = self.range
157 valid = (value >= min and value <= max)
159 valid = valid and self.is_valid_value(value)
163 # Hook receives old value, new value
164 value = self.set_hook(self._value, value)
168 raise ValueError("Invalid value %s for attribute %s" %
169 (str(value), self.name))
171 value = property(get_value, set_value)
173 def is_valid_value(self, value):
174 """ Attribute subclasses will override this method to add
175 adequate validation"""