U
    \Sh0                     @  s  U d Z ddlmZ ddlZddl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mZ ddlmZmZmZmZmZ ddlmZmZ dd	lmZ dd
lmZ ddlmZ ddlmZ d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+ ddl,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4 e 5e6Z7e8dZ9de:d< e8dZ;de:d< ddddddZ<ddddddZ=G d d! d!e>Z?d[d#d$Z@d\d%d&ZAG d'd( d(e>ZBddd)d*d+d,ZCe8d-ZDG d.d/ d/ZEG d0d1 d1ZFdd2d3d4d5d6ZGd2ddd7d8d9d:ZHddd7d;d<d=ZId]dd)d>d?d@dAZJddBdCdDdEZKdddFdGdHZLddIdJdKdLZMdMdN ZNe"jOdOfejPdPfejQdQfejRdRfejSdSfejTdTfejUdTfejVdUfejWdVfeNdWfdX
ZXdYdZ ZYdS )^zUtility functions for Sphinx.    )annotationsN)import_module)path)IOAny)	parse_qsl
quote_plus	urlencodeurlsplit
urlunsplit)ExtensionErrorFiletypeNotFoundError)__)display)
exceptions)	http_date)logging)osutil)strip_colors)	patfilter)caption_ref_reexplicit_title_renested_parse_with_titlessplit_explicit_title)SEPcopyfile	copytimes	ensuredirmake_filenamemtimes_of_filesos_pathrelative_uriz\s+zre.Pattern[str]ws_rez(?P<schema>.+)://.*url_restr)basedocnamedocnamereturnc                 C  s    t t d|  d|dd  S )N/z..   )	posixpathnormpathjoin)r%   r&    r-   G/root/rtd-docs/venv/lib/python3.8/site-packages/sphinx/util/__init__.pydocname_join5   s
    r/   zdict[str, str])source_suffixfilenamer'   c                 C  s0   |   D ]\}}||r|p d  S qtd S )Nrestructuredtext)itemsendswithr   )r0   r1   suffixfiletyper-   r-   r.   get_filetype:   s    
r7   c                   @  sp   e Zd ZdZddddZdddddd	Zddd
ddZddddddZddddZdddddZ	dS )FilenameUniqDictz
    A dictionary that automatically generates unique names for its keys,
    interpreted as filenames, and keeps track of a set of docnames they
    appear in.  Used for images and downloadable files in the environment.
    Noner'   c                 C  s   t  | _d S N)set	_existingselfr-   r-   r.   __init__H   s    zFilenameUniqDict.__init__r$   )r&   newfiler'   c                 C  s   || kr&| | d  | | | d S t|}t|\}}d}|| jkrf|d7 }| | | }qB|h|f| |< | j | |S )Nr   r)   )addr   basenamesplitextr=   )r?   r&   rA   Z
uniquenamebaseextir-   r-   r.   add_fileK   s    

zFilenameUniqDict.add_filer&   r'   c                 C  s>   t |  D ],\}\}}|| |s| |= | j| qd S r;   )listr3   discardr=   )r?   r&   r1   docsuniquer-   r-   r.   	purge_docY   s
    
zFilenameUniqDict.purge_docset[str]dict[str, tuple[set[str], Any]]docnamesotherr'   c                 C  s8   |  D ]*\}\}}|t|@ D ]}| || q qd S r;   r3   r<   rH   )r?   rR   rS   r1   rL   _uniquedocr-   r-   r.   merge_other`   s    zFilenameUniqDict.merge_otherc                 C  s   | j S r;   r=   r>   r-   r-   r.   __getstate__e   s    zFilenameUniqDict.__getstate__)stater'   c                 C  s
   || _ d S r;   rX   )r?   rZ   r-   r-   r.   __setstate__h   s    zFilenameUniqDict.__setstate__N)
__name__
__module____qualname____doc__r@   rH   rN   rW   rY   r[   r-   r-   r-   r.   r8   B   s   r8       c                 K  s.   t jdd dkr tj| ddS tj| f|S )z\Wrapper around hashlib.md5

    Attempt call with 'usedforsecurity=False' if supported.
    N         FZusedforsecurity)sysversion_infohashlibmd5datakwargsr-   r-   r.   ri   l   s    ri   c                 K  s.   t jdd dkr tj| ddS tj| f|S )z]Wrapper around hashlib.sha1

    Attempt call with 'usedforsecurity=False' if supported.
    Nra   rb   Fre   )rf   rg   rh   sha1rj   r-   r-   r.   rm   w   s    rm   c                   @  sD   e Zd ZdZddddddZddddd	Zd
dddddZdS )DownloadFileszA special dictionary for download files.

    .. important:: This class would be refactored in nearly future.
                   Hence don't hack this directly.
    r$   )r&   r1   r'   c                 C  sZ   || kr<t |  }| dtj| }t |f| |< | | d | | | d S )Nr(   r   r)   )ri   encode	hexdigestosr   rC   r<   rB   )r?   r&   r1   digestdestr-   r-   r.   rH      s    zDownloadFiles.add_filer9   rI   c                 C  s2   t |  D ] \}\}}|| |s| |= qd S r;   )rJ   r3   rK   )r?   r&   r1   rL   _destr-   r-   r.   rN      s    
zDownloadFiles.purge_docrO   rP   rQ   c                 C  s8   |  D ]*\}\}}|t|@ D ]}| || q qd S r;   rT   )r?   rR   rS   r1   rL   rt   r&   r-   r-   r.   rW      s    zDownloadFiles.merge_otherN)r\   r]   r^   r_   rH   rN   rW   r-   r-   r-   r.   rn      s   	rn   z
str | None)modname	attributer'   c                 C  sB   | d krd S t | }|}|dD ]}|r"t||}q"t|dd S )N.r]   )r   splitgetattr)ru   rv   modulevalueattrr-   r-   r.   get_full_modname   s    r}   zcoding[:=]\s*([-\w.]+)c                   @  s0   e Zd ZdZdddddZddd	d
dZdS )UnicodeDecodeErrorHandlerz8Custom error handler for open() that warns and replaces.r$   r9   rI   c                 C  s
   || _ d S r;   )r&   )r?   r&   r-   r-   r.   r@      s    z"UnicodeDecodeErrorHandler.__init__UnicodeDecodeErrorztuple[str, int])errorr'   c                 C  s   |j dd|j}|j d|j}|dkr4t|j }|j dd|jd }tjtd|j |d |j d |j |j|j	  d |j |j	|  | j
|fd d	|j	fS )
N   
r   r)   z5undecodable source characters, replacing with "?": %rs   >>>s   <<<)location?)objectrfindstartfindlencountloggerwarningr   endr&   )r?   r   Z	linestartZlineendlinenor-   r-   r.   __call__   s     

z"UnicodeDecodeErrorHandler.__call__N)r\   r]   r^   r_   r@   r   r-   r-   r-   r.   r~      s   r~   c                   @  s@   e Zd ZdZddddddZdddd	d
ZddddZdS )Teez2
    File-like object writing to two streams.
    r   r9   )stream1stream2r'   c                 C  s   || _ || _d S r;   )r   r   )r?   r   r   r-   r-   r.   r@      s    zTee.__init__r$   )textr'   c                 C  s   | j | | j| d S r;   )r   writer   )r?   r   r-   r-   r.   r      s    z	Tee.writer:   c                 C  s0   t | jdr| j  t | jdr,| j  d S )Nflush)hasattrr   r   r   r>   r-   r-   r.   r      s    
z	Tee.flushN)r\   r]   r^   r_   r@   r   r   r-   r-   r-   r.   r      s   r   intz	list[int])spectotalr'   c           	      C  s   g }|  d}|D ]}z|  d}ddg|kr6tt|dkrZ|t|d d  nXt|dkrt|d prd}t|d pt||}||krt|t|d | ntW q t	k
r } ztd|  |W 5 d}~X Y qX q|S )	z_Parse a line number spec (such as "1,2,4-6") and return a list of
    wanted line numbers.
    ,- r)   r   ra   zinvalid line number spec: %rN)
rx   strip
ValueErrorr   appendr   maxextendrange	Exception)	r   r   r3   partspartZbegendr   r   excr-   r-   r.   parselinenos   s&    
"r   z	list[str])ntyper{   r'   c                 C  sH   dd | d| d D }tttd|| k rDtd| d||S )z@Split an index entry into a given number of parts at semicolons.c                 S  s   g | ]}|  qS r-   )r   ).0xr-   r-   r.   
<listcomp>   s     zsplit_into.<locals>.<listcomp>;r)   Ninvalid  index entry )rx   r   rJ   filterr   )r   r   r{   r   r-   r-   r.   
split_into   s    r   )
entry_typer{   r'   c                 C  s   | dkr8zt dd|W S  tk
r6   t dd| Y S X | dkrLt dd|S | dkr`t dd|S | dkrtt dd|S td	|  d
|d S )NZsinglera   r)   pairZtriplerc   >   seeseealsor   r   r   )r   r   )r   r{   r-   r-   r.   split_index_msg   s    r   r   )objnamesourcer'   c              
   C  s   zd|  d}|d}t|}|D ]<}|d| 7 }zt||}W q" tk
r\   t|}Y q"X q"|W S  ttfk
r } z,|rtd| |f ||td|  ||W 5 d}~X Y nX dS )z!Import python object by qualname.rw   r   z#Could not import %s (needed for %s)zCould not import %sN)rx   popr   ry   AttributeErrorImportErrorr   )r   r   Zobjpathru   objnamer   r-   r-   r.   import_object  s*    

r   ztuple[str | None, str])r   r'   c              
   C  s   |  d}t|dD ]\}}zd|d| }t| W q tk
r   |d|d  rd|d|d  d||d d f Y   S dd|f Y   S Y q tk
r   Y qX q| dfS )a  Split full qualified name to a pair of modname and qualname.

    A qualname is an abbreviation for "Qualified name" introduced at PEP-3155
    (https://peps.python.org/pep-3155/).  It is a dotted path name
    from the module top-level.

    A "full" qualified name means a string containing both module name and
    qualified name.

    .. note:: This function actually imports the module to check its existence.
              Therefore you need to mock 3rd party modules if needed before
              calling this function.
    rw   r)   Nr   )rx   	enumerater,   r   r   
IndexError)r   r   rG   Z_partru   r-   r-   r.   split_full_qualified_name$  s    
4r   )urir'   c                 C  sd   t t| }|d dd|d< t|d  d|d< dd t|d D }t||d< t|S )	Nr)   idnaasciira   r(   c                 S  s   g | ]\}}||  fqS r-   )ro   )r   qvr-   r-   r.   r   F  s     zencode_uri.<locals>.<listcomp>rc   )rJ   r
   ro   decoder   r   r	   r   )r   rx   queryr-   r-   r.   
encode_uriB  s    r   bool)urlr'   c                 C  s   t | od| kS )zCheck *url* is URL or not.z://)r   )r   r-   r-   r.   isurlK  s    r   c                  C  s   ddl m}  | S )Nr   _XML_NAME_PATTERN)Zsphinx.builders.epub3r   r   r-   r-   r.   _xml_name_checkerP  s    r   z!sphinx.util.osutil.path_stabilizez!sphinx.util.display.display_chunkz#sphinx.util.display.status_iteratorz'sphinx.util.display.SkipProgressMessagez!sphinx.http_date.epoch_to_rfc1123z!sphinx.http_date.rfc1123_to_epochz sphinx.exceptions.save_tracebackz-sphinx.exceptions.format_exception_cut_framesz'sphinx.builders.epub3._XML_NAME_PATTERN)
path_stabilizedisplay_chunkstatus_iteratorSkipProgressMessageprogress_messageepoch_to_rfc1123rfc1123_to_epochsave_tracebackformat_exception_cut_framesZxmlname_checkerc                 C  sH   | t krtdtd| ddlm} t |  \}}|t| |dd |S )Nzmodule z has no attribute r   )_deprecation_warning)rd   r   )remove)_DEPRECATED_OBJECTSr   r\   Zsphinx.deprecationr   )r   r   Zdeprecated_objectcanonical_namer-   r-   r.   __getattr__h  s    r   )r`   )r`   )N)Zr_   
__future__r   rh   rq   r*   rerf   	importlibr   r   typingr   r   urllib.parser   r   r	   r
   r   Zsphinx.errorsr   r   Zsphinx.localer   Zsphinx.utilr   _displayr   _exceptionsr   Z
_http_dater   r   Z_osutilZsphinx.util.consoler   Zsphinx.util.matchingr   Zsphinx.util.nodesr   r   r   r   Zsphinx.util.osutilr   r   r   r   r   r   r    r!   	getLoggerr\   r   compiler"   __annotations__r#   r/   r7   dictr8   ri   rm   rn   r}   Z
_coding_rer~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r-   r-   r-   r.   <module>   st   	(
*


		