Replace homemade hacky defaultdict with defaultdict ¯\_(ツ)_/¯

This commit is contained in:
Kristóf Tóth 2018-02-26 15:00:17 +01:00
parent 5e9eb88bfc
commit 47a4e078de

View File

@ -1,4 +1,5 @@
from tornado.web import Application from tornado.web import Application
from collections import defaultdict
from tfw.networking.server.controller_responder import ControllerResponder from tfw.networking.server.controller_responder import ControllerResponder
from tfw.networking.server.zmq_websocket_handler import ZMQWebSocketProxy from tfw.networking.server.zmq_websocket_handler import ZMQWebSocketProxy
@ -39,26 +40,19 @@ class TFWServer:
class FSMManager: class FSMManager:
def __init__(self, fsm_type): def __init__(self, fsm_type):
self.fsm = fsm_type() self.fsm = fsm_type()
self.trigger_handlers = {} self.trigger_handlers = defaultdict(list)
def trigger(self, trigger): def trigger(self, trigger):
self._prepare_handlerlist(trigger)
if all((handler() for handler in self.trigger_handlers[trigger])): if all((handler() for handler in self.trigger_handlers[trigger])):
try: self.fsm.trigger(trigger, message='') try: self.fsm.trigger(trigger, message='')
except AttributeError: log.debug('FSM failed to execute nonexistent trigger: "{}"'.format(trigger)) except AttributeError: log.debug('FSM failed to execute nonexistent trigger: "{}"'.format(trigger))
def subscribe_handler(self, trigger, handler): def subscribe_handler(self, trigger, handler):
self._prepare_handlerlist(trigger)
self.trigger_handlers[trigger].append(handler) self.trigger_handlers[trigger].append(handler)
def unsubscribe_handler(self, trigger, handler): def unsubscribe_handler(self, trigger, handler):
self._prepare_handlerlist(trigger)
self.trigger_handlers[trigger].append(handler) self.trigger_handlers[trigger].append(handler)
def _prepare_handlerlist(self, trigger):
try: self.trigger_handlers[trigger]
except KeyError: self.trigger_handlers[trigger] = []
class FSMUpdater: class FSMUpdater:
def __init__(self, fsm): def __init__(self, fsm):