Package: mediator
A library implements the Mediator pattern to make your code extensible.Inspired bysymfony/event-dispatchercomponent.InstallationUsing pip:# pip install mediatorManually:$gitclonehttps://github.com/Kilte/mediator$cdmediator# python setup.py installUsagefrommediatorimportMediatorm=Mediator()Defining eventsfrommediatorimportEventclassEventOne(Event):passclassEventTwo(Event):event_name='event_two'Adding a listenerdefevent_listener(event):print('Got event:%r'%event)m.add_listener(EventOne,event_listener)# or m.add_listener('EventOne', event_listener)m.add_listener('event_two',event_listener)# or m.add_listener(EventTwo, event_listener)Triggering eventsevent1=EventOne()event2=EventTwo()m.dispatch(event1)# Got event: <EventOne object at 0x7f954bc2b250>m.dispatch(event2)# Got event: <EventTwo object at 0x7f954bbbd510>Removing a listenerdefanother_listener(event):print('Got another event:%r'%event)m.add_listener(EventOne,another_listener)m.remove_listener('EventOne',event_listener)m.dispatch(event1)# Got another event: <EventOne object at 0x7f954bc2b250>Removing all listenersm.remove_listener('EventOne')m.dispatch(event1)# Nothing was happenedUsing prioritiesm.add_listener('EventOne',another_listener,-255)m.add_listener('EventOne',event_listener,255)m.dispatch(event1)# Got another event: <EventOne object at 0x7f954bc2b250># Got event: <EventOne object at 0x7f954bc2b250>Using SubscribersfrommediatorimportSubscriberInterfaceclassSubscriber(SubscriberInterface):deffirst(self,event):event.first=Truedefmiddle(self,event):event.middle=Truedeflast(self,event):event.last=Truedefevent2_handler(self,event):event.success=Truedefevent3_handler(self,event):event.success=Truedefget_subscribed_events(self):return{'event1':[['middle'],['first',-100],['last',100]],'event2':'event2_handler','event3':['event3_handler']}classEvent1(Event):event_name='event1'def__init__(self):self.first=Falseself.middle=Falseself.last=FalseclassEvent2(Event):event_name='event2'def__init__(self):self.success=TrueclassEvent3(Event2):event_name='event3'subscriber=Subscriber()event1=Event1()event2=Event2()event3=Event3()m.add_subscriber(subscriber)m.dispatch(event1)print('%s;%s;%s'%(event1.first,event1.middle,event1.last))# True;True;Truem.dispatch(event2)print(event2.success)# Truem.dispatch(event3)print(event3.success)# TrueAdding listeners using decoratorimportsysimportvenusianfrommediatorimportVENUSIAN_CATEGORY@SomeEvent.listen(priority=255,instance='mediator',category=VENUSIAN_CATEGORY)# All args are optionaldefsome_event_listener(event):event.attr='value'scanner=venusian.Scanner(mediator=mediator)scanner.scan(package=sys.modules[__name__],categories=[VENUSIAN_CATEGORY])m.dispatch(SomeEvent())See source code and tests for more information.Changelog0.4.0 (17.03.2017)Fixed multiple inheritance supportImproved venusian support0.3.0 (25.02.2016)Mediator.__init__andMediator.scannow takes keyword arguments only.RemovedMediator.set_scannermethod.Mediator.dispatch()now takes event instances only.Mediator.add_listenerandMediator.remove_listenertakes subclass ofEventorstr.Event.get_name()andEvent.set_name()were removed in favor ofEvent.get_event_name()andEvent.event_nameclass attribute.And now there is no requirement to callsuper().__init__()in your own events.0.2.1 (18.12.2015)AddedMediator.set_scannermethod in order to allow use custom scanner instance.0.2.0 (17.12.2015)Automatic event name detection based on a class name.Added%Event%.listendecorator.0.1.0 (19.11.2015)First release.ContributingFork and clone itCreate your feature branch (git checkout -b awesome-feature)Make your changesWrite/update tests, if it’s necessary (make buildout && make tests)Update README.md, if it’s necessaryPush your branch (git push origin awesome-feature)Send a pull requestLICENSEThe MIT License (MIT)