Remove zope event handlers from component registry

This commit is contained in:
Arjan Molenaar 2019-05-06 21:20:49 +02:00
parent b8442109f9
commit 5aa63e6f5c
3 changed files with 34 additions and 37 deletions

View File

@ -219,17 +219,3 @@ class inject:
if not obj:
return self
return Application.get_service(self._name)
def event_handler(event_type):
"""
Mark a function/method as an event handler for a particular type of event.
"""
from zope import component
def wrapper(func):
func.__event_handler__ = event_type
component.adapter(event_type)(func)
return func
return wrapper

View File

@ -4,7 +4,8 @@ The Core module provides an entry point for Gaphor's core constructs.
An average module should only need to import this module.
"""
from gaphor.application import inject, event_handler, Application
from gaphor.application import inject, Application
from gaphor.services.componentregistry import event_handler
from gaphor.transaction import Transaction, transactional
from gaphor.action import action, toggle_action, radio_action, build_action_group
from gaphor.i18n import _

View File

@ -1,17 +1,22 @@
"""
TODO: Move component information (event handling, and other stuff done by
zope.component) to this service.
Maybe we should split the ComponentRegistry in a Dispatcher (register_handler,
unregister_handler, handle), a AdapterRegistry and a Subscription registry.
A registry for components (e.g. services) and event handling.
"""
from zope import component
from zope.interface import registry
from gaphor.abc import Service
from gaphor.application import ComponentLookupError
from gaphor.misc.generic.event import Manager
def event_handler(*event_types):
"""
Mark a function/method as an event handler for a particular type of event.
"""
def wrapper(func):
func.__event_types__ = event_types
return func
return wrapper
class ComponentRegistry(Service):
@ -19,13 +24,9 @@ class ComponentRegistry(Service):
The ComponentRegistry provides a home for application wide components.
"""
def __init__(self):
self._comp = set()
def init(self, app):
self._components = registry.Components(
name="component_registry", bases=(component.getGlobalSiteManager(),)
)
self._comp = set()
self._events = Manager()
def shutdown(self):
pass
@ -57,23 +58,32 @@ class ComponentRegistry(Service):
def all(self, base):
return ((c, n) for c, n in self._comp if isinstance(c, base))
# Wrap zope.component's Components methods
def register_handler(self, factory, adapts=None):
def register_handler(self, handler):
"""
Register a handler. Handlers are triggered (executed) when specific
events are emitted through the handle() method.
"""
self._components.registerHandler(factory, adapts, event=False)
event_types = getattr(handler, "__event_types__", None)
if not event_types:
raise Exception(f"No event types provided for function {handler}")
def unregister_handler(self, factory=None, required=None):
for et in event_types:
self._events.subscribe(handler, et)
def unregister_handler(self, handler=None, event_types=None):
"""
Unregister a previously registered handler.
"""
self._components.unregisterHandler(factory, required)
event_types = getattr(handler, "__event_types__", None)
if not event_types:
raise Exception(f"No event types provided for function {handler}")
for et in event_types:
self._events.unsubscribe(handler, et)
def handle(self, *events):
"""
Send event notifications to registered handlers.
"""
list(map(self._components.handle, events))
for e in events:
self._events.fire(e)