Ejemplo n.º 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)
Ejemplo n.º 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
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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)
Ejemplo n.º 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
Ejemplo n.º 6
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
Ejemplo n.º 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
Ejemplo n.º 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
Ejemplo n.º 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)
Ejemplo n.º 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)