"""
Provides the core event loop
"""

import signal
import copy
from spock.mcp import mcdata
from spock.utils import pl_announce

import logging
logger = logging.getLogger('spock')

class EventCore:
	def __init__(self):
		self.kill_event = False
		self.event_handlers = {}
		signal.signal(signal.SIGINT, self.kill)
		signal.signal(signal.SIGTERM, self.kill)

	def event_loop(self):
		while not self.kill_event:
			self.emit('event_tick')
		logger.info('Event Kill called, shutting down')
		self.emit('kill')

	def reg_event_handler(self, event, handler):
		if event not in self.event_handlers:
			self.event_handlers[event] = []
		self.event_handlers[event].append(handler)

	def emit(self, event, data = None):
		if event not in self.event_handlers:
			self.event_handlers[event] = []
		to_remove = []
		for handler in self.event_handlers[event]:
			if handler(
				event,
				data.clone() if hasattr(data, 'clone') else copy.deepcopy(data)
			):
				to_remove.append(handler)
		for handler in to_remove:
			self.event_handlers[event].remove(handler)

	def kill(self, *args):
		self.kill_event = True

@pl_announce('Event')
class EventPlugin:
	def __init__(self, ploader, settings):
		ploader.provides('Event', EventCore())

@@ -34,63 +34,70 @@
 %22%22%22%0A
-%0Aimport signal%0Aimport copy%0Afrom spock.mcp import mcdata
+from collections import defaultdict%0A%0Aimport signal%0Aimport copy
 %0Afro
@@ -274,10 +274,25 @@
 s = 
-%7B%7D
+defaultdict(list)
 %0A%09%09s
@@ -574,82 +574,8 @@
 r):%0A
-%09%09if event not in self.event_handlers:%0A%09%09%09self.event_handlers%5Bevent%5D = %5B%5D%0A
 %09%09se
@@ -657,82 +657,8 @@
 e):%0A
-%09%09if event not in self.event_handlers:%0A%09%09%09self.event_handlers%5Bevent%5D = %5B%5D%0A
 %09%09to

Use defaultdict