Skip to content

Commit

Permalink
Renamed ListenerHandler to EventListener
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed Sep 25, 2015
1 parent 9858956 commit a7cb917
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
12 changes: 6 additions & 6 deletions asphalt/core/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from .util import qualified_name, asynchronous

__all__ = 'Event', 'ListenerHandle', 'EventSource'
__all__ = 'Event', 'EventListener', 'EventSource'


class Event:
Expand All @@ -20,7 +20,7 @@ def __init__(self, source: 'EventSource', topic: str):
self.topic = topic


class ListenerHandle:
class EventListener:
"""A handle that can be used to remove an event listener from its :class:`EventSource`."""

__slots__ = 'topic', 'callback', 'args', 'kwargs'
Expand All @@ -33,7 +33,7 @@ def __init__(self, topic: str, callback: Callable[[Event], Any],
self.kwargs = kwargs

def __repr__(self):
return ('ListenerHandle(topic={0.topic!r}, callback={1}, args={0.args!r}, '
return ('{0.__class__.__name__}(topic={0.topic!r}, callback={1}, args={0.args!r}, '
'kwargs={0.kwargs!r})'.
format(self, qualified_name(self.callback)))

Expand All @@ -59,7 +59,7 @@ def _register_topics(self, topics: Dict[str, Any]):

@asynchronous
def add_listener(self, topic: str, callback: Callable[[Any], Any],
args: Sequence[Any]=(), kwargs: Dict[str, Any]=None) -> ListenerHandle:
args: Sequence[Any]=(), kwargs: Dict[str, Any]=None) -> EventListener:
"""
Starts listening to events specified by ``topic``. The callback (which can be
a coroutine function) will be called with a single argument (an :class:`Event` instance).
Expand All @@ -76,13 +76,13 @@ def add_listener(self, topic: str, callback: Callable[[Any], Any],
if topic not in self._topics:
raise LookupError('no such topic registered: {}'.format(topic))

handle = ListenerHandle(topic, callback, args, kwargs or {})
handle = EventListener(topic, callback, args, kwargs or {})
handles = self._topics[topic]['listeners']
handles.append(handle)
return handle

@asynchronous
def remove_listener(self, handle: ListenerHandle):
def remove_listener(self, handle: EventListener):
"""
Removes an event listener previously added via :meth:`add_listener`.
Expand Down
15 changes: 7 additions & 8 deletions tests/test_event.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from asyncio import coroutine
import asyncio
import operator

import pytest

from asphalt.core.event import EventSource, Event, ListenerHandle
from asphalt.core.event import EventSource, Event, EventListener


class DummyEvent(Event):
Expand All @@ -14,15 +13,15 @@ def __init__(self, source, topic, *args, **kwargs):
self.kwargs = kwargs


class TestListenerHandle:
class TestEventListener:
@pytest.fixture
def handle(self):
return ListenerHandle('foo', lambda: None, (), {})
return EventListener('foo', lambda: None, (), {})

def test_repr(self, handle):
assert repr(handle) == (
"ListenerHandle(topic='foo', "
"callback=test_event.TestListenerHandle.handle.<locals>.<lambda>, args=(), kwargs={})")
"EventListener(topic='foo', "
"callback=test_event.TestEventListener.handle.<locals>.<lambda>, args=(), kwargs={})")


class TestEventSource:
Expand All @@ -34,7 +33,7 @@ def source(self):

def test_add_listener(self, source):
handle = source.add_listener('event_a', lambda: None)
assert isinstance(handle, ListenerHandle)
assert isinstance(handle, EventListener)
assert handle.topic == 'event_a'

def test_add_listener_nonexistent_event(self, source):
Expand Down Expand Up @@ -88,7 +87,7 @@ def callback(event: Event, *args, **kwargs):
@pytest.mark.parametrize('topic', ['event_a', 'foo'],
ids=['existing_event', 'nonexistent_event'])
def test_remove_noexistent_listener(self, source, topic):
handle = ListenerHandle(topic, lambda: None, (), {})
handle = EventListener(topic, lambda: None, (), {})
exc = pytest.raises(LookupError, source.remove_listener, handle)
assert str(exc.value) == 'listener not found'

Expand Down

0 comments on commit a7cb917

Please sign in to comment.