Skip to content

Commit

Permalink
add wildcards
Browse files Browse the repository at this point in the history
  • Loading branch information
spulec committed Oct 19, 2012
1 parent 87ad6a5 commit 5fd83ce
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
4 changes: 4 additions & 0 deletions eventlib/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def _register_handler(event, fun, external=False):
if external:
registry = core.EXTERNAL_HANDLER_REGISTRY

if not isinstance(event, basestring):
# If not basestring, it is a BaseEvent subclass
event = core.parse_event_to_name(event)

if event in registry:
registry[event].append(fun)
else:
Expand Down
29 changes: 22 additions & 7 deletions eventlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
"""Implementation of the basic operations for the eventlib"""


import logging
from collections import OrderedDict
from datetime import datetime
import fnmatch
from importlib import import_module
import logging

from .conf import getsetting
from .util import get_ip
Expand Down Expand Up @@ -52,6 +53,12 @@ def parse_event_name(name):
).format(name))


def parse_event_to_name(event):
app_name = event.__module__.split('.')[0]
class_name = event.__name__
return u"{}.{}".format(app_name, class_name)


def find_event(name):
"""Actually import the event represented by name
Expand Down Expand Up @@ -88,21 +95,29 @@ def cleanup_handlers(event=None):
EXTERNAL_HANDLER_REGISTRY.clear()


def find_handlers(event_name):
"""Small halper to find all handlers associated to a given event
def find_handlers(event_name, registry=HANDLER_REGISTRY):
"""Small helper to find all handlers associated to a given event
If the event can't be found, an empty list will be returned, since
this is an internal function and all validation against the event
name and its existence was already performed.
"""
handlers = HANDLER_REGISTRY.get(find_event(event_name), [])
handlers.extend(HANDLER_REGISTRY.get(event_name, []))
handlers = []

# event_name can be a BaseEvent or the string representation
if isinstance(event_name, basestring):
matched_events = [event for event in registry.keys()
if fnmatch.fnmatchcase(event_name, event)]
for matched_event in matched_events:
handlers.extend(registry.get(matched_event))
else:
handlers = registry.get(find_event(event_name), [])

return handlers


def find_external_handlers(event_name):
handlers = EXTERNAL_HANDLER_REGISTRY.get(event_name, [])
return handlers
return find_handlers(event_name, registry=EXTERNAL_HANDLER_REGISTRY)


def process(event_name, data):
Expand Down
34 changes: 27 additions & 7 deletions tests/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,21 @@ def handle_stuff(self):
pass

core.HANDLER_REGISTRY.should.have.length_of(1)
core.HANDLER_REGISTRY[MyEvent].should.be.equals([MyEvent.handle_stuff])
core.HANDLER_REGISTRY['tests.MyEvent'].should.be.equals([MyEvent.handle_stuff])


def test_external_handler_with_methods():
def test_wildcard_handler():
core.cleanup_handlers()

@eventlib.handler('stuff.*')
def do_nothing(data):
pass

core.HANDLER_REGISTRY.should.have.length_of(1)
core.HANDLER_REGISTRY['stuff.*'].should.be.equals([do_nothing])


def test_external_handler():
core.cleanup_handlers()

@eventlib.external_handler('stuff.Klass')
Expand Down Expand Up @@ -111,16 +122,25 @@ def other_stuff(data):
return 1
core.find_handlers('app.Event').should.be.equals([stuff, other_stuff])

@eventlib.handler('app.Event2')
@eventlib.handler('other_app.Event2')
def more_stuff(data):
return 2
core.find_handlers('app.Event').should.be.equals([stuff, other_stuff])
core.find_handlers('app.Event2').should.be.equals([more_stuff])
core.find_handlers('other_app.Event2').should.be.equals([more_stuff])

@eventlib.handler('app.*')
def even_more_stuff(data):
return 2
core.find_handlers('app.Event').should.be.equals([stuff, other_stuff, even_more_stuff])

@eventlib.external_handler('app2.Event')
def still_more_stuff(data):
return 3
core.find_external_handlers('app2.Event').should.be.equals([still_more_stuff])

@eventlib.external_handler('app2.*')
def another_method(data):
return 4
core.find_external_handlers('app2.Event').should.be.equals([still_more_stuff, another_method])

core.find_handlers('dont.Exist').should.be.equal([])
core.find_external_handlers('dont.Exist').should.be.equal([])
Expand All @@ -137,10 +157,10 @@ def handle_stuff(self):

find_event.return_value = MyEvent

@eventlib.handler('stuff.MyEvent')
@eventlib.handler('tests.MyEvent')
def do_nothing(data):
return len(data)

core.find_handlers('stuff.MyEvent').should.be.equals([
core.find_handlers('tests.MyEvent').should.be.equals([
MyEvent.handle_stuff, do_nothing
])

0 comments on commit 5fd83ce

Please sign in to comment.