Adding fancier version checks to allow to the cache to be preserved longer
[theonering] / src / gvoice / state_machine.py
index 4dcd809..6d184d0 100644 (file)
@@ -2,14 +2,12 @@
 
 import logging
 
-import gobject
-
 import util.go_utils as gobject_utils
 import util.coroutines as coroutines
 import util.misc as misc_utils
 
 
-_moduleLogger = logging.getLogger("gvoice.state_machine")
+_moduleLogger = logging.getLogger(__name__)
 
 
 def to_milliseconds(**kwd):
@@ -109,6 +107,11 @@ class NTimesStateStrategy(object):
                except IndexError:
                        return self._postTimeout
 
+       def __str__(self):
+               return "NTimesStateStrategy(timeout=%r)" % (
+                       self.timeout,
+               )
+
        def __repr__(self):
                return "NTimesStateStrategy(timeouts=%r, postTimeout=%r)" % (
                        self._timeouts,
@@ -151,6 +154,11 @@ class GeometricStateStrategy(object):
                        timeout = self._init + self._current
                return timeout
 
+       def __str__(self):
+               return "GeometricStateStrategy(timeout=%r)" % (
+                       self.timeout
+               )
+
        def __repr__(self):
                return "GeometricStateStrategy(init=%r, min=%r, max=%r)" % (
                        self._init, self._min, self._max
@@ -226,6 +234,7 @@ class UpdateStateMachine(StateMachine):
                self._name = name
                self._updateItems = updateItems
                self._maxTime = maxTime
+               self._isActive = False
 
                self._state = self.STATE_ACTIVE
                self._onTimeout = gobject_utils.Timeout(self._on_timeout)
@@ -237,6 +246,14 @@ class UpdateStateMachine(StateMachine):
                        )
                )
 
+       def __str__(self):
+               return """UpdateStateMachine(
+       name=%r,
+       strategie=%s,
+       isActive=%r,
+       isPolling=%r,
+)""" % (self._name, self._strategy, self._isActive, self._onTimeout.is_running())
+
        def __repr__(self):
                return """UpdateStateMachine(
        name=%r,
@@ -251,10 +268,12 @@ class UpdateStateMachine(StateMachine):
                        strategy.initialize_state()
                if self._strategy.timeout != self.INFINITE_PERIOD:
                        self._onTimeout.start(seconds=0)
+               self._isActive = True
                _moduleLogger.info("%s Starting State Machine" % (self._name, ))
 
        def stop(self):
                _moduleLogger.info("%s Stopping State Machine" % (self._name, ))
+               self._isActive = False
                self._onTimeout.cancel()
 
        def close(self):
@@ -294,7 +313,7 @@ class UpdateStateMachine(StateMachine):
                self._reset_timers()
 
        def _reset_timers(self, initialize=False):
-               if self._timeoutId is None:
+               if not self._isActive:
                        return # not started yet
                _moduleLogger.info("%s Resetting State Machine" % (self._name, ))
                self._onTimeout.cancel()