def flowcontrolWHILE(func): parent = _getCommandGroup() source = _getSource(parent) try: parentLoop = source._currentLoop except AttributeError: parentLoop = None cg = CommandGroup(func.__name__) cg._source = source # Set the current loop for any BREAK statements source._currentLoop = cg func(cg) source._currentLoop = parentLoop def cancelLoop(self): self.getGroup().forceCancel = True end = Command("END WHILE") end.initialize = cancelLoop.__get__(end) cond = ConditionalCommand("flowcontrolWHILE", cg, end) cond.condition = condition cond.forceCancel = False cond.isFinished = _restartWhile.__get__(cond) cond._parentLoop = parentLoop parent.addSequential(cond) cg.conditionalCommand = cond
def BREAK(steps=1): """ Calling this function will end the loop that contains it. Pass an integer to break out of that number of nested loops. """ if steps < 1: raise ValueError('Steps to BREAK cannot be < 1') parent = _getCommandGroup() source = _getSource(parent) try: loop = source._currentLoop except AttributeError: raise ValueError('Cannot BREAK outside of a loop') if loop is None: raise ValueError('Cannot BREAK outside of a loop') # We can't use CancelCommand here, because the conditionalCommand attribute # isn't yet bound to the CommandGroup, so we find it at initialization time # instead def cancelLoop(): nonlocal loop, steps step = 1 while steps > step: loop = loop.conditionalCommand._parentLoop if loop is None: raise ValueError( 'BREAK %i not possible with loop depth %i' % (steps, step) ) step += 1 loop.conditionalCommand.forceCancel = True breakLoop = Command('flowcontrolBREAK') breakLoop.initialize = cancelLoop parent.addSequential(breakLoop)
def BREAK(steps=1): """ Calling this function will end the loop that contains it. Pass an integer to break out of that number of nested loops. """ if steps < 1: raise ValueError("Steps to BREAK cannot be < 1") parent = _getCommandGroup() source = _getSource(parent) try: loop = source._currentLoop except AttributeError: raise ValueError("Cannot BREAK outside of a loop") if loop is None: raise ValueError("Cannot BREAK outside of a loop") # We can't use CancelCommand here, because the conditionalCommand attribute # isn't yet bound to the CommandGroup, so we find it at initialization time # instead def cancelLoop(): nonlocal loop, steps step = 1 while steps > step: loop = loop.conditionalCommand._parentLoop if loop is None: raise ValueError("BREAK %i not possible with loop depth %i" % (steps, step)) step += 1 loop.conditionalCommand.forceCancel = True breakLoop = Command("flowcontrolBREAK") breakLoop.initialize = cancelLoop parent.addSequential(breakLoop)