Package traylib :: Module config
[frames] | no frames]

Source Code for Module traylib.config

  1   
2 -class Config(object):
3 """ 4 A C{Config} is an object containing attributes. They can be added using the 5 L{add_attribute()} method, changed using L{set_attribute()} and accessed 6 using L{get_attribute()}. 7 If an attribute is changed, an update function is called on all configurable 8 objects registered via L{add_configurable()}. 9 """ 10
11 - def __init__(self):
12 """ 13 Creates a new C{Config}. 14 """ 15 object.__init__(self) 16 17 self.__attributes = {} 18 self.__objects = [] 19 20 assert self.__attributes == {} 21 assert self.__objects == []
22
23 - def add_attribute(self, key, value, update_func = None, set_func = None):
24 """ 25 Adds an attribute to the C{Config}. 26 27 @param key: The key. 28 @param value: The value. 29 @param update_func: The name of a method that gets called on all 30 configurable objects (registered via L{add_configurable()}) when the 31 value of the attribute has changed. 32 @param set_func: The name of a method with signature 33 C{set_func(old_value, new_value)} that gets called on the C{Config} 34 when the value of the attribute has changed. 35 """ 36 assert update_func == None or isinstance(update_func, str) 37 assert set_func == None or isinstance(set_func, str) 38 39 self.__attributes[key] = (value, update_func, set_func) 40 if set_func: 41 getattr(self, set_func)(None, value) 42 43 assert key in self.__attributes 44 assert self.__attributes[key] == (value, update_func, set_func)
45
46 - def set_attribute(self, key, value):
47 """ 48 Sets the attribute identified by C{key} to the value C{value}. 49 50 @param key: The key. 51 @param value: The new value. 52 """ 53 assert key in self.__attributes 54 55 old_value, update_func, set_func = self.__attributes[key] 56 if old_value == value: 57 return 58 self.__attributes[key] = (value, update_func, set_func) 59 if set_func: 60 getattr(self, set_func)(old_value, value) 61 62 assert key in self.__attributes 63 assert self.__attributes[key] == (value, update_func, set_func) 64 65 if update_func: 66 for obj in self.__objects: 67 if hasattr(obj, update_func): 68 getattr(obj, update_func)()
69
70 - def get_attribute(self, key):
71 """ 72 @param key: The key. 73 @return: The value associated with the given C{key}. 74 """ 75 assert key in self.__attributes 76 77 value, update_func, set_func = self.__attributes[key] 78 return value
79
80 - def add_configurable(self, obj):
81 """ 82 Registers a configurable object. 83 84 @param obj: The object. 85 """ 86 assert obj not in self.__objects 87 88 self.__objects.append(obj) 89 90 assert obj in self.__objects
91
92 - def remove_configurable(self, obj):
93 """ 94 Unregisters a configurable object. 95 96 @param obj: The object. 97 """ 98 assert obj in self.__objects 99 100 self.__objects.remove(obj) 101 102 assert obj not in self.__objects
103
104 - def has_configurable(self, obj):
105 """ 106 @param obj: The configurable object. 107 @return: C{True} if the configurable object is registered with the 108 C{Config}. 109 """ 110 return obj in self.__objects
111