| 1 |
# (C) Copyright 2007-2025 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
""" The Envisage core plugin. """ |
| 11 |
|
| 12 |
from contextlib import closing |
| 13 |
|
| 14 |
from traits.api import List, on_trait_change, Str |
| 15 |
|
| 16 |
# Enthought library imports. |
| 17 |
from envisage.extension_point import ExtensionPoint |
| 18 |
from envisage.plugin import Plugin |
| 19 |
from envisage.service_offer import ServiceOffer |
| 20 |
|
| 21 |
|
| 22 |
class CorePlugin(Plugin): |
| 23 |
"""The Envisage core plugin. |
| 24 |
|
| 25 |
The core plugin offers facilities that are generally useful when building |
| 26 |
extensible applications such as adapters and hooks etc. It does |
| 27 |
not contain anything to do with user interfaces! |
| 28 |
|
| 29 |
The core plugin should be started before any other plugin. It is up to |
| 30 |
the plugin manager to do this. |
| 31 |
|
| 32 |
""" |
| 33 |
|
| 34 |
#: Extension point ID for preferences |
| 35 |
PREFERENCES = "envisage.preferences" |
| 36 |
|
| 37 |
#: Extension point ID for service offers |
| 38 |
SERVICE_OFFERS = "envisage.service_offers" |
| 39 |
|
| 40 |
#### 'IPlugin' interface ################################################## |
| 41 |
|
| 42 |
#: The plugin's unique identifier. |
| 43 |
id = "envisage.core" |
| 44 |
|
| 45 |
#: The plugin's name (suitable for displaying to the user). |
| 46 |
name = "Core" |
| 47 |
|
| 48 |
#### Extension points offered by this plugin ############################## |
| 49 |
|
| 50 |
#: preferences ExtensionPoint |
| 51 |
preferences = ExtensionPoint( |
| 52 |
List(Str), |
| 53 |
id=PREFERENCES, |
| 54 |
desc=""" |
| 55 |
|
| 56 |
Preferences files allow plugins to contribute default values for |
| 57 |
user preferences. Each contributed string must be the URL of a |
| 58 |
file-like object that contains preferences values. |
| 59 |
|
| 60 |
e.g. |
| 61 |
|
| 62 |
'pkgfile://envisage/preferences.ini' |
| 63 |
|
| 64 |
- this looks for the 'preferences.ini' file in the 'envisage' |
| 65 |
package. |
| 66 |
|
| 67 |
'file://C:/tmp/preferences.ini' |
| 68 |
|
| 69 |
- this looks for the 'preferences.ini' file in 'C:/tmp' |
| 70 |
|
| 71 |
'http://some.website/preferences.ini' |
| 72 |
|
| 73 |
- this looks for the 'preferences.ini' document on the 'some.website' |
| 74 |
web site! |
| 75 |
|
| 76 |
The files themselves are parsed using the excellent 'ConfigObj' |
| 77 |
package. For detailed documentation please go to:- |
| 78 |
|
| 79 |
http://www.voidspace.org.uk/python/configobj.html |
| 80 |
|
| 81 |
""", |
| 82 |
) |
| 83 |
|
| 84 |
@on_trait_change("preferences_items") |
| 85 |
def _update_preferences(self, event): |
| 86 |
"""React to new preferencess being *added*. |
| 87 |
|
| 88 |
Note that we don't currently do anything if preferences are *removed*. |
| 89 |
|
| 90 |
""" |
| 91 |
|
| 92 |
self._load_preferences(event.added) |
| 93 |
|
| 94 |
#: service offers ExtensionPoint |
| 95 |
service_offers = ExtensionPoint( |
| 96 |
List(ServiceOffer), |
| 97 |
id=SERVICE_OFFERS, |
| 98 |
desc=""" |
| 99 |
|
| 100 |
Services are simply objects that a plugin wants to make available to |
| 101 |
other plugins. This extension point allows you to offer services |
| 102 |
that are created 'on-demand'. |
| 103 |
|
| 104 |
e.g. |
| 105 |
|
| 106 |
my_service_offer = ServiceOffer( |
| 107 |
protocol = 'acme.IMyService', |
| 108 |
factory = an_object_or_a_callable_that_creates_one, |
| 109 |
properties = {'a dictionary' : 'that is passed to the factory'} |
| 110 |
) |
| 111 |
|
| 112 |
See the documentation for 'ServiceOffer' for more details. |
| 113 |
|
| 114 |
""", |
| 115 |
) |
| 116 |
|
| 117 |
@on_trait_change("service_offers_items") |
| 118 |
def _register_new_services(self, event): |
| 119 |
"""React to new service offers being *added*. |
| 120 |
|
| 121 |
Note that we don't currently do anything if services are *removed* as |
| 122 |
we have no facility to let users of the service know that the offer |
| 123 |
has been retracted. |
| 124 |
|
| 125 |
""" |
| 126 |
for service in event.added: |
| 127 |
self._register_service_offer(service) |
| 128 |
|
| 129 |
#### Contributions to extension points made by this plugin ################ |
| 130 |
|
| 131 |
# None. |
| 132 |
|
| 133 |
########################################################################### |
| 134 |
# 'IPlugin' interface. |
| 135 |
########################################################################### |
| 136 |
|
| 137 |
def start(self): |
| 138 |
"""Start the plugin.""" |
| 139 |
# Load all contributed preferences files into the application's root |
| 140 |
# preferences node. |
| 141 |
self._load_preferences(self.preferences) |
| 142 |
|
| 143 |
# Register all service offers. |
| 144 |
# |
| 145 |
# These services are unregistered by the default plugin activation |
| 146 |
# strategy (due to the fact that we store the service ids in this |
| 147 |
# specific trait!). |
| 148 |
self._service_ids = self._register_service_offers(self.service_offers) |
| 149 |
|
| 150 |
########################################################################### |
| 151 |
# Private interface. |
| 152 |
########################################################################### |
| 153 |
|
| 154 |
def _load_preferences(self, preferences): |
| 155 |
"""Load all contributed preferences into a preferences node.""" |
| 156 |
|
| 157 |
# Enthought library imports. |
| 158 |
from envisage.resource.api import ResourceManager |
| 159 |
|
| 160 |
# We add the plugin preferences to the default scope. The default scope |
| 161 |
# is a transient scope which means that (quite nicely ;^) we never |
| 162 |
# save the actual default plugin preference values. They will only get |
| 163 |
# saved if a value has been set in another (persistent) scope - which |
| 164 |
# is exactly what happens in the preferences UI. |
| 165 |
default = self.application.preferences.node("default/") |
| 166 |
|
| 167 |
# The resource manager is used to find the preferences files. |
| 168 |
resource_manager = ResourceManager() |
| 169 |
for resource_name in preferences: |
| 170 |
with closing(resource_manager.file(resource_name)) as f: |
| 171 |
default.load(f) |
| 172 |
|
| 173 |
def _register_service_offers(self, service_offers): |
| 174 |
"""Register a list of service offers.""" |
| 175 |
|
| 176 |
return list(map(self._register_service_offer, service_offers)) |
| 177 |
|
| 178 |
def _register_service_offer(self, service_offer): |
| 179 |
"""Register a service offer.""" |
| 180 |
|
| 181 |
service_id = self.application.register_service( |
| 182 |
protocol=service_offer.protocol, |
| 183 |
obj=service_offer.factory, |
| 184 |
properties=service_offer.properties, |
| 185 |
) |
| 186 |
|
| 187 |
return service_id |