return self._value
def set_value(self, value):
- if self._is_in_range(value) and \
- self._is_in_allowed_values(value) and \
- self._is_valid(value):
+ if self.is_valid_value(value):
self._value = value
self._modified = True
else:
value = property(get_value, set_value)
+ def is_valid_value(self, value):
+ return self._is_in_range(value) and \
+ self._is_in_allowed_values(value) and \
+ self._is_valid(value)
+
def _is_in_range(self, value):
return not self.range or \
(value >= self.range[0] and value <= self.range[1])
def is_attribute_modified(self, name):
return self._attributes[name].modified
+ def is_attribute_value_valid(self, name, value):
+ return self._attributes[name].is_valid_value(value)
+
def add_attribute(self, name, help, type, value = None, range = None,
allowed = None, flags = Attribute.NoFlags, validation_function = None):
if name in self._attributes:
def defer_configure(self, name, value):
if not self._attributes.has_attribute(name):
- raise RuntimeError("Invalid attribute %s for testbed" % name)
+ raise AttributeError("Invalid attribute %s for testbed" % name)
# Validation
self._attributes.set_attribute_value(name, value)
self._configure[name] = value
def defer_create(self, guid, factory_id):
if factory_id not in self._factories:
- raise RuntimeError("Invalid element type %s for testbed version %s" %
+ raise AttributeError("Invalid element type %s for testbed version %s" %
(factory_id, self._testbed_version))
if guid in self._create:
- raise RuntimeError("Cannot add elements with the same guid: %d" %
+ raise AttributeError("Cannot add elements with the same guid: %d" %
guid)
self._create[guid] = factory_id
factory_id = self._create[guid]
factory = self._factories[factory_id]
if not factory.box_attributes.has_attribute(name):
- raise RuntimeError("Invalid attribute %s for element type %s" %
+ raise AttributeError("Invalid attribute %s for element type %s" %
(name, factory_id))
- factory.box_attributes.set_attribute_value(name, value)
+ if not factory.box_attributes.is_attribute_value_valid(name, value):
+ raise AttributeError("Invalid value %s for attribute %s" % \
+ (value, name))
if guid not in self._create_set:
self._create_set[guid] = dict()
self._create_set[guid][name] = value
factory_id = self._create[guid]
factory = self._factories[factory_id]
if not factory.has_attribute(name):
- raise RuntimeError("Invalid attribute %s for element type %s" %
+ raise AttributeError("Invalid attribute %s for element type %s" %
(name, factory_id))
- factory.set_attribute_value(name, value)
+ if not factory.is_attribute_value_valid(name, value):
+ raise AttributeError("Invalid value %s for attribute %s" % \
+ (value, name))
if guid not in self._factory_set:
self._factory_set[guid] = dict()
self._factory_set[guid][name] = value
factory_id = self._create[guid]
factory = self._factories[factory_id]
if not factory.box_attributes.has_attribute(name):
- raise RuntimeError("Invalid attribute %s for element type %s" %
+ raise AttributeError("Invalid attribute %s for element type %s" %
(name, factory_id))
if self._started and factory.is_attribute_design_only(name):
- raise RuntimeError("Attribute %s can only be modified during experiment design" % name)
- factory.box_attributes.set_attribute_value(name, value)
+ raise AttributeError("Attribute %s can only be modified during experiment design" % name)
+ if not factory.box_attributes.is_attribute_value_valid(name, value):
+ raise AttributeError("Invalid value %s for attribute %s" % \
+ (value, name))
if guid not in self._set:
self._set[guid] = dict()
if time not in self._set[guid]: