def __init__(self, commandManager, eventManager): self._cmdExprs = [] self._cmdMgr = commandManager self._genMgr = concurrency.GeneratorManager(eventManager) self._qmStartEvents = EventResponderList(eventManager, "startQuasimode", self._onQuasimodeStart)
class GeneratorManager( object ): """ Responsible for managing generators in a way similar to tasklets in Stackless Python by iterating the state of all registered generators on every timer tick. """ def __init__( self, eventManager ): self.__generators = EventResponderList( eventManager, "timer", self.__onTimer ) @safetyNetted def __callGenerator( self, generator, keepAlives ): try: generator.next() keepAlives.append( generator ) except StopIteration: pass def __onTimer( self, msPassed ): keepAlives = [] for generator in self.__generators: self.__callGenerator( generator, keepAlives ) self.__generators[:] = keepAlives def reset( self ): self.__generators[:] = [] def add( self, generator ): self.__generators.append( generator )
class ScriptCommandTracker: def __init__( self, commandManager, eventManager ): self._cmdExprs = [] self._cmdMgr = commandManager self._genMgr = concurrency.GeneratorManager( eventManager ) self._qmStartEvents = EventResponderList( eventManager, "startQuasimode", self._onQuasimodeStart ) @safetyNetted def _callHandler( self, handler ): result = handler() if isinstance( result, types.GeneratorType ): self._genMgr.add( result ) def _onQuasimodeStart( self ): for handler in self._qmStartEvents: self._callHandler( handler ) def _clearCommands( self ): for cmdExpr in self._cmdExprs: self._cmdMgr.unregisterCommand( cmdExpr ) self._cmdExprs = [] self._qmStartEvents[:] = [] self._genMgr.reset() def _registerCommand( self, cmdObj, cmdExpr ): try: self._cmdMgr.registerCommand( cmdExpr, cmdObj ) self._cmdExprs.append( cmdExpr ) except CommandAlreadyRegisteredError: logging.warn( "Command already registered: %s" % cmdExpr ) def registerNewCommands( self, commandInfoList ): self._clearCommands() for info in commandInfoList: if hasattr( info["func"], "on_quasimode_start" ): self._qmStartEvents.append( info["func"].on_quasimode_start ) cmd = adapters.makeCommandFromInfo( info, ensoapi.EnsoApi(), self._genMgr ) self._registerCommand( cmd, info["cmdExpr"] )
class ScriptCommandTracker: def __init__( self, commandManager, eventManager ): self._cmdExprs = [] self._cmdMgr = commandManager self._genMgr = concurrency.GeneratorManager( eventManager ) self._qmStartEvents = EventResponderList( eventManager, "startQuasimode", self._onQuasimodeStart ) @safetyNetted def _callHandler( self, handler ): result = handler() if isinstance( result, types.GeneratorType ): self._genMgr.add( result ) def _onQuasimodeStart( self ): for handler in self._qmStartEvents: self._callHandler( handler ) def clearCommands( self ): for cmdExpr in self._cmdExprs: self._cmdMgr.unregisterCommand( cmdExpr ) self._cmdExprs = [] self._qmStartEvents[:] = [] self._genMgr.reset() def _registerCommand( self, cmdObj, cmdExpr ): try: self._cmdMgr.registerCommand( cmdExpr, cmdObj ) self._cmdExprs.append( cmdExpr ) except CommandAlreadyRegisteredError: logging.warn( "Command already registered: %s" % cmdExpr ) def registerNewCommands( self, commandInfoList ): for info in commandInfoList: if hasattr( info["func"], "on_quasimode_start" ): self._qmStartEvents.append( info["func"].on_quasimode_start ) cmd = adapters.makeCommandFromInfo( info, ensoapi.EnsoApi(), self._genMgr ) self._registerCommand( cmd, info["cmdExpr"] )
def __init__( self, commandManager, eventManager ): self._cmdExprs = [] self._cmdMgr = commandManager self._genMgr = concurrency.GeneratorManager( eventManager ) self._qmStartEvents = EventResponderList( eventManager, "startQuasimode", self._onQuasimodeStart )
def __init__(self, eventManager): self.__generators = EventResponderList(eventManager, "timer", self.__onTimer)
def __init__( self, eventManager ): self.__generators = EventResponderList( eventManager, "timer", self.__onTimer )