From d6e6c1293943d71413fe8e857cfc38cd067f7f6f Mon Sep 17 00:00:00 2001 From: Griatch Date: Fri, 14 Feb 2014 01:31:09 +0100 Subject: [PATCH] Fixed so refurbished Scripts work normally. Added script.fire() method to fire the script on-demand, as suggested in #420. Also added as method remaining_repeats() to be able to get how many more times the script will fire. --- src/commands/default/system.py | 15 +++++++++++-- src/scripts/scripts.py | 41 ++++++++++++++++++---------------- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/commands/default/system.py b/src/commands/default/system.py index 2f71d3c0ff..766c51ff73 100644 --- a/src/commands/default/system.py +++ b/src/commands/default/system.py @@ -227,12 +227,23 @@ def format_script_list(scripts): table.align = 'r' for script in scripts: nextrep = script.time_until_next_repeat() + if nextrep is None: + nextrep = "PAUS" if script.db._paused_time else "--" + else: + nextrep = "%ss" % nextrep + + maxrepeat = script.repeats + if maxrepeat: + rept = "%i/%i" % (maxrepeat - script.remaining_repeats(), maxrepeat) + else: + rept = "-/-" + table.add_row([script.id, script.obj.key if (hasattr(script, 'obj') and script.obj) else "", script.key, script.interval if script.interval > 0 else "--", - "%ss" % nextrep if nextrep else "--", - "%i/%i" % (script.remaining_repeats(), script.repeats) if script.repeats else "--", + nextrep, + rept, "*" if script.persistent else "-", script.typeclass_path.rsplit('.', 1)[-1], crop(script.desc, width=20)]) diff --git a/src/scripts/scripts.py b/src/scripts/scripts.py index 3cdec6c202..4c8fffcfdc 100644 --- a/src/scripts/scripts.py +++ b/src/scripts/scripts.py @@ -62,7 +62,7 @@ class ExtendedLoopingCall(LoopingCall): self() else: if self.repeats is not None: - # need to ignore the first reschedule + # need to compensate for the first reschedule self.repeats += 1 if start_delay is not None and start_delay >= 0: # we set start_delay after the _reshedule call to make @@ -82,23 +82,21 @@ class ExtendedLoopingCall(LoopingCall): nulling start_delay and stopping if number of repeats is reached. """ + self.start_delay = None if self.repeats is not None: self.repeats -= 1 - self.start_delay = None super(ExtendedLoopingCall, self)._reschedule() - # we cannot kill the task here, it seems the - # callback will often not have time to be - # triggered if we do. So kill from outside - # by checking repeats <= 0. - #if self.repeats <= 0: - # self.stop() + # for self.repeats <= 0, don't kill loop here; the callback + # won't have time to be called in some situations. Kill + # externally by checking task.repeats <= 0. def fire(self): "Force-fire the callback" - assert self.running ("Tried to fire an ExtendedLoopingCall " - "that was not running.") + assert self.running, ("Tried to fire an ExtendedLoopingCall " + "that was not running.") if self.call is not None: self.call.cancel() + self._expectNextCallAt = self.clock.seconds() self() def next_call_time(self): @@ -106,9 +104,10 @@ class ExtendedLoopingCall(LoopingCall): Return the time in seconds until the next call. This takes start_delay into account. """ - currentTime = self.clock.seconds() - return self._expectNextCallAt - currentTime - + if self.running: + currentTime = self.clock.seconds() + return self._expectNextCallAt - currentTime + return None # # Base script, inherit from Script below instead. @@ -139,15 +138,15 @@ class ScriptBase(TypeClass): # the script was paused; restarting repeats = self.db._paused_repeats or self.dbobj.db_repeats self.ndb._task.start(self.dbobj.db_interval, - now=False, - start_delay=self.ndb._paused_time, - repeats=repeats) + now=False, + start_delay=self.db._paused_time, + repeats=repeats) del self.db._paused_time del self.db._paused_repeats else: # starting script anew self.ndb._task.start(self.dbobj.db_interval, - now=self.dbobj.db_start_delay, + now=not self.dbobj.db_start_delay, repeats=self.dbobj.db_repeats) def _stop_task(self): @@ -181,6 +180,7 @@ class ScriptBase(TypeClass): # check repeats repeats = self.ndb._task.repeats if repeats is not None and repeats <= 0: + print "stopping script!" self.stop() def _step_task(self): @@ -202,7 +202,10 @@ class ScriptBase(TypeClass): """ task = self.ndb._task if task: - return int(task.next_call_time()) + try: + return int(task.next_call_time()) + except TypeError: + pass return None def remaining_repeats(self): @@ -284,8 +287,8 @@ class ScriptBase(TypeClass): """ This stops a running script and stores its active state. """ - #print "pausing", self.key, self.time_until_next_repeat() task = self.ndb._task + print "pausing", self.key, self.time_until_next_repeat() if task: self.db._paused_time = task.next_call_time() self.db._paused_repeats = task.repeats