示例#1
0
def convert_to_unicode(charset, value):
    #in case of unicode we have nothing to do
    if isinstance(value, unicode):
        return value

    charset = _translate_charset(charset)
    return to_unicode(value, charset=charset)
示例#2
0
def convert_to_unicode(charset, value):
    if isinstance(value, six.text_type):
        return value

    charset = _ensure_charset(charset)
    value = to_unicode(value, charset)
    return value
示例#3
0
文件: charsets.py 项目: plq/flanker
def convert_to_unicode(charset, value):
    if isinstance(value, six.text_type):
        if six.PY2:
            return value

        value = value.encode('ascii')

    charset = _ensure_charset(charset)
    value = to_unicode(value, charset)
    return value
示例#4
0
文件: parsing.py 项目: nylas/flanker
def parse_header_value(name, val):
    if parametrized.is_parametrized(name, val):
        val, params = parametrized.decode(val)
        if name == 'Content-Type':
            main, sub = parametrized.fix_content_type(val)
            return ContentType(main, sub, params)
        else:
            return WithParams(val, params)
    else:
        return val if is_pure_ascii(val) else to_unicode(val)
示例#5
0
def parse_header_value(name, val):
    if not is_pure_ascii(val):
        val = to_unicode(val)
    if parametrized.is_parametrized(name, val):
        val, params = parametrized.decode(val)
        if val is not None and not is_pure_ascii(val):
            raise DecodingError('Non-ascii content header value')
        if name == 'Content-Type':
            main, sub = parametrized.fix_content_type(val)
            return ContentType(main, sub, params)

        return WithParams(val, params)

    return val
示例#6
0
文件: parsing.py 项目: plq/flanker
def parse_header_value(name, val):
    if not is_pure_ascii(val):
        val = to_unicode(val)
    if parametrized.is_parametrized(name, val):
        val, params = parametrized.decode(val)
        if val is not None and not is_pure_ascii(val):
            raise DecodingError('Non-ascii content header value')
        if name == 'Content-Type':
            main, sub = parametrized.fix_content_type(val)
            return ContentType(main, sub, params)

        return WithParams(val, params)

    return val
示例#7
0
def parse_header_value(name, val):
    if not is_pure_ascii(val):
        if parametrized.is_parametrized(name, val):
            raise DecodingError("Unsupported value in content- header")
        return to_unicode(val)
    else:
        if parametrized.is_parametrized(name, val):
            val, params = parametrized.decode(val)
            if name == 'Content-Type':
                main, sub = parametrized.fix_content_type(val)
                return ContentType(main, sub, params)
            else:
                return WithParams(val, params)
        else:
            return val
示例#8
0
def parse_header_value(name, val):
    if not is_pure_ascii(val):
        if parametrized.is_parametrized(name, val):
            raise DecodingError("Unsupported value in content- header")
        return to_unicode(val)
    else:
        if parametrized.is_parametrized(name, val):
            val, params = parametrized.decode(val)
            if name == 'Content-Type':
                main, sub = parametrized.fix_content_type(val)
                return ContentType(main, sub, params)
            else:
                return WithParams(val, params)
        else:
            return val
示例#9
0
def scan(string):
    """Scanner that uses 1 pass to scan the entire message and
    build a message tree"""

    if six.PY2:
        if not isinstance(string, six.binary_type):
            raise DecodingError('Scanner works with binary only')
    else:
        if isinstance(string, six.binary_type):
            string = to_unicode(string)

        if not isinstance(string, six.text_type):
            raise DecodingError('Cannot scan type %s' % type(string))

    tokens = tokenize(string)
    if not tokens:
        tokens = [default_content_type()]
    try:
        return traverse(Start(), TokensIterator(tokens, string))
    except DecodingError:
        raise
    except Exception as cause:
        raise six.raise_from(DecodingError("Malformed MIME message"), cause)
示例#10
0
def scan(string):
    """Scanner that uses 1 pass to scan the entire message and
    build a message tree"""

    if six.PY2:
        if not isinstance(string, six.binary_type):
            raise DecodingError('Scanner works with binary only')
    else:
        if isinstance(string, six.binary_type):
            string = to_unicode(string)

        if not isinstance(string, six.text_type):
            raise DecodingError('Cannot scan type %s' % type(string))

    tokens = tokenize(string)
    if not tokens:
        tokens = [default_content_type()]
    try:
        return traverse(Start(), TokensIterator(tokens, string))
    except DecodingError:
        raise
    except Exception as cause:
        raise six.raise_from(DecodingError("Malformed MIME message"), cause)