Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19912592
  • 博文数量: 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:33:11

import re
import string

from robottypes import is_str


_escape_re = re.compile(r'(\\+)([^\\]{0,2})')   # escapes and nextchars


def escape(item):
    if not is_str(item):
        return item
    for orig in [ '\\', '${', '@{', '%{', '&{', '*{' ]:
        item = item.replace(orig, '\\' + orig)
    return item

def unescape(item):
    if not is_str(item):
        return item
    result = []
    unprocessed = item
    while True:
        res = _escape_re.search(unprocessed)
        # If no escapes found append string to result and exit loop
        if res is None:
            result.append(unprocessed)
            break
        # Split string to pre match, escapes, nextchars and unprocessed parts
        # (e.g. '
') where nextchars contains 0-2 chars
        # and unprocessed may contain more escapes. Pre match part contains
        # no escapes can is appended directly to result.
        result.append(unprocessed[:res.start()])
        escapes = res.group(1)
        nextchars = res.group(2)
        unprocessed = unprocessed[res.end():]
        # Append every second escape char to result
        result.append('\\' * (len(escapes) / 2))
        # Handle '\n', '\r' and '\t'. Note that both '\n' and '\n ' are
        # converted to '\n'
        if len(escapes) % 2 == 0 or len(nextchars) == 0 \
                    or nextchars[0] not in ['n','r','t']:
            result.append(nextchars)
        elif nextchars[0] == 'n':
            if len(nextchars) == 1 or nextchars[1] == ' ':
                result.append('\n')
            else:
                result.append('\n' + nextchars[1])
        elif nextchars[0] == 'r':
            result.append('\r' + nextchars[1:])
        else:
            result.append('\t' + nextchars[1:])
    return ''.join(result)


def escape_file_name(filename):
    """Escapes filename.
    
    Use only with actual file name and not with full path because possible
    '/' and '\\' in the given name are also escaped!
    """
    return ''.join([ _escape_char(c) for c in filename ])       


_ok_chars = string.ascii_letters + string.digits + '-+.'
_replaced_chars = { u'\xe4' : 'a',  u'\xe5' : 'a',
                    u'\xc4' : 'A',  u'\xc5' : 'A',
                    u'\xf6' : 'o',  u'\xd6' : 'O',
                    u'\xfc' : 'u',  u'\xdc' : 'U', }

def _escape_char(char):
    if char in _ok_chars: 
        return char
    elif _replaced_chars.has_key(char):
        return _replaced_chars[char]
    else:
        return '_'

 文件路径:robotframework-2.1.2\src\robot\utils\escaping.py
功能:关于转义函数的定义。
_escape_re = re.compile(r'(\\+)([^\\]{0,2})')  为用正则表达式来匹配转义序列,这里奇怪的是只抓取反斜杠和后面2个字符,需要以后查看转义的表示方法。
escape对特殊符号多加一次转义。
unescape为对转义的处理
escape_file_name以及后面的好像没有地方调用,暂时不用理睬

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