description classes are generic classes with no testbed specific code.
[nepi.git] / src / nepi / core / attributes.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 class AttributesMap(object):
5     """AttributesMap is the base class for every object whose attributes 
6     are going to be manipulated by the end-user in a script or GUI.
7     """
8     def __init__(self):
9         self._attributes = dict()
10
11     @property
12     def attributes_name(self):
13         return set(self._attributes.keys())
14
15     def set_attribute_value(self, name, value):
16         self._attributes[name].value = value
17
18     def set_attribute_readonly(self, name, value):
19         self._attributes[name].readonly = value
20
21     def get_attribute_value(self, name):
22         return self._attributes[name].value
23
24     def get_attribute_help(self, name):
25         return self._attributes[name].help
26
27     def get_attribute_type(self, name):
28         return self._attributes[name].type
29
30     def get_attribute_range(self, name):
31         return self._attributes[name].range
32
33     def get_attribute_allowed(self, name):
34         return self._attributes[name].allowed
35
36     def get_attribute_readonly(self, name):
37         return self._attributes[name].readonly
38
39     def add_attribute(self, name, help, type, value = None, range = None,
40         allowed = None, readonly = False, validation_function = None):
41         if name in self._attributes:
42             raise AttributeError('Attribute %s already exists' % name))
43         attribute = Attribute(name, help, type, value, range, allowed, readonly,
44                 validation_function)
45         self._attributes[name] = attribute
46
47     def del_attribute(self, name):
48         del self._attributes[name]
49
50     def has_attribute(self, name):
51         return name in self._attributes    
52     
53     def destroy(self):
54         self._attributes = dict()
55
56 class Attribute(object):
57     STRING , BOOL, ENUM, DOUBLE, INTEGER, ENDPOINT, TIME = (
58                 "STRING", "BOOL", "ENUM", "DOUBLE", "INTEGER", "ENDPOINT", "TIME")
59
60     types = [STRING, BOOL, ENUM, DOUBLE, INTEGER, ENDPOINT, TIME]
61
62     def __init__(self, name, help, type, value = None, range = None,
63         allowed = None, readonly = False, validation_function = None):
64         if not type in Attribute.types:
65             raise AttributeError("invalid type %s " % type)
66         self.name = name
67         self.type = type
68         self.help = help
69         self._value = value
70         self._validation_function = validation_function
71         self.readonly = (readonly == True)
72         self.modified = False
73         # range: max and min possible values
74         self.range = range
75         # list of possible values
76         self.allowed = allowed
77
78     def get_value(self):
79         return self._value
80
81     def set_value(self, value):
82         func = self._validation_function
83         if not func or func(value):
84             self._value = value
85         else:
86             raise RuntimeError("Invalid value %s for attribute %s" %
87                     (str(value), self.name))
88
89     value = property(get_value, set_value)
90