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 :param name: Name of the attribute
50 :param help: Help about the attribute
52 :param type: type of the attribute
54 :param flags: Help about the attribute
56 :param default: Default value of the attribute
58 :param allowed: Allowed value for this attribute
60 :param range: Range of the attribute
62 :param set_hook: hook that is related with this attribute
66 def __init__(self, name, help, type = Types.String,
67 flags = Flags.NoFlags, default = None, allowed = None,
68 range = None, set_hook = None):
73 self._allowed = allowed
75 self._default = self._value = default
76 # callback to be invoked upon changing the
78 self.set_hook = set_hook
82 """ Returns the name of the attribute """
87 """ Returns the default value of the attribute """
92 """ Returns the type of the attribute """
97 """ Returns the help of the attribute """
102 """ Returns the flags of the attribute """
107 """ Returns the allowed value for this attribute """
112 """ Returns the range of the attribute """
115 def has_flag(self, flag):
116 """ Returns true if the attribute has the flag 'flag'
118 :param flag: Flag that need to be ckecked
121 return (self._flags & flag) == flag
124 """ Returns the value of the attribute """
127 def set_value(self, value):
128 """ Change the value of the attribute after checking the type """
131 if self.type == Types.Enumerate:
132 valid = value in self._allowed
134 if self.type in [Types.Double, Types.Integer] and self.range:
135 (min, max) = self.range
136 valid = (value >= min and value <= max)
138 valid = valid and self.is_valid_value(value)
142 # Hook receives old value, new value
143 value = self.set_hook(self._value, value)
147 raise ValueError("Invalid value %s for attribute %s" %
148 (str(value), self.name))
150 value = property(get_value, set_value)
152 def is_valid_value(self, value):
153 """ Attribute subclasses will override this method to add
154 adequate validation"""