2 # -*- coding: utf-8 -*-
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.
9 self._attributes = dict()
13 return self._attributes.values()
16 def attributes_name(self):
17 return self._attributes.keys()
19 def set_attribute_value(self, name, value):
20 self._attributes[name].value = value
22 def set_attribute_readonly(self, name, readonly = True):
23 self._attributes[name].readonly = (readonly == True)
25 def get_attribute_value(self, name):
26 return self._attributes[name].value
28 def get_attribute_help(self, name):
29 return self._attributes[name].help
31 def get_attribute_type(self, name):
32 return self._attributes[name].type
34 def get_attribute_range(self, name):
35 if not self._attributes[name].range:
37 return self._attributes[name].range
39 def get_attribute_allowed(self, name):
40 return self._attributes[name].allowed
42 def get_attribute_readonly(self, name):
43 return self._attributes[name].readonly
45 def get_attribute_visible(self, name):
46 return self._attributes[name].visible
48 def is_attribute_modified(self, name):
49 return self._attributes[name].modified
51 def add_attribute(self, name, help, type, value = None, range = None,
52 allowed = None, readonly = False, visible = True,
53 validation_function = None):
54 if name in self._attributes:
55 raise AttributeError("Attribute %s already exists" % name)
56 attribute = Attribute(name, help, type, value, range, allowed, readonly,
57 visible, validation_function)
58 self._attributes[name] = attribute
60 def del_attribute(self, name):
61 del self._attributes[name]
63 def has_attribute(self, name):
64 return name in self._attributes
67 self._attributes = dict()
69 class Attribute(object):
70 STRING, BOOL, ENUM, DOUBLE, INTEGER = (
71 "STRING", "BOOL", "ENUM", "DOUBLE", "INTEGER")
73 types = [STRING, BOOL, ENUM, DOUBLE, INTEGER]
75 def __init__(self, name, help, type, value = None, range = None,
76 allowed = None, readonly = False, visible = True,
77 validation_function = None):
78 if not type in Attribute.types:
79 raise AttributeError("invalid type %s " % type)
84 self._validation_function = validation_function
85 # readonly attributes can be seen but not changed by users
86 self._readonly = (readonly == True)
87 # invisible attributes cannot be seen or changed by users
88 self._visible = (visible == True)
89 self._modified = False
90 # range: max and min possible values
92 # list of possible values
93 self._allowed = allowed
109 return self._readonly
113 return self._modified
124 def validation_function(self):
125 return self._validation_function
130 def set_value(self, value):
131 if self._is_in_range(value) and \
132 self._is_in_allowed_values(value) and \
133 self._is_valid(value):
135 self._modified = True
137 raise RuntimeError("Invalid value %s for attribute %s" %
138 (str(value), self.name))
140 value = property(get_value, set_value)
142 def _is_in_range(self, value):
143 return not self.range or \
144 (value >= self.range[0] and value <= self.range[1])
146 def _is_in_allowed_values(self, value):
147 return not self.allowed or value in self._allowed
149 def _is_valid(self, value):
150 return not self._validation_function or self._validation_function(value)