1 # -*- coding: utf-8 -*-
3 class Attribute(object):
21 BOOL : lambda x : str(x).lower() in ("1","on","yes","true"),
29 # Read-only attribute at design time
31 # Invisible attribute at design time
32 DesignInvisible = 0x02
33 # Read-only attribute at execution time
35 # Invisible attribute at execution time
37 # Attribute doesn't change value during execution time
39 # Attribute has no default value in the testbed
41 # Metadata attribute (is not directly reflected by a real object attribute)
44 def __init__(self, name, help, type, value = None, range = None,
45 allowed = None, flags = None, validation_function = None,
47 if not type in Attribute.types:
48 raise AttributeError("invalid type %s " % type)
53 self._flags = flags if flags != None else Attribute.NoFlags
54 # range: max and min possible values
56 # list of possible values
57 self._allowed = allowed
58 self._validation_function = validation_function
59 self._modified = False
60 self._category = category
79 def is_design_invisible(self):
80 return self.has_flag(Attribute.DesignInvisible)
83 def is_design_read_only(self):
84 return self.has_flag(Attribute.DesignReadOnly)
87 def is_exec_invisible(self):
88 return self.has_flag(Attribute.ExecInvisible)
91 def is_exec_read_only(self):
92 return self.has_flag(Attribute.ExecReadOnly)
95 def is_exec_immutable(self):
96 return self.has_flag(Attribute.ExecImmutable)
99 def is_metadata(self):
100 return self.has_flag(Attribute.Metadata)
103 def has_no_default_value(self):
104 return self.has_flag(Attribute.NoDefaultValue)
108 return self._modified
112 return self._category
123 def validation_function(self):
124 return self._validation_function
126 def has_flag(self, flag):
127 return (self._flags & flag) == flag
132 def set_value(self, value):
133 if self.is_valid_value(value):
135 self._modified = True
137 raise ValueError("Invalid value %s for attribute %s" %
138 (str(value), self.name))
140 value = property(get_value, set_value)
142 def is_valid_value(self, value):
143 return self._is_in_range(value) and \
144 self._is_in_allowed_values(value) and \
145 self._is_valid(value)
147 def _is_in_range(self, value):
148 return not self.range or \
149 (value >= self.range[0] and value <= self.range[1])
151 def _is_in_allowed_values(self, value):
152 return not self._allowed or value in self._allowed
154 def _is_valid(self, value):
155 return not self._validation_function or \
156 self._validation_function(self, value)
158 class AttributesMap(object):
159 """AttributesMap is the base class for every object whose attributes
160 are going to be manipulated by the end-user in a script or GUI.
163 super(AttributesMap, self).__init__()
164 self._attributes = dict()
167 def attributes(self):
168 return self._attributes.values()
170 def get_attribute_list(self, filter_flags = None, exclude = False):
172 Returns the list of attributes.
175 filter_flags: if given, only attributes with (all) the specified
176 flags will be returned.
178 exclude: if True, only attributes without (any of) the specified
179 flags will be returned.
181 attributes = self._attributes
182 if filter_flags != None:
183 def filter_attrs(attr_data):
184 (attr_id, attr) = attr_data
185 return attr.has_flag(filter_flags) == (not exclude)
186 attributes = dict(filter(filter_attrs, attributes.iteritems()))
187 return attributes.keys()
189 def set_attribute_value(self, name, value):
190 self._attributes[name].value = value
192 def get_attribute_value(self, name):
193 return self._attributes[name].value
195 def get_attribute_help(self, name):
196 return self._attributes[name].help
198 def get_attribute_type(self, name):
199 return self._attributes[name].type
201 def get_attribute_range(self, name):
202 if not self._attributes[name].range:
204 return self._attributes[name].range
206 def get_attribute_allowed(self, name):
207 return self._attributes[name].allowed
209 def get_attribute_category(self, name):
210 return self._attributes[name].category
212 def is_attribute_design_invisible(self, name):
213 return self._attributes[name].is_design_invisible
215 def is_attribute_design_read_only(self, name):
216 return self._attributes[name].is_design_read_only
218 def is_attribute_exec_invisible(self, name):
219 return self._attributes[name].is_exec_invisible
221 def is_attribute_exec_read_only(self, name):
222 return self._attributes[name].is_exec_read_only
224 def is_attribute_exec_immutable(self, name):
225 return self._attributes[name].is_exec_immutable
227 def is_attribute_metadata(self, name):
228 return self._attributes[name].is_metadata
230 def has_attribute_no_default_value(self, name):
231 return self._attributes[name].has_no_default_value
233 def is_attribute_modified(self, name):
234 return self._attributes[name].modified
236 def is_attribute_value_valid(self, name, value):
237 return self._attributes[name].is_valid_value(value)
239 def add_attribute(self, name, help, type, value = None, range = None,
240 allowed = None, flags = Attribute.NoFlags, validation_function = None,
242 if name in self._attributes:
243 raise AttributeError("Attribute %s already exists" % name)
244 attribute = Attribute(name, help, type, value, range, allowed, flags,
245 validation_function, category)
246 self._attributes[name] = attribute
248 def del_attribute(self, name):
249 del self._attributes[name]
251 def has_attribute(self, name):
252 return name in self._attributes
255 self._attributes = dict()