o
    Ni8                     @  s`  d Z ddlmZ ddlZddlZddlZddlmZ ddl	m
Z
 ddlmZ ddlmZmZ ddlmZ dd	lmZ dd
lmZmZmZ ddlmZ ddlmZ ddlmZ erddlmZ ddl m!Z! ddl"m#Z# ddl$m%Z% ddlm&Z&m'Z'm(Z(m)Z)m*Z* ddl+m,Z,m-Z- d<ddZ.d=ddZ/d>d'd(Z0d?d.d/Z1d@d2d3Z2dAd5d6Z3dAd7d8Z4dBd:d;Z5dS )CzBThis module contains the code for loading intersphinx inventories.    )annotationsN)
itemgetter)path)TYPE_CHECKING)urlsplit
urlunsplit)INVENTORY_FILENAME)ConfigError)LOGGERInventoryAdapter_IntersphinxProject)__)requests)InventoryFile)Path)HTTPResponse)Sphinx)Config)IntersphinxMappingInventoryCacheEntryInventoryLocationInventoryNameInventoryURI)	Inventory_ReadableStreamappr   configr   returnNonec                 C  s  i }d}|j   D ]\}}t|tr|s)|d7 }td}t|| |j |= qt|tt	B sD|d7 }td}t||| |j |= qz|\}}W n t
ttfyi   |d7 }td}t||| |j |= Y qw |rqt|ts|d7 }td}t||| |j |= q||v r|d7 }td}t|||||  |j |= q|||< t|tt	B s|f}g }	|D ]&}
|
du s|
rt|
tr|	|
 q|d7 }td	}t||
| |j |= q||t|	ff|j |< q|dkrtd
}t||dkrtd}t|| dS )ah  Validate and normalise :confval:`intersphinx_mapping`.

    Ensure that:

    * Keys are non-empty strings.
    * Values are two-element tuples or lists.
    * The first element of each value pair (the target URI)
      is a non-empty string.
    * The second element of each value pair (inventory locations)
      is a tuple of non-empty strings or None.
    r      zrInvalid intersphinx project identifier `%r` in intersphinx_mapping. Project identifiers must be non-empty strings.zTInvalid value `%r` in intersphinx_mapping[%r]. Expected a two-element tuple or list.zgInvalid value `%r` in intersphinx_mapping[%r]. Values must be a (target URI, inventory locations) pair.zjInvalid target URI value `%r` in intersphinx_mapping[%r][0]. Target URIs must be unique non-empty strings.zInvalid target URI value `%r` in intersphinx_mapping[%r][0]. Target URIs must be unique (other instance in intersphinx_mapping[%r]).Nz{Invalid inventory location value `%r` in intersphinx_mapping[%r][1]. Inventory locations must be non-empty strings or None.z6Invalid `intersphinx_mapping` configuration (1 error).z8Invalid `intersphinx_mapping` configuration (%s errors).)intersphinx_mappingcopyitems
isinstancestrr   r
   errortuplelist	TypeError
ValueError	Exceptionappendr	   )r   r   seenerrorsnamevaluemsguriinvtargetstarget r5   P/home/ubuntu/.local/lib/python3.10/site-packages/sphinx/ext/intersphinx/_load.pyvalidate_intersphinx_mapping%   s   r7   c                   s  t t t jj}|j jj}g }| D ]*\}\}}z	t	|||d}W n t
y= } z	td}	t|	|d}~ww || qdd |D }
tD ]}| d |vr]|= qO||
vrd|= qOtj  fdd|D }d	d tj|D }W d   n1 sw   Y  t|r|  tdd
}t |d}|D ]\}}}||j|< | D ]\}}|j|i | qqdS dS )zuLoad all intersphinx mappings into the environment.

    The intersphinx mappings are expected to be normalized.
    )r.   
target_uri	locationszCAn invalid intersphinx_mapping entry was added after normalisation.Nc                 S  s   h | ]}|j qS r5   )r8   .0projectr5   r5   r6   	<setcomp>   s    z load_mappings.<locals>.<setcomp>r   c              
     s&   g | ]}j t| j jd qS ))r<   cachenowr   srcdir)submit_fetch_inventory_groupr   r@   r:   r   intersphinx_cacher?   poolr5   r6   
<listcomp>   s    	z!load_mappings.<locals>.<listcomp>c                 S  s   g | ]}|  qS r5   )resultr;   fr5   r5   r6   rF      s    r   )key)inttimer   builderenvr>   r   r    valuesr   r)   r   r	   r+   	frozenset
concurrentfuturesThreadPoolExecutoras_completedanyclearr   sortednamed_inventoryr"   main_inventory
setdefaultupdate)r   inventoriesr    projectsr.   r1   r9   r<   errr0   expected_urisrR   updatedby_name_and_timecache_values_expiryinvdataobjtypeobjectsr5   rC   r6   load_mappings   sT   

	

rg   r<   r   r>   'dict[InventoryURI, InventoryCacheEntry]r?   rK   r@   r   boolc                 C  s`  |j dkr||j d  }nd}d}g }| jD ]a}|d u r$t| jt}	n|}	d|	vs8| j|vs8|| j d |k rwttd| j	t
|	 zt| j|	||d}
W n tyg } z||j W Y d }~qd }~ww |
rw| j	||
f|| j< d} nq|s}	 |S t|t| jk rttd	 |D ]}tj|  q|S d
dd |D }ttdd
 |  |S )Nr   iQ F://r   z.loading intersphinx inventory '%s' from %s ...r8   inv_locationr   r@   TzXencountered some issues with some of the inventories, but they had working alternatives:
c                 s  s$    | ]}|d  |dd  V  qdS )r   r   Nr5   rH   r5   r5   r6   	<genexpr>  s   " z)_fetch_inventory_group.<locals>.<genexpr>zAfailed to reach any of the inventories with the following issues:)intersphinx_cache_limitr9   	posixpathjoinr8   r   r
   infor   r.   _get_safe_url_fetch_inventoryr*   r+   argslenwarning)r<   r>   r?   r   r@   
cache_timer`   failureslocationr2   rd   r^   failissuesr5   r5   r6   rB      sn   



rB   r1   r   r2   r$   r   c                 C  s   t ||| j| jdS )6Fetch, parse and return an intersphinx inventory file.rk   )rt   r   r@   )r   r1   r2   r5   r5   r6   fetch_inventory  s   r~   r8   rl   c           
      C  sd  d| v rt | } zd|v rt||d}n	tt||d}W n ty5 } zd||jt|f|_ d}~ww zct	|drb|j
}||krbtd}t||| | |t|t|d hv rbt|} |+ z
t|| tj}W n ty }	 z
d	|	}t||	d}	~	ww W d   W |S 1 sw   Y  W |S  ty } zd
||jjt|f|_ d}~ww )r}   rj   )r   rbz4intersphinx inventory %r not fetchable due to %s: %sNurlz)intersphinx inventory has moved: %s -> %s/z*unknown or unsupported inventory version: z3intersphinx inventory %r not readable due to %s: %s)_strip_basic_auth_read_from_urlopenr   rq   r*   	__class__r$   ru   hasattrr   r   r
   rr   dirnamer   loadrp   r)   __name__)
r8   rl   r   r@   rI   r^   new_inv_locationr0   rd   excr5   r5   r6   rt   $  sd   



rt   r   c                 C  sh   t | }|jdu r| S t|}|jr%|j d|j d|j |d< t|S |j d|j |d< t|S )a5  Gets version of *url* with basic auth passwords obscured. This function
    returns results suitable for printing and logging.

    E.g.: https://user:12345@example.com => https://user@example.com

    :param url: a url
    :type url: ``str``

    :return: *url* with password removed
    :rtype: ``str``
    N@:r   )r   usernamer'   porthostnamer   )r   partsfragsr5   r5   r6   rs   Z  s   
rs   c                 C  s6   t t| }d|d v r|d dd |d< t|S )a  Returns *url* with basic auth credentials removed. Also returns the
    basic auth username and password if they're present in *url*.

    E.g.: https://user:pass@example.com => https://example.com

    *url* need not include basic auth credentials.

    :param url: url which may or may not contain basic auth credentials
    :type url: ``str``

    :return: *url* with any basic auth creds removed
    :rtype: ``str``
    r   r   )r'   r   splitr   )r   r   r5   r5   r6   r   s  s   r   r   c                C  s@   t j| d|j|j|j|jfd}|  |j|j_d|j_	|jS )u  Reads data from *url* with an HTTP *GET*.

    This function supports fetching from resources which use basic HTTP auth as
    laid out by RFC1738 § 3.1. See § 5 for grammar definitions for URLs.

    .. seealso:

       https://www.ietf.org/rfc/rfc1738.txt

    :param url: URL of an HTTP resource
    :type url: ``str``

    :return: data read from resource described by *url*
    :rtype: ``file``-like object
    T)streamtimeout_user_agent	_tls_info)
r   getintersphinx_timeout
user_agent
tls_verifytls_cacertsraise_for_statusr   rawdecode_content)r   r   rr5   r5   r6   r     s   

r   )r   r   r   r   r   r   )r   r   r   r   )r<   r   r>   rh   r?   rK   r   r   r@   r   r   ri   )r   r   r1   r   r2   r$   r   r   )
r8   r   rl   r$   r   r   r@   r   r   r   )r   r$   r   r$   )r   r$   r   r   r   r   )6__doc__
__future__r   concurrent.futuresrQ   rp   rL   operatorr   osr   typingr   urllib.parser   r   sphinx.builders.htmlr   sphinx.errorsr	   sphinx.ext.intersphinx._sharedr
   r   r   sphinx.localer   sphinx.utilr   sphinx.util.inventoryr   pathlibr   urllib3.responser   sphinx.applicationr   sphinx.configr   r   r   r   r   r   sphinx.util.typingr   r   r7   rg   rB   r~   rt   rs   r   r   r5   r5   r5   r6   <module>   s<    

e
C
M


6
