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()
12 def attributes_name(self):
13 return set(self._attributes.keys())
15 def set_attribute_value(self, name, value):
16 self._attributes[name].value = value
18 def set_attribute_readonly(self, name, value):
19 self._attributes[name].readonly = value
21 def get_attribute_value(self, name):
22 return self._attributes[name].value
24 def get_attribute_help(self, name):
25 return self._attributes[name].help
27 def get_attribute_type(self, name):
28 return self._attributes[name].type
30 def get_attribute_range(self, name):
31 return self._attributes[name].range
33 def get_attribute_allowed(self, name):
34 return self._attributes[name].allowed
36 def get_attribute_readonly(self, name):
37 return self._attributes[name].readonly
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,
45 self._attributes[name] = attribute
47 def del_attribute(self, name):
48 del self._attributes[name]
50 def has_attribute(self, name):
51 return name in self._attributes
54 self._attributes = dict()
56 class Attribute(object):
57 STRING , BOOL, ENUM, DOUBLE, INTEGER, ENDPOINT, TIME = (
58 "STRING", "BOOL", "ENUM", "DOUBLE", "INTEGER", "ENDPOINT", "TIME")
60 types = [STRING, BOOL, ENUM, DOUBLE, INTEGER, ENDPOINT, TIME]
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)
70 self._validation_function = validation_function
71 self.readonly = (readonly == True)
73 # range: max and min possible values
75 # list of possible values
76 self.allowed = allowed
81 def set_value(self, value):
82 func = self._validation_function
83 if not func or func(value):
86 raise RuntimeError("Invalid value %s for attribute %s" %
87 (str(value), self.name))
89 value = property(get_value, set_value)