added category to attributes
[nepi.git] / src / nepi / core / attributes.py
index 0f0b6fc..321e5c9 100644 (file)
@@ -16,6 +16,14 @@ class Attribute(object):
         DOUBLE, 
         INTEGER
     ]
+    
+    type_parsers = {
+        STRING : str,
+        BOOL : lambda x : str(x).lower() in ("1","on","yes","true"),
+        ENUM : str,
+        DOUBLE : float,
+        INTEGER : int,
+    }
 
     ### Attribute Flags
     NoFlags     = 0x00
@@ -24,15 +32,15 @@ class Attribute(object):
     # Attribute is read only and can't be modified by the user
     # Note: ReadOnly implies DesignOnly
     ReadOnly    = 0x03
-    # Attribute is invisible to the user
-    # Note: Invisible implies ReadOnly and DesignOnly
-    Invisible   = 0x07
+    # Attribute is invisible to the user but can be modified
+    Invisible   = 0x04
     # Attribute has no default value in the testbed instance. 
     # So it needs to be set explicitely
     HasNoDefaultValue = 0x08
 
     def __init__(self, name, help, type, value = None, range = None,
-        allowed = None, flags = NoFlags, validation_function = None):
+        allowed = None, flags = NoFlags, validation_function = None, 
+        category = None):
         if not type in Attribute.types:
             raise AttributeError("invalid type %s " % type)
         self._name = name
@@ -46,6 +54,7 @@ class Attribute(object):
         self._allowed = allowed
         self._validation_function = validation_function
         self._modified = False
+        self._category = category
 
     @property
     def name(self):
@@ -64,7 +73,7 @@ class Attribute(object):
         return self._flags
 
     @property
-    def invsible(self):
+    def invisible(self):
         return (self._flags & Attribute.Invisible) == Attribute.Invisible
 
     @property
@@ -84,6 +93,10 @@ class Attribute(object):
     def modified(self):
         return self._modified
 
+    @property
+    def category(self):
+        return self._category
+
     @property
     def range(self):
         return self._range
@@ -100,9 +113,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,12 +122,17 @@ 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])
 
     def _is_in_allowed_values(self, value):
-        return not self.allowed or value in self._allowed
+        return not self._allowed or value in self._allowed
 
     def _is_valid(self, value):
         return not self._validation_function or \
@@ -134,7 +150,7 @@ class AttributesMap(object):
         return self._attributes.values()
 
     @property
-    def attributes_name(self):
+    def attributes_list(self):
         return self._attributes.keys()
 
     def set_attribute_value(self, name, value):
@@ -157,6 +173,9 @@ class AttributesMap(object):
     def get_attribute_allowed(self, name):
         return self._attributes[name].allowed
 
+    def get_attribute_category(self, name):
+        return self._attributes[name].category
+
     def is_attribute_read_only(self, name):
         return self._attributes[name].read_only
 
@@ -172,12 +191,16 @@ 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):
+        allowed = None, flags = Attribute.NoFlags, validation_function = None,
+        category = None):
         if name in self._attributes:
             raise AttributeError("Attribute %s already exists" % name)
         attribute = Attribute(name, help, type, value, range, allowed, flags,
-                validation_function)
+                validation_function, category)
         self._attributes[name] = attribute
 
     def del_attribute(self, name):