Fixed return state for error handling
This commit is contained in:
@@ -22,11 +22,28 @@ import logging
|
|||||||
|
|
||||||
class Context:
|
class Context:
|
||||||
|
|
||||||
def __init__(self, communicator):
|
def __init__(self, communicator, omit_welcome=False):
|
||||||
|
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self._comm = communicator
|
self._comm = communicator
|
||||||
self.state = WelcomeState()
|
self.is_running = False
|
||||||
|
if omit_welcome:
|
||||||
|
self.state = StartupState()
|
||||||
|
else:
|
||||||
|
self.state = WelcomeState()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_running(self):
|
||||||
|
|
||||||
|
return self._is_running
|
||||||
|
|
||||||
|
@is_running.setter
|
||||||
|
def is_running(self, running):
|
||||||
|
|
||||||
|
if not isinstance(running, bool):
|
||||||
|
raise TypeError('is_running must be a bool')
|
||||||
|
|
||||||
|
self._is_running = running
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
@@ -37,7 +54,7 @@ class Context:
|
|||||||
def state(self, new_state):
|
def state(self, new_state):
|
||||||
|
|
||||||
if not isinstance(new_state, State):
|
if not isinstance(new_state, State):
|
||||||
raise TypeError('new_state must implement State')
|
raise TypeError('state must implement State')
|
||||||
|
|
||||||
logging.debug('New state is "{}"'.format(new_state))
|
logging.debug('New state is "{}"'.format(new_state))
|
||||||
|
|
||||||
@@ -52,8 +69,10 @@ class Context:
|
|||||||
logging.debug('Handling event "{}"'.format(event))
|
logging.debug('Handling event "{}"'.format(event))
|
||||||
|
|
||||||
if isinstance(event, ErrorEvent):
|
if isinstance(event, ErrorEvent):
|
||||||
self.state = ErrorState(event.origin, event.message, self.state)
|
self.state = ErrorState(event.origin, event.message, self.state,
|
||||||
|
self.is_running)
|
||||||
elif isinstance(event, TeardownEvent):
|
elif isinstance(event, TeardownEvent):
|
||||||
|
self.is_running = False
|
||||||
self.state = TeardownState(event.target)
|
self.state = TeardownState(event.target)
|
||||||
if event.target == TeardownEvent.EXIT:
|
if event.target == TeardownEvent.EXIT:
|
||||||
self._comm.bcast(None)
|
self._comm.bcast(None)
|
||||||
@@ -198,11 +217,12 @@ class State:
|
|||||||
|
|
||||||
class ErrorState(State):
|
class ErrorState(State):
|
||||||
|
|
||||||
def __init__(self, origin, message, old_state):
|
def __init__(self, origin, message, old_state, is_running):
|
||||||
|
|
||||||
self.origin = origin
|
self.origin = origin
|
||||||
self.message = message
|
self.message = message
|
||||||
self.old_state = old_state
|
self.old_state = old_state
|
||||||
|
self.is_running = is_running
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
@@ -248,13 +268,29 @@ class ErrorState(State):
|
|||||||
|
|
||||||
self._old_state = old_state
|
self._old_state = old_state
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_running(self):
|
||||||
|
|
||||||
|
return self._is_running
|
||||||
|
|
||||||
|
@is_running.setter
|
||||||
|
def is_running(self, running):
|
||||||
|
|
||||||
|
if not isinstance(running, bool):
|
||||||
|
raise TypeError('is_running must be a bool')
|
||||||
|
|
||||||
|
self._is_running = running
|
||||||
|
|
||||||
def handleEvent(self, event, context):
|
def handleEvent(self, event, context):
|
||||||
|
|
||||||
if isinstance(event, GuiEvent) and event.name == 'retry':
|
if isinstance(event, GuiEvent) and event.name == 'retry':
|
||||||
context.state = self.old_state
|
context.state = self.old_state
|
||||||
context.state.update()
|
context.state.update()
|
||||||
elif isinstance(event, GuiEvent) and event.name == 'abort':
|
elif isinstance(event, GuiEvent) and event.name == 'abort':
|
||||||
context.state = TeardownState(TeardownEvent.WELCOME)
|
if self.is_running:
|
||||||
|
context.state = IdleState()
|
||||||
|
else:
|
||||||
|
context.state = TeardownState(TeardownEvent.WELCOME)
|
||||||
else:
|
else:
|
||||||
raise TypeError('Unknown Event type "{}"'.format(event))
|
raise TypeError('Unknown Event type "{}"'.format(event))
|
||||||
|
|
||||||
@@ -322,6 +358,7 @@ class StartupState(State):
|
|||||||
def handleEvent(self, event, context):
|
def handleEvent(self, event, context):
|
||||||
|
|
||||||
if isinstance(event, CameraEvent) and event.name == 'ready':
|
if isinstance(event, CameraEvent) and event.name == 'ready':
|
||||||
|
context.is_running = True
|
||||||
context.state = IdleState()
|
context.state = IdleState()
|
||||||
else:
|
else:
|
||||||
raise TypeError('Unknown Event type "{}"'.format(event))
|
raise TypeError('Unknown Event type "{}"'.format(event))
|
||||||
|
|||||||
Reference in New Issue
Block a user