rendered paste body# encoding: utf-8"""FSM.pyCreated by Cody Brocious on 2006-09-18.Copyright (c) 2006 Falling Leaf Systems, LLC. All rights reserved."""import tracebackclass State: pass class StateMachine: def __init__(self): self.last_state = None self.current_state = None self.states = [] def __copy__(self): copy = FSM.__new__() copy.last_state = self.last_state copy.current_state = self.current_state copy.states = self.states return copy def addState(self, state): self.states.append(state) def run(self, initial_state=None): if initial_state != None: self.current_state = initial_state while self.current_state != None: try: new_state = self.current_state(self) self.last_state = self.current_state self.current_state = new_state except Exception, e: print 'Unhandled exception in state `%s\':' % self.current_state traceback.print_exc() return False