(component, allowed_chars, encoding='utf-8')
return encoded_component.decode(encoding)
Percent-encodes a URI component without reapplying
    onto an already percent-encoded component. Based on
    rfc3986.normalizers.encode_component()
Percent-encodes a URI component without reapplying
    onto an already percent-encoded component. Based on
    rfc3986.normalizers.encode_component()
Percent - encodes a URI component without reapplying onto an already percent - encoded component . Based on rfc3986 . normalizers . encode_component ()
def _encode_invalid_chars(component, allowed_chars, encoding='utf-8'):
    """Percent-encodes a URI component without reapplying
    onto an already percent-encoded component. Based on
    rfc3986.normalizers.encode_component()
    """
    if component is None:
        return component

    # Try to see if the component we're encoding is already percent-encoded
    # so we can skip all '%' characters but still encode all others.
    percent_encodings = len(normalizers.PERCENT_MATCHER.findall(
                            compat.to_str(component, encoding)))

    uri_bytes = component.encode('utf-8', 'surrogatepass')
    is_percent_encoded = percent_encodings == uri_bytes.count(b'%')

    encoded_component = bytearray()

    for i in range(0, len(uri_bytes)):
        # Will return a single character bytestring on both Python 2 & 3
        byte = uri_bytes[i:i+1]
        byte_ord = ord(byte)
        if ((is_percent_encoded and byte == b'%')
                or (byte_ord < 128 and byte.decode() in allowed_chars)):
            encoded_component.extend(byte)
            continue
        encoded_component.extend('%{0:02x}'.format(byte_ord).encode().upper())

    return encoded_component.decode(encoding)
def _encode_invalid_chars ( component , allowed_chars , encoding = 'utf-8' ) : if component is None : return component # Try to see if the component we're encoding is already percent-encoded # so we can skip all '%' characters but still encode all others. percent_encodings = len ( normalizers . PERCENT_MATCHER . findall ( compat . to_str ( component , encoding ) ) ) uri_bytes = component . encode ( 'utf-8' , 'surrogatepass' ) is_percent_encoded = percent_encodings == uri_bytes . count ( b'%' ) encoded_component = bytearray ( ) for i in range ( 0 , len ( uri_bytes ) ) : # Will return a single character bytestring on both Python 2 & 3 byte = uri_bytes [ i : i + 1 ] byte_ord = ord ( byte ) if ( ( is_percent_encoded and byte == b'%' ) or ( byte_ord < 128 and byte . decode ( ) in allowed_chars ) ) : encoded_component . extend ( byte ) continue encoded_component . extend ( '%{0:02x}' . format ( byte_ord ) . encode ( ) . upper ( ) ) return encoded_component . decode ( encoding )