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
41 # Attribute is a filter used to discover resources
44 class Attribute(object):
46 .. class:: Class Args :
48 An Attribute reflects a configuration parameter for
49 a particular resource. Attributes might be read only or
52 :param name: Name of the attribute
55 :param help: Attribute description
58 :param type: The type expected for the attribute value.
59 Should be one of Attribute.Types .
62 :param flags: Defines attribute behavior (i.e. whether it is read-only,
63 read and write, etc). This parameter should take its values from
64 Attribute.Flags. Flags values can be bitwised.
67 :param default: Default value of the attribute
68 :type default: depends on the type of attribute
70 :param allowed: List of values that the attribute can take.
71 This parameter is only meaningful for Enumerate type attributes.
74 :param range: (max, min) tuple with range of possible values for
76 This parameter is only meaningful for Integer or Double type
78 :type range: (int, int) or (float, float)
80 :param set_hook: Function that will be executed when ever a new
81 value is set for the attribute.
82 :type set_hook: function
85 def __init__(self, name, help, type = Types.String,
86 flags = Flags.NoFlags, default = None, allowed = None,
87 range = None, set_hook = None):
92 self._allowed = allowed
94 self._default = self._value = default
95 # callback to be invoked upon changing the
97 self.set_hook = set_hook
101 """ Returns the name of the attribute """
106 """ Returns the default value of the attribute """
111 """ Returns the type of the attribute """
116 """ Returns the help of the attribute """
121 """ Returns the flags of the attribute """
126 """ Returns the allowed value for this attribute """
131 """ Returns the range of the attribute """
134 def has_flag(self, flag):
135 """ Returns true if the attribute has the flag 'flag'
137 :param flag: Flag that need to be ckecked
140 return (self._flags & flag) == flag
143 """ Returns the value of the attribute """
146 def set_value(self, value):
147 """ Change the value of the attribute after checking the type """
150 if self.type == Types.Enumerate:
151 valid = value in self._allowed
153 if self.type in [Types.Double, Types.Integer] and self.range:
154 (min, max) = self.range
155 valid = (value >= min and value <= max)
157 valid = valid and self.is_valid_value(value)
161 # Hook receives old value, new value
162 value = self.set_hook(self._value, value)
166 raise ValueError("Invalid value %s for attribute %s" %
167 (str(value), self.name))
169 value = property(get_value, set_value)
171 def is_valid_value(self, value):
172 """ Attribute subclasses will override this method to add
173 adequate validation"""