64a3d05699bf855e8655cd4a2e1da42c30adb043
[nepi.git] / src / nepi / execution / attribute.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2013 INRIA
4 #
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.
9 #
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.
14 #
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/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19
20 ### Attribute Types
21 class Types:
22     String  = "STRING"
23     Bool    = "BOOL"
24     Enumerate    = "ENUM"
25     Double  = "DOUBLE"
26     Integer = "INTEGER"
27
28 ### Attribute Flags
29 class Flags:
30     """ Differents flags to characterize an attribute
31
32     """
33     # Attribute value can not be read (it is hidden to the user) 
34     NoRead    = 1 # 1
35     
36     # Attribute value can not be modified (it is not editable by the user)
37     NoWrite   = 1 << 1 # 2
38     
39     # Attribute value can be modified only before deployment
40     Design  = 1 << 2 # 4
41
42     # Attribute value will be used only during the deployment face
43     Construct    = 1 << 3 | Design # 8 + 4
44
45     # Attribute provides credentials to access resources
46     Credential  = 1 << 4  | Design # 16 + 4
47
48     # Attribute is a filter used to discover resources
49     Filter  = 1 << 5 | Design # 32 + 4
50
51
52 class Attribute(object):
53     """
54     .. class:: Class Args :
55
56         An Attribute reflects a configuration parameter for
57         a particular resource. Attributes might be read only or
58         not.
59       
60         :param name: Name of the attribute
61         :type name: str
62
63         :param help: Attribute description
64         :type help: str
65         
66         :param type: The type expected for the attribute value.
67                      Should be one of Attribute.Types .
68         :type type: str
69
70         :param flags: Defines attribute behavior (i.e. whether it is read-only,
71                 read and write, etc). This parameter should take its values from
72                 Attribute.Flags. Flags values can be bitwised.
73         :type flags: hex
74
75         :param default: Default value of the attribute
76         :type default: depends on the type of attribute
77         
78         :param allowed: List of values that the attribute can take. 
79                 This parameter is only meaningful for Enumerate type attributes.
80         :type allowed: list
81         
82         :param range: (max, min) tuple with range of possible values for
83                 attributes.
84                 This parameter is only meaningful for Integer or Double type
85                 attributes.
86         :type range: (int, int) or (float, float)
87         
88         :param set_hook: Function that will be executed when ever a new 
89                 value is set for the attribute.
90         :type set_hook: function
91
92     """
93     def __init__(self, name, help, type = Types.String,
94             flags = None, default = None, allowed = None,
95             range = None, set_hook = None):
96         self._name = name
97         self._help = help
98         self._type = type
99         self._flags = flags or 0
100         self._allowed = allowed
101         self._range = range
102         self._default = self._value = default
103         # callback to be invoked upon changing the 
104         # attribute value
105         self.set_hook = set_hook
106
107     @property
108     def name(self):
109         """ Returns the name of the attribute """
110         return self._name
111
112     @property
113     def default(self):
114         """ Returns the default value of the attribute """
115         return self._default
116
117     @property
118     def type(self):
119         """ Returns the type of the attribute """
120         return self._type
121
122     @property
123     def help(self):
124         """ Returns the help of the attribute """
125         return self._help
126
127     @property
128     def flags(self):
129         """ Returns the flags of the attribute """
130         return self._flags
131
132     @property
133     def allowed(self):
134         """ Returns the allowed value for this attribute """
135         return self._allowed
136
137     @property
138     def range(self):
139         """ Returns the range of the attribute """
140         return self._range
141
142     def has_flag(self, flag):
143         """ Returns true if the attribute has the flag 'flag'
144
145         :param flag: Flag that need to be ckecked
146         :type flag: Flags
147         """
148         return (self._flags & flag) == flag
149
150     def get_value(self):
151         """ Returns the value of the attribute """
152         return self._value
153
154     def set_value(self, value):
155         """ Change the value of the attribute after checking the type """
156         valid = True
157
158         if self.type == Types.Enumerate:
159             valid = value in self._allowed
160
161         if self.type in [Types.Double, Types.Integer] and self.range:
162             (min, max) = self.range
163
164             value = float(value)
165
166             valid = (value >= min and value <= max) 
167         
168         valid = valid and self.is_valid_value(value)
169
170         if valid: 
171             if self.set_hook:
172                 # Hook receives old value, new value
173                 value = self.set_hook(self._value, value)
174
175             self._value = value
176         else:
177             raise ValueError("Invalid value %s for attribute %s" %
178                     (str(value), self.name))
179
180     value = property(get_value, set_value)
181
182     def is_valid_value(self, value):
183         """ Attribute subclasses will override this method to add 
184         adequate validation"""
185         return True
186
187     def has_changed(self):
188         """ Returns true if the value has changed from the default """
189         return self.value != self.default