| | 50 | def __get__(self, instance, owner): |
|---|
| | 51 | """If requesting an extension point member, return a list of components |
|---|
| | 52 | that declare to implement the extension point interface.""" |
|---|
| | 53 | xtnpt = instance._extension_points.get(self.name) |
|---|
| | 54 | extensions = ComponentMeta._registry.get(xtnpt.interface, []) |
|---|
| | 55 | return [instance.compmgr[cls] for cls in extensions |
|---|
| | 56 | if instance.compmgr[cls]] |
|---|
| | 57 | |
|---|
| | 58 | class SelectedExtensionPoint(ExtensionPoint): |
|---|
| | 59 | """ Implementation of a 'singleton' extension point. Looks up the name of |
|---|
| | 60 | the selected interface implementation to use by calling |
|---|
| | 61 | ComponentManager.get_selected_implementation(interface), which should return |
|---|
| | 62 | the class name of the selected component. Caches the selected implementation |
|---|
| | 63 | for speed. """ |
|---|
| | 64 | def __init__(self, interface): |
|---|
| | 65 | ExtensionPoint.__init__(self, interface) |
|---|
| | 66 | self._implementation = None |
|---|
| | 67 | |
|---|
| | 68 | def __get__(self, instance, owner): |
|---|
| | 69 | """ Return the selected interface implementation, or throw an exception if |
|---|
| | 70 | not found. """ |
|---|
| | 71 | if self._implementation: |
|---|
| | 72 | return self._implementation |
|---|
| | 73 | xtnpt = instance._extension_points.get(self.name) |
|---|
| | 74 | extensions = ComponentMeta._registry.get(xtnpt.interface, []) |
|---|
| | 75 | seek = instance.compmgr.get_selected_implementation(xtnpt.interface) |
|---|
| | 76 | if seek: |
|---|
| | 77 | for extension in extensions: |
|---|
| | 78 | if extension.__name__ == seek: |
|---|
| | 79 | self._implementation = instance.compmgr[extension] |
|---|
| | 80 | return self._implementation |
|---|
| | 81 | raise ComponentError("can't find selected interface '%s' named '%s'" % (xtnpt.interface.__name__, seek)) |
|---|
| | 82 | else: |
|---|
| | 83 | raise ComponentError("no component active for '%s' interface" % xtnpt.interface.__name__) |
|---|
| 142 | | |
|---|
| 143 | | def __getattr__(self, name): |
|---|
| 144 | | """If requesting an extension point member, return a list of components |
|---|
| 145 | | that declare to implement the extension point interface.""" |
|---|
| 146 | | xtnpt = self._extension_points.get(name) |
|---|
| 147 | | if xtnpt: |
|---|
| 148 | | extensions = ComponentMeta._registry.get(xtnpt.interface, []) |
|---|
| 149 | | return [self.compmgr[cls] for cls in extensions |
|---|
| 150 | | if self.compmgr[cls]] |
|---|
| 151 | | raise AttributeError, name |
|---|
| 152 | | |
|---|