Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

EventMagicMeta will now also discover events defined in base classes of cls. #37

Merged
merged 2 commits into from

2 participants

@specialunderwear

We've got a lot of events and we want to factor them into separate classes.

Currently that does not work well with EventMagicMeta because it only looks in attrs, not cls for events. The patch makes EventMagicMeta look for all methods defined on cls with an '_event_name' property, which includes the events defined in additional bases of cls.

@mrjoes mrjoes merged commit c5584e9 into mrjoes:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 5 additions and 11 deletions.
  1. +5 −11 tornadio2/conn.py
View
16 tornadio2/conn.py
@@ -22,6 +22,7 @@
"""
import time
import logging
+from inspect import ismethod, getmembers
from tornadio2 import proto
@@ -57,17 +58,10 @@ def handler(f):
class EventMagicMeta(type):
"""Event handler metaclass"""
def __init__(cls, name, bases, attrs):
- # Manage events
- events = {}
-
- for a in attrs:
- attr = getattr(cls, a)
- name = getattr(attr, '_event_name', None)
-
- if name:
- events[name] = attr
-
- setattr(cls, '_events', events)
+ # find events, also in bases
+ is_event = lambda x: ismethod(x) and hasattr(x, '_event_name')
+ events = [(e._event_name, e) for _, e in getmembers(cls, is_event)]
+ setattr(cls, '_events', dict(events))
# Call base
super(EventMagicMeta, cls).__init__(name, bases, attrs)
Something went wrong with that request. Please try again.