Percent/Query encode a raw URI tree.

def uri_tree_encode(uri_tree, type_host = HOST_REG_NAME):
    """
    Percent/Query encode a raw URI tree.
    """
    scheme, authority, path, query, fragment = uri_tree
    if authority:
        user, passwd, host, port = authority
        if user:
            user = pct_encode(user, USER_ENCDCT)
        if passwd:
            passwd = pct_encode(passwd, PASSWD_ENCDCT)
        if host and type_host == HOST_REG_NAME:
            host = pct_encode(host, REG_NAME_ENCDCT)
        if isinstance(port, (int, long)):
            port = str(port)
        authority = (user, passwd, host, port)
    if path:
        path = pct_encode(path, P_ENCDCT)
        if (not authority) and (not scheme):
            # check for path-noscheme special case
            sppath = path.split('/', 1)
            if ':' in sppath[0]:
                sppath[0] = sppath[0].replace(':', '%3A')
                path = '/'.join(sppath)
    if query:
        query = tuple([(query_elt_encode(x, QUERY_KEY_ENCDCT),
                        query_elt_encode(y, QUERY_VAL_ENCDCT)) for (x, y) in query])
    if fragment:
        fragment = pct_encode(fragment, FRAG_ENCDCT)
    return (scheme, authority, path, query, fragment)