From 377bd1f67871f4e9f5bb99cabbf0cf65abb66103 Mon Sep 17 00:00:00 2001 From: Alina Quereilhac Date: Sun, 1 May 2011 12:01:10 +0200 Subject: [PATCH] added is_valid_value as a function for attributes.Attribute --- src/nepi/core/attributes.py | 12 +++++++++--- src/nepi/core/testbed_impl.py | 26 ++++++++++++++++---------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/nepi/core/attributes.py b/src/nepi/core/attributes.py index b6bf9139..258772c7 100644 --- a/src/nepi/core/attributes.py +++ b/src/nepi/core/attributes.py @@ -100,9 +100,7 @@ class Attribute(object): 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: @@ -111,6 +109,11 @@ class Attribute(object): 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]) @@ -172,6 +175,9 @@ class AttributesMap(object): 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: diff --git a/src/nepi/core/testbed_impl.py b/src/nepi/core/testbed_impl.py index e0608bcb..d408eb45 100644 --- a/src/nepi/core/testbed_impl.py +++ b/src/nepi/core/testbed_impl.py @@ -51,17 +51,17 @@ class TestbedController(execute.TestbedController): 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 @@ -71,9 +71,11 @@ class TestbedController(execute.TestbedController): 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 @@ -84,9 +86,11 @@ class TestbedController(execute.TestbedController): 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 @@ -292,11 +296,13 @@ class TestbedController(execute.TestbedController): 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]: -- 2.47.0