<s> """<STR_LIT>""" <EOL> import logging <EOL> import signal <EOL> from collections import defaultdict <EOL> from copy import deepcopy <EOL> from spockbot . plugins . base import pl_announce <EOL> from spockbot . plugins . tools . event import EVENT_UNREGISTER <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> @ pl_announce ( '<STR_LIT>' ) <EOL> class EventPlugin ( object ) : <EOL> def __init__ ( self , ploader , settings ) : <EOL> ploader . provides ( '<STR_LIT>' , self ) <EOL> self . has_run = False <EOL> self . kill_event = False <EOL> self . event_handlers = defaultdict ( list ) <EOL> signal . signal ( signal . SIGINT , self . kill ) <EOL> signal . signal ( signal . SIGTERM , self . kill ) <EOL> def event_loop ( self , once = False ) : <EOL> if once : <EOL> self . run_once ( ) <EOL> else : <EOL> self . run_continuous ( ) <EOL> def run_continuous ( self ) : <EOL> if not self . has_run and not self . kill_event : <EOL> self . has_run = True <EOL> self . emit ( '<STR_LIT>' ) <EOL> while not self . kill_event : <EOL> self . emit ( '<STR_LIT>' ) <EOL> logger . debug ( '<STR_LIT>' ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> def run_once ( self ) : <EOL> if not self . has_run and not self . kill_event : <EOL> self . has_run = True <EOL> self . emit ( '<STR_LIT>' ) <EOL> if not self . kill_event : <EOL> self . emit ( '<STR_LIT>' ) <EOL> else : <EOL> logger . debug ( '<STR_LIT>' ) <EOL> self . emit ( '<STR_LIT>' ) <EOL> def reg_event_handler ( self , event , handler ) : <EOL> self . event_handlers [ event ] . append ( handler ) <EOL> def unreg_event_handler ( self , event , handler ) : <EOL> self . event_handlers [ event ] . remove ( handler ) <EOL> def emit ( self , event , data = None ) : <EOL> try : <EOL> for handler in self . event_handlers [ event ] [ : ] : <EOL> d = data . clone ( ) if hasattr ( data , '<STR_LIT>' ) else deepcopy ( data ) <EOL> if handler ( event , d ) == EVENT_UNREGISTER : <EOL> self . event_handlers [ event ] . remove ( handler ) <EOL> except : <EOL> logger . debug ( '<STR_LIT>' , <EOL> event , data ) <EOL> raise <EOL> def kill ( self , * args ) : <EOL> self . kill_event = True </s>
