| Line | |
|---|
| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
import warnings |
|---|
| 5 |
warnings.warn( |
|---|
| 6 |
"Create your own event dispatching mechanism, " |
|---|
| 7 |
"twisted.python.dispatch will soon be no more.", |
|---|
| 8 |
DeprecationWarning, 2) |
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
class EventDispatcher: |
|---|
| 12 |
""" |
|---|
| 13 |
A global event dispatcher for events. |
|---|
| 14 |
I'm used for any events that need to span disparate objects in the client. |
|---|
| 15 |
|
|---|
| 16 |
I should only be used when one object needs to signal an object that it's |
|---|
| 17 |
not got a direct reference to (unless you really want to pass it through |
|---|
| 18 |
here, in which case I won't mind). |
|---|
| 19 |
|
|---|
| 20 |
I'm mainly useful for complex GUIs. |
|---|
| 21 |
""" |
|---|
| 22 |
|
|---|
| 23 |
def __init__(self, prefix="event_"): |
|---|
| 24 |
self.prefix = prefix |
|---|
| 25 |
self.callbacks = {} |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
def registerHandler(self, name, meth): |
|---|
| 29 |
self.callbacks.setdefault(name, []).append(meth) |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
def autoRegister(self, obj): |
|---|
| 33 |
from twisted.python import reflect |
|---|
| 34 |
d = {} |
|---|
| 35 |
reflect.accumulateMethods(obj, d, self.prefix) |
|---|
| 36 |
for k,v in d.items(): |
|---|
| 37 |
self.registerHandler(k, v) |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
def publishEvent(self, name, *args, **kwargs): |
|---|
| 41 |
for cb in self.callbacks[name]: |
|---|
| 42 |
cb(*args, **kwargs) |
|---|