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
|
"""
The Application object. One application should be available.
All important services are present in the application object:
- plugin manager
- undo manager
- main window
- UML element factory
- action sets
"""
import pkg_resources
from zope import component
from gaphor.interfaces import IService, IEventFilter
from gaphor.event import ServiceInitializedEvent, ServiceShutdownEvent
import gaphor.UML
class _Application(object):
"""
The Gaphor application is started from the Application instance. It behaves
like a singleton in many ways.
The Application is responsible for loading services and plugins. Services
are registered as "utilities" in the application registry.
Methods are provided that wrap zope.component's handle, adapter and
subscription registrations. In addition to registration methods also
unregister methods are provided. This way services can be properly
unregistered on shutdown for example.
"""
# interface.implements(IApplication)
def __init__(self):
self._uninitialized_services = {}
self.init_components()
self._event_filter = None
def init(self, services=None):
"""
Initialize the application.
"""
self.load_services(services)
self.init_all_services()
def init_components(self):
"""
Initialize application level component registry.
Frequently used query methods are overridden on the zope.component
module.
"""
#self._components = component.getGlobalSiteManager()
#return
self._components = component.registry.Components(name='app',
bases=(component.getGlobalSiteManager(),))
# Make sure component.handle() and query methods works.
# TODO: eventually all queries should be done through the Application
# instance.
component.handle = self.handle
component.getMultiAdapter = self._components.getMultiAdapter
component.queryMultiAdapter = self._components.queryMultiAdapter
component.getAdapter = self._components.getAdapter
component.queryAdapter = self._components.queryAdapter
component.getAdapters = self._components.getAdapters
component.getUtility = self._components.getUtility
component.queryUtility = self._components.queryUtility
component.getUtilitiesFor = self._components.getUtilitiesFor
def load_services(self, services=None):
"""
Load services from resources.
Services are registered as utilities in zope.component.
Service should provide an interface gaphor.interfaces.IService.
"""
for ep in pkg_resources.iter_entry_points('gaphor.services'):
log.debug('found entry point service.%s' % ep.name)
cls = ep.load()
if not IService.implementedBy(cls):
raise 'MisConfigurationException', 'Entry point %s doesn''t provide IService' % ep.name
if services is None or ep.name in services:
srv = cls()
self._uninitialized_services[ep.name] = srv
def init_all_services(self):
while self._uninitialized_services:
self.init_service(self._uninitialized_services.iterkeys().next())
def init_service(self, name):
"""
Initialize a not yet initialized service.
Raises ComponentLookupError if the service has nor been found
"""
try:
srv = self._uninitialized_services.pop(name)
except KeyError:
raise component.ComponentLookupError(IService, name)
else:
log.info('initializing service service.%s' % name)
srv.init(self)
self._components.registerUtility(srv, IService, name)
self.handle(ServiceInitializedEvent(name, srv))
return srv
distribution = property(lambda s: pkg_resources.get_distribution('gaphor'),
doc='Get the PkgResources distribution for Gaphor')
def get_service(self, name):
try:
return self._components.getUtility(IService, name)
except component.ComponentLookupError:
return self.init_service(name)
def run(self):
import gtk
gtk.main()
def shutdown(self):
for name, srv in self._components.getUtilitiesFor(IService):
srv.shutdown()
self.handle(ServiceShutdownEvent(name, srv))
self._components.unregisterUtility(srv, IService, name)
# Re-initialize components registry
self.init_components()
# Wrap zope.component's Components methods
def register_adapter(self, factory, adapts=None, provides=None, name=''):
"""
Register an adapter (factory) that adapts objects to a specific
interface. A name can be used to distinguish between different adapters
that adapt to the same interfaces.
"""
self._components.registerAdapter(factory, adapts, provides,
name, event=False)
def unregister_adapter(self, factory=None,
required=None, provided=None, name=u''):
"""
Unregister a previously registered adapter.
"""
self._components.unregisterAdapter(factory,
required, provided, name)
def register_subscription_adapter(self, factory, adapts=None, provides=None):
"""
Register a subscription adapter. See registerAdapter().
"""
self._components.registerSubscriptionAdapter(factory, adapts,
provides, event=False)
def unregister_subscription_adapter(self, factory=None,
required=None, provided=None, name=u''):
"""
Unregister a previously registered subscription adapter.
"""
self._components.unregisterSubscriptionAdapter(factory,
required, provided, name)
def register_handler(self, factory, adapts=None):
"""
Register a handler. Handlers are triggered (executed) when specific
events are emitted through the handle() method.
"""
self._components.registerHandler(factory, adapts, event=False)
def unregister_handler(self, factory=None, required=None):
"""
Unregister a previously registered handler.
"""
self._components.unregisterHandler(factory, required)
def _filter(self, objects):
filtered = list(objects)
for o in objects:
for adapter in self._components.subscribers(objects, IEventFilter):
if adapter.filter():
# event is blocked
filtered.remove(o)
break
return filtered
def handle(self, *events):
"""
Send event notifications to registered handlers.
"""
objects = self._filter(events)
if objects:
self._components.handle(*events)
# Make sure there is only one!
Application = _Application()
# vim:sw=4:et:ai
|