1e3bdde87744691ea94e955a34e2e344942910ba
[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 can be modified by the user 
34     NoFlags         = 0x00
35     # Attribute is not modifiable by the user
36     ReadOnly        = 0x01
37     # Attribute is not modifiable by the user during runtime
38     ExecReadOnly        = 0x02
39     # Attribute is an access credential
40     # TODO REMOVE!!!
41     Credential      = 0x04
42     # Attribute is a filter used to discover resources
43     # TODO REMOVE!!!
44     Filter      = 0x08
45
46 class Attribute(object):
47     """
48     .. class:: Class Args :
49
50         An Attribute reflects a configuration parameter for
51         a particular resource. Attributes might be read only or
52         not.
53       
54         :param name: Name of the attribute
55         :type name: str
56
57         :param help: Attribute description
58         :type help: str
59         
60         :param type: The type expected for the attribute value.
61                      Should be one of Attribute.Types .
62         :type type: str
63
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.
67         :type flags: hex
68
69         :param default: Default value of the attribute
70         :type default: depends on the type of attribute
71         
72         :param allowed: List of values that the attribute can take. 
73                 This parameter is only meaningful for Enumerate type attributes.
74         :type allowed: list
75         
76         :param range: (max, min) tuple with range of possible values for
77                 attributes.
78                 This parameter is only meaningful for Integer or Double type
79                 attributes.
80         :type range: (int, int) or (float, float)
81         
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
85
86     """
87     def __init__(self, name, help, type = Types.String,
88             flags = Flags.NoFlags, default = None, allowed = None,
89             range = None, set_hook = None):
90         self._name = name
91         self._help = help
92         self._type = type
93         self._flags = flags
94         self._allowed = allowed
95         self._range = range
96         self._default = self._value = default
97         # callback to be invoked upon changing the 
98         # attribute value
99         self.set_hook = set_hook
100
101     @property
102     def name(self):
103         """ Returns the name of the attribute """
104         return self._name
105
106     @property
107     def default(self):
108         """ Returns the default value of the attribute """
109         return self._default
110
111     @property
112     def type(self):
113         """ Returns the type of the attribute """
114         return self._type
115
116     @property
117     def help(self):
118         """ Returns the help of the attribute """
119         return self._help
120
121     @property
122     def flags(self):
123         """ Returns the flags of the attribute """
124         return self._flags
125
126     @property
127     def allowed(self):
128         """ Returns the allowed value for this attribute """
129         return self._allowed
130
131     @property
132     def range(self):
133         """ Returns the range of the attribute """
134         return self._range
135
136     def has_flag(self, flag):
137         """ Returns true if the attribute has the flag 'flag'
138
139         :param flag: Flag that need to be ckecked
140         :type flag: Flags
141         """
142         return (self._flags & flag) == flag
143
144     def get_value(self):
145         """ Returns the value of the attribute """
146         return self._value
147
148     def set_value(self, value):
149         """ Change the value of the attribute after checking the type """
150         valid = True
151
152         if self.type == Types.Enumerate:
153             valid = value in self._allowed
154
155         if self.type in [Types.Double, Types.Integer] and self.range:
156             (min, max) = self.range
157
158             value = float(value)
159
160             valid = (value >= min and value <= max) 
161         
162         valid = valid and self.is_valid_value(value)
163
164         if valid: 
165             if self.set_hook:
166                 # Hook receives old value, new value
167                 value = self.set_hook(self._value, value)
168
169             self._value = value
170         else:
171             raise ValueError("Invalid value %s for attribute %s" %
172                     (str(value), self.name))
173
174     value = property(get_value, set_value)
175
176     def is_valid_value(self, value):
177         """ Attribute subclasses will override this method to add 
178         adequate validation"""
179         return True
180