def test_CrontabSchedule_unicode(self): assert text_t(CrontabSchedule( minute=3, hour=3, day_of_week=None, )) == '3 3 * * * (m/h/d/dM/MY) UTC' assert text_t(CrontabSchedule( minute=3, hour=3, day_of_week='tue', day_of_month='*/2', month_of_year='4,6', )) == '3 3 tue */2 4,6 (m/h/d/dM/MY) UTC'
def test_CrontabSchedule_unicode(self): assert text_t(CrontabSchedule( minute=3, hour=3, day_of_week=None, )) == '3 3 * * * (m/h/d/dM/MY)' assert text_t( CrontabSchedule( minute=3, hour=3, day_of_week='tue', day_of_month='*/2', month_of_year='4,6', )) == '3 3 tue */2 4,6 (m/h/d/dM/MY)'
def start_scheduler(self): c = self.colored if self.pidfile: platforms.create_pidlock(self.pidfile) beat = self.Service(app=self.app, max_interval=self.max_interval, scheduler_cls=self.scheduler_cls, schedule_filename=self.schedule) print( text_t( # noqa (pyflakes chokes on print) c.blue('__ ', c.magenta('-'), c.blue(' ... __ '), c.magenta('-'), c.blue(' _\n'), c.reset(self.startup_info(beat))), )) self.setup_logging() if self.socket_timeout: logger.debug('Setting default socket timeout to %r', self.socket_timeout) socket.setdefaulttimeout(self.socket_timeout) try: self.install_sync_handler(beat) beat.start() except Exception as exc: logger.critical('beat raised exception %s: %r', exc.__class__, exc, exc_info=True) raise exc
def test_PeriodicTask_unicode_solar(self): p = self.create_model_solar( solar('solar_noon', 48.06, 12.86), name='solar_event' ) assert text_t(p) == 'solar_event: {0} ({1}, {2})'.format( 'solar_noon', '48.06', '12.86' )
def format(self, record): msg = logging.Formatter.format(self, record) color = self.colors.get(record.levelname) # reset exception info later for other handlers... einfo = sys.exc_info() if record.exc_info == 1 else record.exc_info if color and self.use_color: try: # safe_str will repr the color object # and color will break on non-string objects # so need to reorder calls based on type. # Issue #427 try: if isinstance(msg, string_t): return text_t(color(safe_str(msg))) return safe_str(color(msg)) except UnicodeDecodeError: # pragma: no cover return safe_str(msg) # skip colors except Exception as exc: # pylint: disable=broad-except prev_msg, record.exc_info, record.msg = ( record.msg, 1, '<Unrepresentable {0!r}: {1!r}>'.format( type(msg), exc ), ) try: return logging.Formatter.format(self, record) finally: record.msg, record.exc_info = prev_msg, einfo else: return safe_str(msg)
def test_taskmeta(self, ctype='application/json', cenc='utf-8'): m1 = self.create_task_result() m2 = self.create_task_result() m3 = self.create_task_result() assert text_t(m1).startswith('<Task:') assert m1.task_id assert isinstance(m1.date_done, datetime) assert TaskResult.objects.get_task(m1.task_id).task_id == m1.task_id assert TaskResult.objects.get_task(m1.task_id).status != states.SUCCESS TaskResult.objects.store_result( ctype, cenc, m1.task_id, True, status=states.SUCCESS) TaskResult.objects.store_result( ctype, cenc, m2.task_id, True, status=states.SUCCESS) assert TaskResult.objects.get_task(m1.task_id).status == states.SUCCESS assert TaskResult.objects.get_task(m2.task_id).status == states.SUCCESS # Have to avoid save() because it applies the auto_now=True. TaskResult.objects.filter( task_id=m1.task_id ).update(date_done=now() - timedelta(days=10)) expired = TaskResult.objects.get_all_expired( self.app.conf.result_expires, ) assert m1 in expired assert m2 not in expired assert m3 not in expired TaskResult.objects.delete_expired( self.app.conf.result_expires, ) assert m1 not in TaskResult.objects.all()
def format(self, record): sformat = logging.Formatter.format color = self.colors.get(record.levelname) if color and self.use_color: msg = record.msg try: # safe_str will repr the color object # and color will break on non-string objects # so need to reorder calls based on type. # Issue #427 try: if isinstance(msg, string_t): record.msg = text_t(color(safe_str(msg))) else: record.msg = safe_str(color(msg)) except UnicodeDecodeError: record.msg = safe_str(msg) # skip colors except Exception as exc: record.msg = '<Unrepresentable {0!r}: {1!r}>'.format( type(msg), exc) record.exc_info = True return sformat(self, record) else: return safe_str(sformat(self, record))
def handle_retry(self, task, req, store_errors=True, **kwargs): """Handle retry exception.""" # the exception raised is the Retry semi-predicate, # and it's exc' attribute is the original exception raised (if any). type_, _, tb = sys.exc_info() try: reason = self.retval einfo = ExceptionInfo((type_, reason, tb)) if store_errors: task.backend.mark_as_retry( req.id, reason.exc, einfo.traceback, request=req, ) task.on_retry(reason.exc, req.id, req.args, req.kwargs, einfo) signals.task_retry.send(sender=task, request=req, reason=reason, einfo=einfo) info( LOG_RETRY, { 'id': req.id, 'name': get_task_name(req, task.name), 'exc': text_t(reason), }) return einfo finally: del tb
def test_PeriodicTask_unicode_crontab(self): p = self.create_model_crontab( crontab( hour='4, 5', day_of_week='4, 5', )) assert text_t(p) == '{0}: * 4,5 4,5 * * (m/h/d/dM/MY)'.format(p.name)
def start_scheduler(self): c = self.colored if self.pidfile: platforms.create_pidlock(self.pidfile) beat = self.Service(app=self.app, max_interval=self.max_interval, scheduler_cls=self.scheduler_cls, schedule_filename=self.schedule) print(text_t( # noqa (pyflakes chokes on print) c.blue('__ ', c.magenta('-'), c.blue(' ... __ '), c.magenta('-'), c.blue(' _\n'), c.reset(self.startup_info(beat))), )) self.setup_logging() if self.socket_timeout: logger.debug('Setting default socket timeout to %r', self.socket_timeout) socket.setdefaulttimeout(self.socket_timeout) try: self.install_sync_handler(beat) beat.start() except Exception as exc: logger.critical('beat raised exception %s: %r', exc.__class__, exc, exc_info=True) raise
def test_taskmeta(self, ctype='application/json', cenc='utf-8'): m1 = self.create_task_result() m2 = self.create_task_result() m3 = self.create_task_result() assert text_t(m1).startswith('<Task:') assert m1.task_id assert isinstance(m1.date_done, datetime) assert TaskResult.objects.get_task(m1.task_id).task_id == m1.task_id assert TaskResult.objects.get_task(m1.task_id).status != states.SUCCESS TaskResult.objects.store_result(ctype, cenc, m1.task_id, True, status=states.SUCCESS) TaskResult.objects.store_result(ctype, cenc, m2.task_id, True, status=states.SUCCESS) assert TaskResult.objects.get_task(m1.task_id).status == states.SUCCESS assert TaskResult.objects.get_task(m2.task_id).status == states.SUCCESS # Have to avoid save() because it applies the auto_now=True. TaskResult.objects.filter(task_id=m1.task_id).update( date_done=now() - timedelta(days=10)) expired = TaskResult.objects.get_all_expired( self.app.conf.result_expires, ) assert m1 in expired assert m2 not in expired assert m3 not in expired TaskResult.objects.delete_expired(self.app.conf.result_expires, ) assert m1 not in TaskResult.objects.all()
def test_PeriodicTask_unicode_clocked(self): time = make_aware(datetime.now()) p = self.create_model_clocked( clocked(time), name='clocked_event' ) assert text_t(p) == '{0}: {1} {2}'.format( 'clocked_event', str(time), True )
def test_PeriodicTask_unicode_crontab(self): p = self.create_model_crontab(crontab( hour='4, 5', day_of_week='4, 5', )) assert text_t(p) == """{0}: * 4,5 4,5 * * (m/h/d/dM/MY) UTC""".format( p.name )
def banner(self, service): c = self.colored return text_t( # flake8: noqa c.blue('__ ', c.magenta('-'), c.blue(' ... __ '), c.magenta('-'), c.blue(' _\n'), c.reset(self.startup_info(service))), )
def on_after_fork(self): try: self.backend.client.connection_pool.reset() if self._pubsub is not None: self._pubsub.close() except KeyError as e: logger.warning(text_t(e)) super(ResultConsumer, self).on_after_fork()
def test_colors(self): colors = ( ('black', term.BLACK), ('red', term.RED), ('green', term.GREEN), ('yellow', term.YELLOW), ('blue', term.BLUE), ('magenta', term.MAGENTA), ('cyan', term.CYAN), ('white', term.WHITE), ) for name, key in colors: self.assertIn(fg(30 + key), str(colored().names[name]('foo'))) self.assertTrue(str(colored().bold('f'))) self.assertTrue(str(colored().underline('f'))) self.assertTrue(str(colored().blink('f'))) self.assertTrue(str(colored().reverse('f'))) self.assertTrue(str(colored().bright('f'))) self.assertTrue(str(colored().ired('f'))) self.assertTrue(str(colored().igreen('f'))) self.assertTrue(str(colored().iyellow('f'))) self.assertTrue(str(colored().iblue('f'))) self.assertTrue(str(colored().imagenta('f'))) self.assertTrue(str(colored().icyan('f'))) self.assertTrue(str(colored().iwhite('f'))) self.assertTrue(str(colored().reset('f'))) self.assertTrue(text_t(colored().green('∂bar'))) self.assertTrue(colored().red('éefoo') + colored().green('∂bar')) self.assertEqual(colored().red('foo').no_color(), 'foo') self.assertTrue(repr(colored().blue('åfoo'))) self.assertIn("''", repr(colored())) c = colored() s = c.red('foo', c.blue('bar'), c.green('baz')) self.assertTrue(s.no_color()) c._fold_no_color(s, 'øfoo') c._fold_no_color('fooå', s) c = colored().red('åfoo') self.assertEqual( c._add(c, 'baræ'), '\x1b[1;31m\xe5foo\x1b[0mbar\xe6', ) c2 = colored().blue('ƒƒz') c3 = c._add(c, c2) self.assertEqual( c3, '\x1b[1;31m\xe5foo\x1b[0m\x1b[1;34m\u0192\u0192z\x1b[0m', )
def banner(self, service): c = self.colored return text_t( # flake8: noqa c.blue( "__ ", c.magenta("-"), c.blue(" ... __ "), c.magenta("-"), c.blue(" _\n"), c.reset(self.startup_info(service)), ), )
def old_repr(s): return text_t( RE_LONG_SUFFIX.sub( r'\1', RE_EMPTY_SET_REPR.sub( RE_EMPTY_SET_REPR_REPLACE, RE_OLD_SET_REPR.sub( RE_OLD_SET_REPR_REPLACE, RE_OLD_SET_CUSTOM_REPR.sub(RE_OLD_SET_CUSTOM_REPR_REPLACE, repr(s).replace("u'", "'"))), ), ), ).replace('set([])', 'set()')
def old_repr(s): return text_t(RE_LONG_SUFFIX.sub( r'\1', RE_EMPTY_SET_REPR.sub( RE_EMPTY_SET_REPR_REPLACE, RE_OLD_SET_REPR.sub( RE_OLD_SET_REPR_REPLACE, RE_OLD_SET_CUSTOM_REPR.sub( RE_OLD_SET_CUSTOM_REPR_REPLACE, repr(s).replace("u'", "'"), ) ), ), )).replace('set([])', 'set()')
def _repr_result(obj): try: return repr(obj) except Exception as orig_exc: try: return text_t(obj) except UnicodeDecodeError: if isinstance(obj, string_t): try: return obj.decode('utf-8', errors='replace') except Exception: pass return '<Unrepresentable: {0!r} (o.__repr__ returns unicode?)>'.format( orig_exc, )
def handle_retry(self, task, req, store_errors=True, **kwargs): """Handle retry exception.""" # the exception raised is the Retry semi-predicate, # and it's exc' attribute is the original exception raised (if any). type_, _, tb = sys.exc_info() try: reason = self.retval einfo = ExceptionInfo((type_, reason, tb)) if store_errors: task.backend.mark_as_retry(req.id, reason.exc, einfo.traceback, request=req) task.on_retry(reason.exc, req.id, req.args, req.kwargs, einfo) signals.task_retry.send(sender=task, request=req, reason=reason, einfo=einfo) info(LOG_RETRY, {"id": req.id, "name": task.name, "exc": text_t(reason)}) return einfo finally: del tb
def test_IntervalSchedule_unicode(self): assert (text_t(IntervalSchedule(every=1, period='seconds')) == 'every second') assert (text_t(IntervalSchedule(every=10, period='seconds')) == 'every 10 seconds')
def test_PeriodicTask_unicode_interval(self): p = self.create_model_interval(schedule(timedelta(seconds=10))) assert text_t(p) == '{0}: every 10.0 seconds'.format(p.name)
def reprstream(stack, seen=None, maxlevels=3, level=0, isinstance=isinstance): """Streaming repr, yielding tokens.""" # type: (deque, Set, int, int, Callable) -> Iterator[Any] seen = seen or set() append = stack.append popleft = stack.popleft is_in_seen = seen.__contains__ discard_from_seen = seen.discard add_to_seen = seen.add while stack: lit_start = lit_end = None it = popleft() for val in it: orig = val if isinstance(val, _dirty): discard_from_seen(val.objid) continue elif isinstance(val, _literal): level += val.direction yield val, it elif isinstance(val, _key): yield val, it elif isinstance(val, Decimal): yield _repr(val), it elif isinstance(val, safe_t): yield text_t(val), it elif isinstance(val, chars_t): yield _quoted(val), it elif isinstance(val, range_t): # pragma: no cover yield _repr(val), it else: if isinstance(val, set_t): if not val: yield _repr_empty_set(val), it continue lit_start, lit_end, val = _reprseq( val, LIT_SET_START, LIT_SET_END, set, _chainlist, ) elif isinstance(val, tuple): lit_start, lit_end, val = (LIT_TUPLE_START, LIT_TUPLE_END_SV if len(val) == 1 else LIT_TUPLE_END, _chainlist(val)) elif isinstance(val, dict): lit_start, lit_end, val = (LIT_DICT_START, LIT_DICT_END, _chaindict(val)) elif isinstance(val, list): lit_start, lit_end, val = (LIT_LIST_START, LIT_LIST_END, _chainlist(val)) else: # other type of object yield _repr(val), it continue if maxlevels and level >= maxlevels: yield '%s...%s' % (lit_start.value, lit_end.value), it continue objid = id(orig) if is_in_seen(objid): yield _recursion(orig), it continue add_to_seen(objid) # Recurse into the new list/tuple/dict/etc by tacking # the rest of our iterable onto the new it: this way # it works similar to a linked list. append(chain([lit_start], val, [_dirty(objid), lit_end], it)) break
def test_PeriodicTask_unicode_no_schedule(self): p = self.create_model() assert text_t(p) == '{0}: {{no schedule}}'.format(p.name)
def test_unicode(self): assert text_t(colored().green('∂bar')) assert colored().red('éefoo') + colored().green('∂bar') assert colored().red('foo').no_color() == 'foo'
def reprstream(stack, seen=None, maxlevels=3, level=0, isinstance=isinstance): """Streaming repr, yielding tokens.""" seen = seen or set() append = stack.append popleft = stack.popleft is_in_seen = seen.__contains__ discard_from_seen = seen.discard add_to_seen = seen.add while stack: lit_start = lit_end = None it = popleft() for val in it: orig = val if isinstance(val, _dirty): discard_from_seen(val.objid) continue elif isinstance(val, _literal): level += val.direction yield val, it elif isinstance(val, _key): yield val, it elif isinstance(val, Decimal): yield repr(val), it elif isinstance(val, safe_t): yield text_t(val), it elif isinstance(val, chars_t): yield _quoted(val), it elif isinstance(val, range_t): # pragma: no cover yield repr(val), it else: if isinstance(val, set_t): if not val: yield _repr_empty_set(val), it continue lit_start, lit_end, val = _reprseq(val, LIT_SET_START, LIT_SET_END, set, _chainlist) elif isinstance(val, tuple): lit_start, lit_end, val = ( LIT_TUPLE_START, LIT_TUPLE_END_SV if len(val) == 1 else LIT_TUPLE_END, _chainlist(val), ) elif isinstance(val, Mapping): lit_start, lit_end, val = (LIT_DICT_START, LIT_DICT_END, _chaindict(val)) elif isinstance(val, Iterable): lit_start, lit_end, val = (LIT_LIST_START, LIT_LIST_END, _chainlist(val)) else: # other type of object yield repr(val), it continue if maxlevels and level >= maxlevels: yield "%s...%s" % (lit_start.value, lit_end.value), it continue objid = id(orig) if is_in_seen(objid): yield _recursion(orig), it continue add_to_seen(objid) # Recurse into the new list/tuple/dict/etc by tacking # the rest of our iterable onto the new it: this way # it works similar to a linked list. append(chain([lit_start], val, [_dirty(objid), lit_end], it)) break
def test_IntervalSchedule_unicode(self): assert (text_t(IntervalSchedule(every=1, period='seconds')) == 'every second') assert (text_t(IntervalSchedule( every=10, period='seconds')) == 'every 10 seconds')