summaryrefslogtreecommitdiff
path: root/sleekxmpp/thirdparty/statemachine.py
diff options
context:
space:
mode:
Diffstat (limited to 'sleekxmpp/thirdparty/statemachine.py')
-rw-r--r--sleekxmpp/thirdparty/statemachine.py30
1 files changed, 21 insertions, 9 deletions
diff --git a/sleekxmpp/thirdparty/statemachine.py b/sleekxmpp/thirdparty/statemachine.py
index 33d9b828..6c504dce 100644
--- a/sleekxmpp/thirdparty/statemachine.py
+++ b/sleekxmpp/thirdparty/statemachine.py
@@ -15,7 +15,8 @@ log = logging.getLogger(__name__)
class StateMachine(object):
- def __init__(self, states=[]):
+ def __init__(self, states=None):
+ if not states: states = []
self.lock = threading.Condition()
self.__states = []
self.addStates(states)
@@ -29,11 +30,11 @@ class StateMachine(object):
if state in self.__states:
raise IndexError("The state '%s' is already in the StateMachine." % state)
self.__states.append(state)
- finally:
+ finally:
self.lock.release()
- def transition(self, from_state, to_state, wait=0.0, func=None, args=[], kwargs={}):
+ def transition(self, from_state, to_state, wait=0.0, func=None, args=None, kwargs=None):
'''
Transition from the given `from_state` to the given `to_state`.
This method will return `True` if the state machine is now in `to_state`. It
@@ -64,15 +65,23 @@ class StateMachine(object):
values for `args` and `kwargs` are provided, they are expanded and passed like so:
`func( *args, **kwargs )`.
'''
+ if not args:
+ args = []
+ if not kwargs:
+ kwargs = {}
return self.transition_any((from_state,), to_state, wait=wait,
func=func, args=args, kwargs=kwargs)
- def transition_any(self, from_states, to_state, wait=0.0, func=None, args=[], kwargs={}):
+ def transition_any(self, from_states, to_state, wait=0.0, func=None, args=None, kwargs=None):
'''
Transition from any of the given `from_states` to the given `to_state`.
'''
+ if not args:
+ args = []
+ if not kwargs:
+ kwargs = {}
if not isinstance(from_states, (tuple, list, set)):
raise ValueError("from_states should be a list, tuple, or set")
@@ -83,11 +92,14 @@ class StateMachine(object):
if not to_state in self.__states:
raise ValueError("StateMachine does not contain to_state %s." % to_state)
+ if self.__current_state == to_state:
+ return True
+
start = time.time()
while not self.lock.acquire(False):
time.sleep(.001)
if (start + wait - time.time()) <= 0.0:
- log.debug("Could not acquire lock")
+ log.debug("==== Could not acquire lock in %s sec: %s -> %s ", wait, self.__current_state, to_state)
return False
while not self.__current_state in from_states:
@@ -108,7 +120,7 @@ class StateMachine(object):
# some 'false' value returned from func,
# indicating that transition should not occur:
- if not return_val:
+ if not return_val:
return return_val
log.debug(' ==== TRANSITION %s -> %s', self.__current_state, to_state)
@@ -193,9 +205,9 @@ class StateMachine(object):
while not self.__current_state in states:
# detect timeout:
remainder = start + wait - time.time()
- if remainder > 0:
+ if remainder > 0:
self.lock.wait(remainder)
- else:
+ else:
self.lock.release()
return False
self.lock.release()
@@ -241,7 +253,7 @@ class _StateCtx:
while not self.state_machine[self.from_state] or not self.state_machine.lock.acquire(False):
# detect timeout:
remainder = start + self.wait - time.time()
- if remainder > 0:
+ if remainder > 0:
self.state_machine.lock.wait(remainder)
else:
log.debug('StateMachine timeout while waiting for state: %s', self.from_state)