你正在阅读 Celery 3.1 的文档。开发版本文档见: 此处.
# -*- coding: utf-8 -*-
"""
celery.utils.text
~~~~~~~~~~~~~~~~~
Text formatting utilities
"""
from __future__ import absolute_import
import textwrap
from pprint import pformat
[文档]def abbr(S, max, ellipsis='...'):
if S is None:
return '???'
if len(S) > max:
return ellipsis and (S[:max - len(ellipsis)] + ellipsis) or S[:max]
return S
[文档]def abbrtask(S, max):
if S is None:
return '???'
if len(S) > max:
module, _, cls = S.rpartition('.')
module = abbr(module, max - len(cls) - 3, False)
return module + '[.]' + cls
return S
[文档]def indent(t, indent=0):
"""Indent text."""
return '\n'.join(' ' * indent + p for p in t.split('\n'))
[文档]def truncate(text, maxlen=128, suffix='...'):
"""Truncates text to a maximum number of characters."""
if len(text) >= maxlen:
return text[:maxlen].rsplit(' ', 1)[0] + suffix
return text
[文档]def pretty(value, width=80, nl_width=80, **kw):
if isinstance(value, dict):
return '{\n %s' % (pformat(value, 4, nl_width)[1:])
elif isinstance(value, tuple):
return '\n%s%s' % (' ' * 4, pformat(value, width=nl_width, **kw))
else:
return pformat(value, width=width, **kw)