/usr/lib/python2.7/dist-packages/envisage/application.py is in python-envisage 4.4.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 | """ An extensible, pluggable, application. """
# Standard library imports.
import logging, os
# Enthought library imports.
from traits.etsconfig.api import ETSConfig
from apptools.preferences.api import IPreferences, ScopedPreferences
from apptools.preferences.api import set_default_preferences
from traits.api import Delegate, Event, HasTraits, Instance, Str
from traits.api import VetoableEvent, implements
# Local imports.
from i_application import IApplication
from i_extension_registry import IExtensionRegistry
from i_import_manager import IImportManager
from i_plugin_manager import IPluginManager
from i_service_registry import IServiceRegistry
from application_event import ApplicationEvent
from import_manager import ImportManager
# Logging.
logger = logging.getLogger(__name__)
class Application(HasTraits):
""" An extensible, pluggable, application.
This class handles the common case for non-GUI applications, and it is
intended to be subclassed to change start/stop behaviour etc.
"""
implements(IApplication)
#### 'IApplication' interface #############################################
# The application's globally unique identifier.
id = Str
# The name of a directory (created for you) to which the application can
# read and write non-user accessible data, i.e. configuration information,
# preferences, etc.
home = Str
# The name of a directory (created for you upon access) to which the
# application can read and write user-accessible data, e.g. projects created
# by the user.
user_data = Str
# The root preferences node.
preferences = Instance(IPreferences)
#### Events ####
# Fired when the application is starting.
starting = VetoableEvent(ApplicationEvent)
# Fired when all plugins have been started.
started = Event(ApplicationEvent)
# Fired when the application is stopping.
stopping = VetoableEvent(ApplicationEvent)
# Fired when all plugins have been stopped.
stopped = Event(ApplicationEvent)
#### 'IPluginManager' interface ###########################################
#### Events ####
# Fired when a plugin has been added.
plugin_added = Delegate('plugin_manager', modify=True)
# Fired when a plugin has been removed.
plugin_removed = Delegate('plugin_manager', modify=True)
#### 'Application' interface ##############################################
# These traits allow application developers to build completely different
# styles of extensible application. It allows Envisage to be used as a
# framework for frameworks ;^)
#
# The extension registry.
extension_registry = Instance(IExtensionRegistry)
# The plugin manager (starts and stops plugins etc).
plugin_manager = Instance(IPluginManager)
# The service registry.
service_registry = Instance(IServiceRegistry)
#### Private interface ####################################################
# The import manager.
_import_manager = Instance(IImportManager, factory=ImportManager)
###########################################################################
# 'object' interface.
###########################################################################
def __init__(self, plugins=None, **traits):
""" Constructor.
We allow the caller to specify an initial list of plugins, but the list
itself is not part of the public API. To add and remove plugins after
after construction, use the 'add_plugin' and 'remove_plugin' methods
respectively. The application is also iterable, so to iterate over the
plugins use 'for plugin in application: ...'.
"""
super(Application, self).__init__(**traits)
# fixme: We have to initialize the application home here (i.e. we can't
# wait until the 'home' trait is accessed) because the scoped
# preferences uses 'ETSConfig.application' home as the name of the
# default preferences file.
self._initialize_application_home()
# Set the default preferences node used by the preferences package.
# This allows 'PreferencesHelper' and 'PreferenceBinding' instances to
# be used as more convenient ways to access preferences.
#
# fixme: This is another sneaky global!
set_default_preferences(self.preferences)
# We allow the caller to specify an initial list of plugins, but the
# list itself is not part of the public API. To add and remove plugins
# after construction, use the 'add_plugin' and 'remove_plugin' methods
# respectively. The application is also iterable, so to iterate over
# the plugins use 'for plugin in application: ...'.
if plugins is not None:
map(self.add_plugin, plugins)
return
###########################################################################
# 'IApplication' interface.
###########################################################################
#### Trait initializers ###################################################
def _home_default(self):
""" Trait initializer. """
return ETSConfig.application_home
def _user_data_default(self):
""" Trait initializer. """
user_data = os.path.join(
ETSConfig.user_data, self.id
)
# Make sure it exists!
if not os.path.exists(user_data):
os.makedirs(user_data)
return user_data
def _preferences_default(self):
""" Trait initializer. """
return ScopedPreferences()
#### Methods ##############################################################
def run(self):
""" Run the application. """
if self.start():
self.stop()
return
###########################################################################
# 'IExtensionRegistry' interface.
###########################################################################
def add_extension_point_listener(self, listener, extension_point_id=None):
""" Add a listener for extensions being added/removed. """
self.extension_registry.add_extension_point_listener(
listener, extension_point_id
)
return
def add_extension_point(self, extension_point):
""" Add an extension point. """
self.extension_registry.add_extension_point(extension_point)
return
def get_extensions(self, extension_point_id):
""" Return a list containing all contributions to an extension point.
"""
return self.extension_registry.get_extensions(extension_point_id)
def get_extension_point(self, extension_point_id):
""" Return the extension point with the specified Id. """
return self.extension_registry.get_extension_point(extension_point_id)
def get_extension_points(self):
""" Return all extension points that have been added to the registry.
"""
return self.extension_registry.get_extension_points()
def remove_extension_point_listener(self,listener,extension_point_id=None):
""" Remove a listener for extensions being added/removed. """
self.extension_registry.remove_extension_point_listener(
listener, extension_point_id
)
return
def remove_extension_point(self, extension_point_id):
""" Remove an extension point. """
self.extension_registry.remove_extension_point(extension_point_id)
return
def set_extensions(self, extension_point_id, extensions):
""" Set the extensions contributed to an extension point. """
self.extension_registry.set_extensions(extension_point_id, extensions)
return
###########################################################################
# 'IImportManager' interface.
###########################################################################
def import_symbol(self, symbol_path):
""" Import the symbol defined by the specified symbol path. """
return self._import_manager.import_symbol(symbol_path)
###########################################################################
# 'IPluginManager' interface.
###########################################################################
def __iter__(self):
""" Return an iterator over the manager's plugins. """
return iter(self.plugin_manager)
def add_plugin(self, plugin):
""" Add a plugin to the manager. """
self.plugin_manager.add_plugin(plugin)
return
def get_plugin(self, plugin_id):
""" Return the plugin with the specified Id. """
return self.plugin_manager.get_plugin(plugin_id)
def remove_plugin(self, plugin):
""" Remove a plugin from the manager. """
self.plugin_manager.remove_plugin(plugin)
return
def start(self):
""" Start the plugin manager.
Returns True unless the start was vetoed.
"""
# fixme: This method is notionally on the 'IPluginManager' interface
# but that interface knows nothing about the vetoable events etc and
# hence doesn't have a return value.
logger.debug('---------- application starting ----------')
# Lifecycle event.
self.starting = event = self._create_application_event()
if not event.veto:
# Start the plugin manager (this starts all of the manager's
# plugins).
self.plugin_manager.start()
# Lifecycle event.
self.started = self._create_application_event()
logger.debug('---------- application started ----------')
else:
logger.debug('---------- application start vetoed ----------')
return not event.veto
def start_plugin(self, plugin=None, plugin_id=None):
""" Start the specified plugin. """
return self.plugin_manager.start_plugin(plugin, plugin_id)
def stop(self):
""" Stop the plugin manager.
Returns True unless the stop was vetoed.
"""
# fixme: This method is notionally on the 'IPluginManager' interface
# but that interface knows nothing about the vetoable events etc and
# hence doesn't have a return value.
logger.debug('---------- application stopping ----------')
# Lifecycle event.
self.stopping = event = self._create_application_event()
if not event.veto:
# Stop the plugin manager (this stops all of the manager's
# plugins).
self.plugin_manager.stop()
# Save all preferences.
self.preferences.save()
# Lifecycle event.
self.stopped = self._create_application_event()
logger.debug('---------- application stopped ----------')
else:
logger.debug('---------- application stop vetoed ----------')
return not event.veto
def stop_plugin(self, plugin=None, plugin_id=None):
""" Stop the specified plugin. """
return self.plugin_manager.stop_plugin(plugin, plugin_id)
###########################################################################
# 'IServiceRegistry' interface.
###########################################################################
def get_required_service(self, protocol, query='', minimize='',maximize=''):
""" Return the service that matches the specified query.
Raise a 'NoSuchServiceError' exception if no such service exists.
"""
service = self.service_registry.get_required_service(
protocol, query, minimize, maximize
)
return service
def get_service(self, protocol, query='', minimize='', maximize=''):
""" Return at most one service that matches the specified query. """
service = self.service_registry.get_service(
protocol, query, minimize, maximize
)
return service
def get_service_from_id(self, service_id):
""" Return the service with the specified id. """
return self.service_registry.get_service_from_id(service_id)
def get_service_properties(self, service_id):
""" Return the dictionary of properties associated with a service. """
return self.service_registry.get_service_properties(service_id)
def get_services(self, protocol, query='', minimize='', maximize=''):
""" Return all services that match the specified query. """
services = self.service_registry.get_services(
protocol, query, minimize, maximize
)
return services
def register_service(self, protocol, obj, properties=None):
""" Register a service. """
service_id = self.service_registry.register_service(
protocol, obj, properties
)
return service_id
def set_service_properties(self, service_id, properties):
""" Set the dictionary of properties associated with a service. """
self.service_registry.set_service_properties(service_id, properties)
return
def unregister_service(self, service_id):
""" Unregister a service. """
self.service_registry.unregister_service(service_id)
return
###########################################################################
# 'Application' interface.
###########################################################################
#### Trait initializers ###################################################
def _extension_registry_default(self):
""" Trait initializer. """
# Do the import here to emphasize the fact that this is just the
# default implementation and that the application developer is free
# to override it!
from plugin_extension_registry import PluginExtensionRegistry
return PluginExtensionRegistry(plugin_manager=self)
def _plugin_manager_default(self):
""" Trait initializer. """
# Do the import here to emphasize the fact that this is just the
# default implementation and that the application developer is free
# to override it!
from plugin_manager import PluginManager
return PluginManager(application=self)
def _service_registry_default(self):
""" Trait initializer. """
# Do the import here to emphasize the fact that this is just the
# default implementation and that the application developer is free
# to override it!
from service_registry import ServiceRegistry
return ServiceRegistry()
###########################################################################
# Private interface.
###########################################################################
#### Trait change handlers ################################################
# fixme: We have this to make it easier to assign a new plugin manager
# at construction time due to the fact that the plugin manager needs a
# reference to the application and vice-versa, e.g. we can do
#
# application = Application(plugin_manager=EggPluginManager())
#
# If we didn't have this then we would have to do this:-
#
# application = Application()
# application.plugin_manager = EggPluginManager(application=application)
#
# Of course, it would be better if the plugin manager didn't require a
# reference to the application at all (it currently uses it to set the
# 'application' trait of plugin instances - but that is only done for the
# same reason as this (i.e. it is nice to be able to pass plugins into the
# application constructor).
def _plugin_manager_changed(self, trait_name, old, new):
""" Static trait change handler. """
if old is not None:
old.application = None
if new is not None:
new.application = self
return
#### Methods ##############################################################
def _create_application_event(self):
""" Create an application event. """
return ApplicationEvent(application=self)
def _initialize_application_home(self):
""" Initialize the application home directory. """
ETSConfig.application_home = os.path.join(
ETSConfig.application_data, self.id
)
# Make sure it exists!
if not os.path.exists(ETSConfig.application_home):
os.makedirs(ETSConfig.application_home)
return
#### EOF ######################################################################
|