Ahenk/opt/ahenk/base/event/EventBase.py

39 lines
1.2 KiB
Python
Raw Normal View History

2016-03-04 18:01:47 +02:00
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
class EventBase():
"""
This is base event class for event management.
"""
2016-03-04 18:01:47 +02:00
listeners = []
2016-03-04 18:01:47 +02:00
def __init__(self):
self.listeners.append(self)
self.listener_events = []
2016-03-04 18:01:47 +02:00
def register_event(self, event_name, callback_func):
"""
Registers event listener.
Args:
event_name : name of event, user specify event name
callback_func : when an event fire with specified event name this method will call
"""
self.listener_events.append({'event_name': event_name, 'callback_func': callback_func})
2016-03-04 18:01:47 +02:00
class Event():
"""
This is event class. Takes two argument ;
Args:
event_name : name of event.
callback_args : arguments specified by user. This function will transmit args to callback function directly.
"""
2016-03-04 18:01:47 +02:00
def __init__(self, event_name, *callback_args):
for listener in EventBase.listeners:
for listener_cls in listener.listener_events:
if listener_cls['event_name'] == event_name:
listener_cls['callback_func'](*callback_args)