Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19912736
  • 博文数量: 679
  • 博客积分: 10495
  • 博客等级: 上将
  • 技术积分: 9308
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 10:51
文章分类

全部博文(679)

文章存档

2012年(5)

2011年(38)

2010年(86)

2009年(145)

2008年(170)

2007年(165)

2006年(89)

分类: Python/Ruby

2010-02-10 09:53:07

from robottypes import is_str, unic


_MAX_ASSIGN_LENGTH = 200
_MAX_ERROR_LINES = 20
_MAX_ERROR_LINE_LENGTH = 78
_ERROR_CUT_EXPLN = ('    [ Message content over the limit has been removed. ]')


def cut_long_message(msg):
    if not is_str(msg):
        msg = unic(msg)
    lines = msg.splitlines()
    lengths = _count_line_lenghts(lines)
    if sum(lengths) <= _MAX_ERROR_LINES:
        return msg
    start = _prune_excess_lines(lines, lengths)
    end = _prune_excess_lines(lines, lengths, True)
    return '\n'.join(start + [_ERROR_CUT_EXPLN] + end)

def _prune_excess_lines(lines, lengths, from_end=False):
    if from_end:
        lines.reverse()
        lengths.reverse()
    ret = []
    total = 0
    limit = _MAX_ERROR_LINES/2
    for line, length in zip(lines[:limit], lengths[:limit]):
        if total + length >= limit:
            ret.append(_cut_long_line(line, total, from_end))
            break
        total += length
        ret.append(line)
    if from_end:
        ret.reverse()
    return ret

def _cut_long_line(line, used, from_end):
    available_lines = _MAX_ERROR_LINES/2 - used
    available_chars = available_lines * _MAX_ERROR_LINE_LENGTH - 3
    if len(line) > available_chars:
        if not from_end:
            line = line[:available_chars] + '...'
        else:
            line = '...' + line[-available_chars:]
    return line
    
def _count_line_lenghts(lines):
    return [ _count_virtual_line_length(line) for line in lines ]

def _count_virtual_line_length(line):
    length = len(line) / _MAX_ERROR_LINE_LENGTH
    if not len(line) % _MAX_ERROR_LINE_LENGTH == 0 or len(line) == 0:
        length += 1
    return length


# TODO: rename _msg -> _message
def cut_long_assign_msg(msg):
    if not is_str(msg):
        msg = unic(msg)
    if len(msg) <= _MAX_ASSIGN_LENGTH:
        return msg
    return msg[:_MAX_ASSIGN_LENGTH] + '...'


def wrap(text, width, indent=0):
    """Wraps given text so that it fits into given width with optional indent.
   
    Preserves existing line breaks and most spaces in the text. Expects that
    existing line breaks are posix newlines (\n).

    Based on a recipe from ActiveState Python Cookbook at
   
    """
    text = reduce(lambda line, word, width=width: '%s%s%s' %
                  (line,
                   ' \n'[(len(line)-line.rfind('\n')-1
                         + len(word.split('\n',1)[0]
                              ) >= width)],
                   word),
                  text.split(' ')
                 )
    if not indent > 0:
        return text
    pre = ' ' * indent
    joiner = '\n' + pre
    return pre + joiner.join(text.splitlines())

文件路径:robotframework-2.1.2\src\robot\utils\text.py
功能:文本处理,主要为信息过多去掉一部分。
函数cut_long_message:去处过多的信息
函数wrap为缩进处理
此文件尚未进行完整阅读


阅读(30107) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~