Commit 7fb41afc authored by root's avatar root

Python3

parent ad97104d
...@@ -295,7 +295,7 @@ def parseCronLine(line): ...@@ -295,7 +295,7 @@ def parseCronLine(line):
if not line: if not line:
raise InvalidCronLine('Empty cron line provided') raise InvalidCronLine('Empty cron line provided')
if not isinstance(line, basestring): if not isinstance(line, str):
raise InvalidCronLine('Cron line must be a string') raise InvalidCronLine('Cron line must be a string')
line = re.split('\s+',line.strip()) line = re.split('\s+',line.strip())
...@@ -449,7 +449,7 @@ def parseCronEntry(entry,min,max): ...@@ -449,7 +449,7 @@ def parseCronEntry(entry,min,max):
if not entry: if not entry:
raise InvalidCronEntry('Empty cron entry') raise InvalidCronEntry('Empty cron entry')
if not isinstance(entry, basestring): if not isinstance(entry, str):
raise InvalidCronEntry('Cron entry must be a string') raise InvalidCronEntry('Cron entry must be a string')
try: try:
...@@ -523,7 +523,7 @@ def parseCronEntry(entry,min,max): ...@@ -523,7 +523,7 @@ def parseCronEntry(entry,min,max):
if begin == end and begin % step != 0: if begin == end and begin % step != 0:
raise InvalidCronEntry('Invalid range or step specified: %s' % (e)) raise InvalidCronEntry('Invalid range or step specified: %s' % (e))
total.update(range(begin,end,step)) total.update(list(range(begin,end,step)))
elif not begin is None: elif not begin is None:
raise InvalidCronEntry('Invalid range or step specified: %s' % (e)) raise InvalidCronEntry('Invalid range or step specified: %s' % (e))
......
This diff is collapsed.
...@@ -65,7 +65,7 @@ class ScheduledCall(object): ...@@ -65,7 +65,7 @@ class ScheduledCall(object):
self._reschedule() self._reschedule()
return self.deferred return self.deferred
except Exception, e: except Exception as e:
log.error('Exception while starting %r: %s' % (self, str(e),)) log.error('Exception while starting %r: %s' % (self, str(e),))
self.running = False self.running = False
self.deferred = None self.deferred = None
...@@ -119,9 +119,9 @@ class ScheduledCall(object): ...@@ -119,9 +119,9 @@ class ScheduledCall(object):
def __repr__(self): def __repr__(self):
if hasattr(self.f, 'func_name'): if hasattr(self.f, 'func_name'):
func = self.f.func_name func = self.f.__name__
if hasattr(self.f, 'im_class'): if hasattr(self.f, 'im_class'):
func = self.f.im_class.__name__ + '.' + func func = self.f.__self__.__class__.__name__ + '.' + func
else: else:
func = reflect.safe_repr(self.f) func = reflect.safe_repr(self.f)
......
from logging import getLogger
from twisted.python import reflect
from twisted.internet import defer
from txscheduling.interfaces import ISchedule
log = getLogger('twisted.schedule.task')
class ScheduledCall(object):
"""Call a function repeatedly.
If C{f} returns a deferred, rescheduling will not take place until the
deferred has fired. The result value is ignored.
@ivar f: The function to call.
@ivar a: A tuple of arguments to pass the function.
@ivar kw: A dictionary of keyword arguments to pass to the function.
@ivar clock: A provider of
L{twisted.internet.interfaces.IReactorTime}. The default is
L{twisted.internet.reactor}. Feel free to set this to
something else, but it probably ought to be set *before*
calling L{start}.
@type _lastTime: C{float}
@ivar _lastTime: The time at which this instance most recently scheduled
itself to run.
"""
def __init__(self, f, *a, **kw):
self.call = None
self.running = False
self.scheduled = None
self._lastTime = 0.0
self.starttime = None
self.f = f
self.a = a
self.kw = kw
from twisted.internet import reactor
self.clock = reactor
def start(self, schedule):
"""Start running function based on the provided schedule.
@param schedule: An object that provides or can be adapted to an
ISchedule interface.
@return: A Deferred whose callback will be invoked with
C{self} when C{self.stop} is called, or whose errback will be
invoked when the function raises an exception or returned a
deferred that has its errback invoked.
"""
assert not self.running, ("Tried to start an already running "
"ScheduledCall.")
self.schedule = ISchedule(schedule)
try:
self.running = True
self.deferred = defer.Deferred()
self.starttime = self.clock.seconds()
self._lastTime = None
self._reschedule()
return self.deferred
except Exception, e:
log.error('Exception while starting %r: %s' % (self, str(e),))
self.running = False
self.deferred = None
if self.call is not None:
try:
self.call.cancel()
except:
pass
raise e
finally:
log.debug('%r.start(%r) started' % (self, schedule))
def stop(self):
""" Stop running function. """
assert self.running, ("Tried to stop a ScheduledCall that was "
"not running.")
self.running = False
if self.call is not None:
self.call.cancel()
self.call = None
d, self.deferred = self.deferred, None
d.callback(self)
def __call__(self):
def cb(result):
if self.running:
self._reschedule()
else:
d, self.deferred = self.deferred, None
d.callback(self)
def eb(failure):
self.running = False
d, self.deferred = self.deferred, None
d.errback(failure)
self.call = None
d = defer.maybeDeferred(self.f, *self.a, **self.kw)
d.addCallback(cb)
d.addErrback(eb)
def _reschedule(self):
""" Schedule the next iteration of this scheduled call. """
if self.call is None:
delay = self.schedule.getDelayForNext()
self._lastTime = self.clock.seconds() + delay
self.call = self.clock.callLater(delay, self)
def __repr__(self):
if hasattr(self.f, 'func_name'):
func = self.f.func_name
if hasattr(self.f, 'im_class'):
func = self.f.im_class.__name__ + '.' + func
else:
func = reflect.safe_repr(self.f)
return 'ScheduledCall<%s>(%s, *%s, **%s)' % (
self.schedule, func, reflect.safe_repr(self.a),
reflect.safe_repr(self.kw))
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
...@@ -92,8 +92,8 @@ class CallableTests(TestCase): ...@@ -92,8 +92,8 @@ class CallableTests(TestCase):
self.sc = TestableScheduledCall(self.clock, f) self.sc = TestableScheduledCall(self.clock, f)
def errback(fail): def errback(fail):
self.assert_(fail.check(TestException), self.assertTrue(fail.check(TestException),
u'Expecting a TestException failure') 'Expecting a TestException failure')
def callback(result): def callback(result):
self.fail('Callback should not be called') self.fail('Callback should not be called')
...@@ -110,8 +110,8 @@ class CallableTests(TestCase): ...@@ -110,8 +110,8 @@ class CallableTests(TestCase):
self.sc = TestableScheduledCall(self.clock, f) self.sc = TestableScheduledCall(self.clock, f)
def errback(fail): def errback(fail):
self.assert_(fail.check(TypeError), self.assertTrue(fail.check(TypeError),
u'Expecting a TypeError failure') 'Expecting a TypeError failure')
def callback(result): def callback(result):
self.fail('Callback should not be called') self.fail('Callback should not be called')
...@@ -179,36 +179,36 @@ class SimpleTimingTests(TestCase): ...@@ -179,36 +179,36 @@ class SimpleTimingTests(TestCase):
""" No calls before scheduled delay """ """ No calls before scheduled delay """
self.sc.start(SimpleSchedule(1)) self.sc.start(SimpleSchedule(1))
self.assertEqual(self.callable.count, 0, self.assertEqual(self.callable.count, 0,
u'Callable should not be called before time has passed') 'Callable should not be called before time has passed')
self.clock.pump([0.9]) self.clock.pump([0.9])
self.assertEqual(self.callable.count, 0, self.assertEqual(self.callable.count, 0,
u'Callable should not be called before sufficient time ' 'Callable should not be called before sufficient time '
'has passed') 'has passed')
self.sc.stop() self.sc.stop()
self.assertEqual(self.callable.count, 0, self.assertEqual(self.callable.count, 0,
u'Callable should not be called after stopping') 'Callable should not be called after stopping')
self.clock.pump([0.1]*50) self.clock.pump([0.1]*50)
self.assertEqual(self.callable.count, 0, self.assertEqual(self.callable.count, 0,
u'Callable should not be called after stopping') 'Callable should not be called after stopping')
def test_calls(self): def test_calls(self):
""" Verify calls at proper times """ """ Verify calls at proper times """
self.sc.start(SimpleSchedule(1)) self.sc.start(SimpleSchedule(1))
self.assertEqual(self.callable.count, 0, self.assertEqual(self.callable.count, 0,
u'Callable should not be called before time has passed') 'Callable should not be called before time has passed')
self.clock.pump([0.1]*11) self.clock.pump([0.1]*11)
self.assertEqual(self.callable.count, 1, self.assertEqual(self.callable.count, 1,
u'Callable should be called once now') 'Callable should be called once now')
self.clock.pump([0.1]*10) self.clock.pump([0.1]*10)
self.assertEqual(self.callable.count, 2, self.assertEqual(self.callable.count, 2,
u'Callable should be called twice now') 'Callable should be called twice now')
self.sc.stop() self.sc.stop()
self.assertEqual(self.callable.count, 2, self.assertEqual(self.callable.count, 2,
u'Callable should not be called after stopping') 'Callable should not be called after stopping')
self.clock.pump([0.1]*50) self.clock.pump([0.1]*50)
self.assertEqual(self.callable.count, 2, self.assertEqual(self.callable.count, 2,
u'Callable should not be called after stopping') 'Callable should not be called after stopping')
class LongRunningTimingTests(TestCase): class LongRunningTimingTests(TestCase):
...@@ -227,86 +227,86 @@ class LongRunningTimingTests(TestCase): ...@@ -227,86 +227,86 @@ class LongRunningTimingTests(TestCase):
""" No calls before scheduled delay on long running task """ """ No calls before scheduled delay on long running task """
self.sc.start(SimpleSchedule(1)) self.sc.start(SimpleSchedule(1))
self.assertEqual(self.callable.count, 0, self.assertEqual(self.callable.count, 0,
u'Callable should not be called before time has passed') 'Callable should not be called before time has passed')
self.assertEqual(self.started.count, 0, self.assertEqual(self.started.count, 0,
u'Callable should not be called before time has passed') 'Callable should not be called before time has passed')
self.clock.pump([0.9]) self.clock.pump([0.9])
self.assertEqual(self.callable.count, 0, self.assertEqual(self.callable.count, 0,
u'Callable should not be called before sufficient time ' 'Callable should not be called before sufficient time '
'has passed') 'has passed')
self.assertEqual(self.started.count, 0, self.assertEqual(self.started.count, 0,
u'Callable should not be called before sufficient time ' 'Callable should not be called before sufficient time '
'has passed') 'has passed')
self.sc.stop() self.sc.stop()
self.assertEqual(self.callable.count, 0, self.assertEqual(self.callable.count, 0,
u'Callable should not be called after stopping') 'Callable should not be called after stopping')
self.assertEqual(self.started.count, 0, self.assertEqual(self.started.count, 0,
u'Callable should not be called after stopping') 'Callable should not be called after stopping')
self.clock.pump([0.1]*50) self.clock.pump([0.1]*50)
self.assertEqual(self.callable.count, 0, self.assertEqual(self.callable.count, 0,
u'Callable should not be called after stopping') 'Callable should not be called after stopping')
self.assertEqual(self.started.count, 0, self.assertEqual(self.started.count, 0,
u'Callable should not be called after stopping') 'Callable should not be called after stopping')
def test_calls(self): def test_calls(self):
""" Verify calls at proper times on long running task """ """ Verify calls at proper times on long running task """
self.sc.start(SimpleSchedule(2)) self.sc.start(SimpleSchedule(2))
self.assertEqual(self.callable.count, 0, self.assertEqual(self.callable.count, 0,
u'Callable should not be called before time has passed') 'Callable should not be called before time has passed')
self.assertEqual(self.started.count, 0, self.assertEqual(self.started.count, 0,
u'Callable should not be called before time has passed') 'Callable should not be called before time has passed')
# Time will be approx. 2.1 # Time will be approx. 2.1
self.clock.pump([0.1]*21) self.clock.pump([0.1]*21)
self.assertEqual(self.callable.count, 0, self.assertEqual(self.callable.count, 0,
u'Callable should be running now: %f' % (self.clock.rightNow,)) 'Callable should be running now: %f' % (self.clock.rightNow,))
self.assertEqual(self.started.count, 1, self.assertEqual(self.started.count, 1,
u'Callable should be running now: %f' % (self.clock.rightNow,)) 'Callable should be running now: %f' % (self.clock.rightNow,))
# Time will be approx. 3.2 # Time will be approx. 3.2
self.clock.pump([0.1]*11) self.clock.pump([0.1]*11)
self.assertEqual(self.callable.count, 1, self.assertEqual(self.callable.count, 1,
u'Callable should be called once now: %f' % (self.clock.rightNow,)) 'Callable should be called once now: %f' % (self.clock.rightNow,))
self.assertEqual(self.started.count, 1, self.assertEqual(self.started.count, 1,
u'Callable should be called once now: %f' % (self.clock.rightNow,)) 'Callable should be called once now: %f' % (self.clock.rightNow,))
# Time will be approx. 3.9 # Time will be approx. 3.9
self.clock.pump([0.1]*7) self.clock.pump([0.1]*7)
self.assertEqual(self.callable.count, 1, self.assertEqual(self.callable.count, 1,
u'Callable should be called once now: %f' % (self.clock.rightNow,)) 'Callable should be called once now: %f' % (self.clock.rightNow,))
self.assertEqual(self.started.count, 1, self.assertEqual(self.started.count, 1,
u'Callable should be called once now: %f' % (self.clock.rightNow,)) 'Callable should be called once now: %f' % (self.clock.rightNow,))
# Time will be approx 5.2 # Time will be approx 5.2
self.clock.pump([0.1]*13) self.clock.pump([0.1]*13)
self.assertEqual(self.callable.count, 1, self.assertEqual(self.callable.count, 1,
u'Callable should be running now: %f' % (self.clock.rightNow,)) 'Callable should be running now: %f' % (self.clock.rightNow,))
self.assertEqual(self.started.count, 2, self.assertEqual(self.started.count, 2,
u'Callable should be running now: %f' % (self.clock.rightNow,)) 'Callable should be running now: %f' % (self.clock.rightNow,))
# Time will be approx. 6.4 # Time will be approx. 6.4
self.clock.pump([0.1]*12) self.clock.pump([0.1]*12)
self.assertEqual(self.callable.count, 2, self.assertEqual(self.callable.count, 2,
u'Callable should be called twice now: %f' % (self.clock.rightNow,)) 'Callable should be called twice now: %f' % (self.clock.rightNow,))
self.assertEqual(self.started.count, 2, self.assertEqual(self.started.count, 2,
u'Callable should be called twice now: %f' % (self.clock.rightNow,)) 'Callable should be called twice now: %f' % (self.clock.rightNow,))
self.sc.stop() self.sc.stop()
self.assertEqual(self.callable.count, 2, self.assertEqual(self.callable.count, 2,
u'Callable should not be called after stopping: %f' % (self.clock.rightNow,)) 'Callable should not be called after stopping: %f' % (self.clock.rightNow,))
self.assertEqual(self.started.count, 2, self.assertEqual(self.started.count, 2,
u'Callable should not be called after stopping: %f' % (self.clock.rightNow,)) 'Callable should not be called after stopping: %f' % (self.clock.rightNow,))
self.clock.pump([0.1]*50) self.clock.pump([0.1]*50)
self.assertEqual(self.callable.count, 2, self.assertEqual(self.callable.count, 2,
u'Callable should not be called after stopping: %f' % (self.clock.rightNow,)) 'Callable should not be called after stopping: %f' % (self.clock.rightNow,))
self.assertEqual(self.started.count, 2, self.assertEqual(self.started.count, 2,
u'Callable should not be called after stopping: %f' % (self.clock.rightNow,)) 'Callable should not be called after stopping: %f' % (self.clock.rightNow,))
def test_suite(): def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment