From 5aa63e6f5c99895549435580cb3a24567d0948b4 Mon Sep 17 00:00:00 2001 From: Arjan Molenaar Date: Mon, 6 May 2019 21:20:49 +0200 Subject: [PATCH] Remove zope event handlers from component registry --- gaphor/application.py | 14 -------- gaphor/core.py | 3 +- gaphor/services/componentregistry.py | 54 ++++++++++++++++------------ 3 files changed, 34 insertions(+), 37 deletions(-) diff --git a/gaphor/application.py b/gaphor/application.py index 047a589fc..5e231da61 100644 --- a/gaphor/application.py +++ b/gaphor/application.py @@ -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 diff --git a/gaphor/core.py b/gaphor/core.py index 6104b9a7b..b174a3157 100644 --- a/gaphor/core.py +++ b/gaphor/core.py @@ -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 _ diff --git a/gaphor/services/componentregistry.py b/gaphor/services/componentregistry.py index 3ad3718b0..83a93df31 100644 --- a/gaphor/services/componentregistry.py +++ b/gaphor/services/componentregistry.py @@ -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)