Package pyforms :: Package terminal :: Module PyFormsStateMachine
[hide private]
[frames] | no frames]

Source Code for Module pyforms.terminal.PyFormsStateMachine

  1  import sys, glob, os 
  2  import pyforms.Utils.tools as tools, time 
  3  import argparse 
  4  from pyforms.terminal.BaseWidget import BaseWidget 
  5   
  6  from pyStateMachine.States.State import State,EndState 
  7  from pyStateMachine.StateMachineControllers.StatesController import StatesController 
8 9 10 -def gotoAppState(true=None, false=None, trueParms={}, falseParms={}):
11 def go2decorator(func): 12 def func_wrapper(self, inVar): return func(self, inVar) 13 14 func_wrapper.trueParms = trueParms 15 func_wrapper.falseParms = falseParms 16 func_wrapper.go2StateTrue = true 17 func_wrapper.go2StateFalse = false 18 func_wrapper.label = func.__name__ if func.__doc__==None else '\n'.join([x for x in func.__doc__.replace('\t','').split('\n') if len(x)>0]) 19 return func_wrapper
20 return go2decorator 21
22 23 -class PyFormsState(State):
24
25 - def init(self): pass
26
27 - def enter(self, inVar, currentState={}): return inVar
28
29 - def leave(self, inVar, currentState={}): return inVar
30
31 - def execute(self):
32 if self.app!=None and hasattr(self.app, 'execute'): self.app.execute()
33 34 35 @property
36 - def app(self): return self._app if hasattr(self, '_app') else None
37 38 @app.setter
39 - def app(self, value): self._app = value
40
41 - def parseTerminalParameters(self):
42 if hasattr(self, 'app') and self.app: self.app.parseTerminalParameters();
43
44 - def initForm(self):
45 if hasattr(self, 'app') and self.app: self.app.initForm(parse=False);
46 47 @property
48 - def form(self): return self.app.form if self.app else None
49
50 51 -class PyFormsStateMachine(StatesController, BaseWidget):
52 53 _parser = argparse.ArgumentParser() 54 55
56 - def __init__(self, title):
57 BaseWidget.__init__(self, title) 58 StatesController.__init__(self, self.STATES) 59 60 for stateName, state in self.states.items(): 61 if hasattr(state, 'init'): state.init()
62 63
64 - def initForm(self):
65 66 # Load the applications 67 for fromStateName, state in reversed( self.states.items() ): 68 if hasattr(state, 'app') and state.app: 69 state.app._parser = self._parser 70 state.app._controlsPrefix = fromStateName 71 #Override the original application formset if the state has a new one 72 state.initForm() 73 74 75 self._parser.add_argument( 76 "--exec", default='', 77 help='Function from the application that should be executed. Use | to separate a list of functions.') 78 79 self._parser.add_argument( 80 "--exportDiagramTo", default='', 81 help='File to where the diagram should export to.') 82 83 self._args = self._parser.parse_args() 84 85 if self._args.exportDiagramTo!='': self.exportGraph(self._args.exportDiagramTo) 86 87 88 for fromStateName, state in reversed( self.states.items() ): 89 if hasattr(state, 'app') and state.app: 90 state.app._args = self._args 91 state.parseTerminalParameters() 92 93 for function in self._args.__dict__.get("exec", []).split('|'): 94 if len(function)>0: getattr(self, function)()
95 96 97
98 - def iterateStates(self):
99 """ 100 Iterate states - each call go to another level 101 """ 102 if len(self._waitingStates)>0: 103 statesOutputs = [] 104 105 #First run all the pending states 106 for fromStateName, toStateName, inputParam in self._waitingStates: 107 state = self._states[toStateName] 108 copyOfCurrentState = dict(self._currentState) 109 110 if isinstance(state, EndState): executionDetails = (toStateName, state, inputParam ) 111 else: 112 p = state.enter(inputParam, copyOfCurrentState) 113 state.execute() 114 outParam = state.leave(p, copyOfCurrentState) 115 executionDetails = (toStateName, state, outParam ) 116 117 statesOutputs.append( executionDetails ) 118 119 #Check the events of each exectuted state: 120 for stateName, state, output in statesOutputs: 121 #Remove the exectued state from the waiting queue 122 self._waitingStates.pop(0) 123 #Check each event 124 for e in state.events: 125 #Select the next state to go 126 go2State = e.go2StateTrue if e(state, output) else e.go2StateFalse 127 #In case the state is None, it stops the state execution 128 if go2State!=None: self._waitingStates.append( [stateName, go2State, output] ) 129 else: self._waitingStates.append( [stateName, 'EndState', output] ) 130 131 "Save the currentState of the iteration" 132 self._currentState = self.__returnCurrentStatesValues() 133 else: 134 print("State machine ended")
135 136
137 - def execute(self):
138 # Initiate the parameters set by the user 139 self._currentState = self.__returnCurrentStatesValues() 140 141 # Iterate the states execution 142 while not self.ended: self.iterateStates()
143 144
145 - def __returnCurrentStatesValues(self):
146 """ 147 Iterate all the states and save the values of their controls 148 """ 149 currentState = {} 150 for stateName, state in reversed( self.states.items() ): 151 appState = {} 152 #The state has an application associated to it 153 if hasattr(state, 'app') and state.app: 154 for controlName, control in state.app.formControls.items(): 155 appState[controlName] = control.value 156 currentState[stateName] = appState 157 return currentState
158
159 160 161 -def startApp(states):
162 app = QtGui.QApplication(sys.argv) 163 container = Container(states) 164 app.exec_()
165