Source

pyyaml-legacy / yaml / yaml_escapes.py

Full commit
import re

class YamlInvalidEscapeChar(Exception): pass

escapeCodes = {
    '0'  : '\x00',
    'a'  : '\x07',
    'b'  : '\x08',
    't'  : '\x09',
    'n'  : '\x0a',
    'v'  : '\x0b',
    'f'  : '\x0c',
    'r'  : '\x0d',
    ' '  : '\x20',
    'e'  : '\x1b',
    'N'  : u'\u0085',
    'L'  : u'\u2028',
    'P'  : u'\u2029',
    '_'  : u'\u00a0'
}

escapeCodeChars = reduce(lambda x, y: x + y, escapeCodes.keys())

def escape_slash(match):
    escaped = match.groupdict()['esc']


    if escaped in ['\\', '"']:
        return escaped

    elif escaped in escapeCodes.keys():
        return escapeCodes[escaped] 

    elif escaped[0] == 'x':
        return chr(int(escaped[1:3], 16))

    elif escaped[0] == 'u':
        return unichr(int(escaped[1:5], 16))

    elif escaped[0] == 'U':
        return ( chr(int(escaped[1:3], base=16)) +
                         chr(int(escaped[3:5], base=16)) + 
                 chr(int(escaped[5:7], base=16)) + 
                         chr(int(escaped[7:9], base=16)) )
    else:
        raise YamlInvalidEscapeChar


escapeCodePat = "[%s]" % escapeCodeChars
whackPat = r"\\"
asciiPat = "x[\da-fA-F]{2}"
u16Pat = "u[\da-fA-F]{4}"
u32Pat = "U[\da-fA-F]{8}"

pat = re.compile(r"\\(?P<esc>%s|%s|%s|%s|%s|.)"%(escapeCodePat,
                    whackPat, asciiPat,
                    u16Pat, u32Pat))

upat = re.compile(r"\\(?P<esc>%s|%s)"%(u16Pat, u32Pat))

def escape(val):
    val = pat.sub(escape_slash, val)
    return val

def escape_unicode(val):
    val = upat.sub(escape_slash, val)
    return val