home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / util / fsm.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  2.6 KB  |  79 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from observe import Observable
  5.  
  6. class StateMachine(Observable):
  7.     
  8.     def __init__(self, name, states_list, start_state = None):
  9.         Observable.__init__(self)
  10.         self.nodes = { }
  11.         self.name = name
  12.         for state in states_list:
  13.             self.nodes[state] = { }
  14.         
  15.         self._current_state = None
  16.         self.state = start_state
  17.  
  18.     
  19.     def create_trans(self, from_state, to_state, input):
  20.         if not isinstance(input, basestring):
  21.             for iota in input:
  22.                 self.nodes[from_state][iota] = to_state
  23.             
  24.         else:
  25.             self.nodes[from_state][input] = to_state
  26.  
  27.     
  28.     def process(self, input):
  29.         if self.state not in self.nodes:
  30.             return None
  31.         
  32.         try:
  33.             curr_node = self.nodes[self.state]
  34.         except Exception:
  35.             self.state not in self.nodes
  36.             e = self.state not in self.nodes
  37.             print self.nodes.keys(), self.state
  38.             raise 
  39.         except:
  40.             self.state not in self.nodes
  41.  
  42.         if input in curr_node:
  43.             self.state = curr_node[input]
  44.         
  45.         return self.state
  46.  
  47.     
  48.     def set_state(self, state):
  49.         oldstate = self._current_state
  50.         self._current_state = state
  51.         self.notify('state', oldstate, state)
  52.  
  53.     state = property((lambda self: self._current_state), set_state)
  54.  
  55.  
  56. class StateManager(list):
  57.     
  58.     def __init__(self, important_states = None):
  59.         list.__init__(self)
  60.         if not important_states:
  61.             pass
  62.         self.important_states = []
  63.  
  64.     
  65.     def add_machine(self, machine):
  66.         if machine not in self:
  67.             self.append(machine)
  68.             machine.add_observer(self.machine_changed, 'state')
  69.         
  70.  
  71.     
  72.     def machine_changed(self, src, attr, _old, new):
  73.         important_machines = _[1]
  74.         other_machines = _[2]
  75.         [ machine.process(src.name + '_' + new) for machine in important_machines ]
  76.         [ machine.process(src.name + '_' + new) for machine in other_machines ]
  77.  
  78.  
  79.