Created
May 29, 2011 20:03
-
-
Save EntityReborn/998094 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Manager(object): | |
def __init__(self): | |
self.funcs = {} | |
def regTrigger(self, func, trig): | |
trig = trig.lower() | |
if not trig in self.funcs: | |
self.funcs[trig] = [] | |
self.funcs[trig].append(func) | |
def call(self, trig, *args): | |
trig = trig.lower() | |
if not trig in self.funcs: | |
return False | |
for func in self.funcs[trig]: | |
func(*args) | |
class PlugBase(object): | |
funcs = {} | |
@classmethod | |
def trigger(cls, *triggers): | |
def call(func): | |
for trig in triggers: | |
if not trig in cls.funcs: | |
cls.funcs[trig] = [] | |
cls.funcs[trig].append(func.__name__) | |
return func | |
return call | |
def init(self, manager): | |
for trig, funcs in self.funcs.iteritems(): | |
for func in funcs: | |
manager.regTrigger(getattr(self, func), trig) | |
self.funcs[trig].remove(func) | |
class Plug(PlugBase): | |
@PlugBase.trigger("msg") | |
def on_msg(self, msg): | |
print msg | |
m = Manager() | |
p = Plug() | |
p.init(m) | |
m.call("msg", "ohai") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment