
    -PhaJ                     8   d Z ddl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 ddlmZ ddlmZmZ ddlZddlmZ ddlmZ dd	lmZmZmZ ej        j                             d
          Z!d Z"d Z#d Z$ G d d          Z%ddZ&d Z'd Z(d Z)d Z*dS )zLink resolver objects.    N)BytesIO)Path)	HTTPErrorURLError)ExtensionError)js_index   )_W_KW
_read_jsonstatus_iteratorzsphinx-galleryc                 H   t          j        |           j        dv rd}d|i}t          j        | d|          }t          j        |          }|j                            dd          }|                                }|dk    r5t          j
        t          |                                                    }n|dk    rt          d	|          |                    d
          }n>t          | dd
          5 }|                                }ddd           n# 1 swxY w Y   |S )z+Get data over http(s) or from a local file.)httphttpszhMozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11z
User-AgentNzcontent-encodingplaingzip)fileobjzunknown encoding utf-8r)modeencoding)urllib_parseurlparseschemeurllib_requestRequesturlopenheadersgetreadr   GzipFiler   r   decodeopen)url
user_agentr   reqrespr   datafids           Z/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/sphinx_gallery/docs_resolv.py	_get_datar*      sU   S!!(,===
,$S$88%c**<##$6@@yy{{v=777<<>>DD   !AX!A!ABBB{{7###C'222 	c88::D	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 Ks   6DDDc                     t           j                            |d          }t          j        |          }| |v r	||          }nt          |           }||| <   |                                 |S )z;Persistent dictionary usage to retrieve the search indexes.searchindex)ospathjoinshelver"   r*   close)r#   gallery_dircached_filesearch_indexr'   s        r)   get_datar5   /   si    ',,{M::K;{++L
lC ~~ SK    c                 >   |                      d          }|dk     r|                      d          }|dk     rt          d          |                      d|          }|dk     rt          d          |                      d|          }|dk     rt          d          | |dz   |                                         }i }|                                D ]}|                    dd          \  }}|                                                    d	          }|                                }|d
         dk    r|dd
                                         }|d         dv r|dd
         }n3|dk    rd}n*|dk    rd}n!	 t          |          }n# t          $ r Y w xY w|||<   |S )zParse the Sphinx index for documentation options.

    Parameters
    ----------
    index : str
        The Sphinx index page

    Returns
    -------
    docopts : dict
        The documentation options from the page.
    var DOCUMENTATION_OPTIONSr   zconst DOCUMENTATION_OPTIONSz2Documentation options could not be found in index.{z};r	   :",Nz"'falseFtrueT)findr   strip
splitlinessplitrstripint
ValueError)indexposendposblockdocoptslinekeyvalues           r)   parse_sphinx_docoptsrO   =   s    **0
1
1C
Qwwjj677
QwwQRRR
**S#

C
QwwQRRRZZc""FzzQRRR#'F"#))++EG  ""  ZZQ''
Uiikk$$9#2#J%%''E8u!B$KEEgEEf__EEE

   
  Ns   7F
FFc                   0    e Zd ZdZddZd ZddZddZdS )	SphinxDocLinkResolvera  Resolve documentation links using searchindex.js generated by Sphinx.

    Parameters
    ----------
    doc_url : str
        The base URL of the project website.
    relative : bool
        Return relative links (only useful for links to documentation of this
        package).
    Fc                    || _         || _        || _        || _        i | _        t          |t                    rbt          j        	                    |d          }t          j        	                    |d          }t          j        	                    |dd          }n |rt          d          |dz   }|dz   }|dz   }t          j                                        d	k    r.t          |t                    r|st          d
          d| _        nd| _        t          ||          }d|v rt          |          | _        n$t          ||          }	t          |	          | _        t          ||          }
t#          j        |
          | _        d S )Nz
index.htmlzsearchindex.js_staticzdocumentation_options.jszMRelative links are only supported for local URLs (doc_url cannot be absolute)/z/searchindex.jsz!/_static/documentation_options.jsntzHYou have to use relative=True for the local package on a Windows system.TFr8   )configdoc_urlr2   relative_link_cache
isinstancer   r-   r.   r/   r   namelower_is_windowsr5   rO   _docoptsr   loads_searchindex)selfrV   rW   r2   rX   	index_urlsearchindex_urldocopts_urlrG   rK   sindexs              r)   __init__zSphinxDocLinkResolver.__init__   s   & gt$$ 	HWl;;I gll74DEEO',,w	;UVVKK $8    #I%(99O!$GGK 7==??d""z'4'@'@" $4    $D$D K00&%//077DMM{K88G099DM /;77$N622r6   c                 N   	 | j         d         |         }t          |t                    r	 ||         }nq# t          $ r Y d S w xY wt          |t          t
          f          r2	 |D ]}|d         |k    r|d d         } nd S n## t          $ r Y d S w xY wn# t          $ r Y d S w xY w|S )Nobjects   )r`   rZ   dictKeyErrorlisttuple	Exception)ra   firstsecondmatchitems        r)   _get_index_matchz&SphinxDocLinkResolver._get_index_match   s   	 %i07E %&&   !&MEE      44 ED%=11 	   % $ $7f,,$(!HE!E -  $t " !      44 	   	 	 	44	$ s2   B 4 
AA"B 
BB
B"!B"c                    |rdnd}||         |d         }}|                      ||          }|Pd|v rL|                    dd          \  }}d                    ||         |g          }|                      ||          }|dx}}n8|d         }	t          |d                   }
|d         }| j        d	         |
         }| j        d
         |	         }| j                            dd          }t          j        	                    |          d         |z   }| j
        r<|                    dd          }t          j                            | j        |          }nt          j        | j        |          }d                    ||g          }|dk    r|}n%|dk    r| j        d         |
         d         dz   |z   }|dz   |z   }||fS )z/Get a valid link and type_, False if not found.modulemodule_shortr[   N.r	   r      objtypes	filenamesFILE_SUFFIXz	.rst.htmlrT   \ -objnames#)rs   rC   r/   strr`   r^   r   r-   r.   splitextr]   replacerW   	posixpath)ra   cobjuse_full_modulemodule_typero   rp   rq   linktype_	fname_idxobjname_idxanchorfnameextfullnames                  r)   _get_link_typez$SphinxDocLinkResolver._get_link_type   s   "1Ehh~[)4<v%%eV44=SF]]"LLa00ME6HHd;/788E))%88E=D55aIeAh--K1XF%j1+>E%k29=E -##M;??CG$$U++A.4E ;c400w||DL%88 ~dlE::xx00H||!3*:6{CAFLxW#:&DU{r6   c                    |d         dz   |d         z   }|| j         vr[d}| j        d         D ]-}t          j        ||d         dz   |d         z             rd} n.|                     ||          | j         |<   | j         |         \  }}| j        rJ|Ht          j                            ||	          }| j	        r|
                    d
d          }|dd         }|r||fn|S )a  Resolve the link to the documentation, returns None if not found.

        Parameters
        ----------
        cobj : Dict[str, Any]
            Dict with information about the "code object" for which we are
            resolving a link.

                - cobj['name'] : function or class name (str)
                - cobj['module'] : module name (str)
                - cobj['module_short'] : shortened module name (str)
                - cobj['is_class'] : whether object is class (bool)
                - cobj['is_explicit'] : whether object is an explicit
                  backreference (referred to by sphinx markup) (bool)
        this_url: str
            URL of the current page. Needed to construct relative URLs
            (only used if relative=True in constructor).
        return_type : bool
            If True, return the type as well.

        Returns
        -------
        link : str or None
            The link (URL) to the documentation.
        type_ : str
            The type. Only returned if return_type is True.
        rv   rw   r[   Fprefer_full_moduleru   TN)startr|   rT   rx   )rY   rV   researchr   rX   r-   r.   relpathr]   r   )	ra   r   this_urlreturn_type	full_namer   patternr   r   s	            r)   resolvezSphinxDocLinkResolver.resolve   s   8 (3.f=	D,,,#O;';<  9Wd8ns&:T&\&IJJ &*OE +/*=*=dO*T*TDY'&y1e= 	T-7??4x?88D /||D#.. 8D +5e}}5r6   N)F)__name__
__module____qualname____doc__rf   rs   r   r    r6   r)   rQ   rQ   s   sk        	 	03 03 03 03d  .$ $ $ $L06 06 06 06 06 06r6   rQ   fetchingc                 .   t          | t                    r| d| j         d| j         d| j         d}n!t          | t
                    r| d| j         }t                              d	                    t          |           j        |                     d S )N z: z ()z The following {} has occurred {})rZ   r   r#   codemsgr   reasonloggerwarningformattyper   )er   	error_msgs      r)   _handle_http_url_errorr   !  s    !Y )88QU88af88888			Ax	 	  )((ah((	
NN*11$q''2BINN    r6   c                 <    dD ]}|                      |d          } | S )Nz~!@$%^&*()+=,./';:"?><[]\{}|`#r~   )r   )sxs     r)   _sanitize_css_classr   +  s+    /  IIaHr6   c                 f   t          | j        dd          x}r|S t          | j        dt                                }|                                }|                                D ]?\  }}d |                    dt                                D             }||hz
  D ]}|||<   @|| j        _        |S )a  
    Get the mapping between module names and intersphinx inventories.

    In some cases, intersphinx inventories may provide documentation for modules _other_
    than the one given by the intersphinx_mapping key (e.g., the Python inventory
    contains documentation for all standard library modules, or Matplotlib contains
    several `mpl_toolkits`` modules), so this checks py:module for all inventories and
    adds that additional module mapping.
    sg_intersphinx_inventoryNintersphinx_named_inventoryc                 D    h | ]}|                     d           d         S )rw   r   )rC   ).0qualnames     r)   	<setcomp>z-_get_intersphinx_inventory.<locals>.<setcomp>C  s6     
 
 
'/HNN3"
 
 
r6   z	py:module)getattrenvrj   copyitemsr   r   )app	inventoryintersphinx_inv_origintersphinx_invmodule_namedocumented_modulesother_module_names          r)   _get_intersphinx_inventoryr   1  s     CG%?FFFy  #37,I466RR*//11O"6"<"<">"> ; ;Y
 
3<==dff3U3U
 
 
 "4{m!C 	; 	;1:O-..	; (7CG$r6   c                 	  +, i }t           j                            | j        j        |          }|d                                         D ]\  }}	 |8t          | j        j        t          | j        j
                  |d          ||<   nt          | j        j        ||          ||<   _# t          t          f$ r}t          |           Y d}~d}~ww xY wt           j                            t           j                            | j        j
        |                    }d}	d+d}
d t          j        |          D             }t!          |d	| d
dt#          |          d           }t%          |           }|D ]j\  }}t           j                            |||          }|t#          |          dz   d         }t           j                            |||dd         dz             }t           j                            |          st)          |          }i ,t+          |          D ]}||         }dx}}|D ]}|d         |d         fD ]}|                    d          d         }|d         }||v rX	 ||                             ||d          \  }}n5# t          t          f$ r!}t          |d| d|            Y d}~nd}~ww xY w||cxk    rdk    rn nd}|a||v r]||         }|dk    r|}n| d| }|                                D ]0\  }}|                    d          r||v r||         d         }|} n1|duod|v o|d          }||
                    +fd|                    d          D                       } | d| }!d t3          |          z   }"||"d!t3          |          z   z  }"|r|"d"z  }"|	                    ||!|"| #          ,| <    n| nt+          ,t"          d$          }#d%                    d& |#D                       }$t7          j        |$          }%,fd'}&t#          ,          dk    rt;          |d(d)*          5 }'|'                                }(ddd           n# 1 swxY w Y   t;          |d+fi t>          5 }'|(D ]-})|%                     |&|)          }*|'!                    |*           .	 ddd           n# 1 swxY w Y   ldS ),z?Add resolvers for the packages for which we want to show links.reference_urlNT)rX   z?<a href="{link}" title="{title}" class="{css_class}">{text}</a>z<span class="n">%s</span>z<span class="o">.</span>c                 R    g | ]$\  }}}|D ]}|                     d           ||g%S )z.html)endswith)r   dirpath_rz   filenames        r)   
<listcomp>z%_embed_code_links.<locals>.<listcomp>k  s^       !GQ	!  W%%		(   r6   z'embedding documentation hyperlinks for z... fuchsiac                 L    t           j                            | d                   S )Nr	   )r-   r.   basename)r   s    r)   <lambda>z#_embed_code_links.<locals>.<lambda>v  s    !1!1!A$!7!7 r6   )colorlengthstringify_funcr	   z.codeobj.jsonrv   ru   rw   r   r[   )r   z
resolving )r   builtinspythonpy   zpy:classis_classc              3   "   K   | ]	}|z  V  
d S Nr   )r   partorig_patterns     r)   	<genexpr>z$_embed_code_links.<locals>.<genexpr>  s9       0 048L4/0 0 0 0 0 0r6   zsphx-glr-backref-module-z sphx-glr-backref-type-z sphx-glr-backref-instance)r   title	css_classtext)rM   reverse|c              3   >   K   | ]}t          j        |          V  d S r   )r   escape)r   r[   s     r)   r   z$_embed_code_links.<locals>.<genexpr>  s*      ??RYt__??????r6   c                 8    |                                           S r   )group)rq   str_repls    r)   substitute_linkz*_embed_code_links.<locals>.substitute_link  s    EKKMM**r6   r   r   )r   w)"r-   r.   r/   buildersrcdirr   rQ   rV   sphinx_gallery_confr   outdirr   r   r   abspathwalkr   lenr   existsr   sortedrC   r   
startswithr   r   r   compiler"   	readlinesr
   subwrite)-r   gallery_confr2   doc_resolverssrc_gallery_dirthis_moduler#   r   html_gallery_dirlink_patternperiodflatiteratorr   r   r   
full_fnamesubpath
json_fnameexample_code_objr[   cobjsr   r   r   modnamecnameinvwantrM   rN   is_instance	name_htmlfull_function_namer   names	regex_strregexr   r(   lines_inrL   line_outr   r   s-                                              @@r)   _embed_code_linksr  M  s   Mgll3;#5{CCO(9??AA & &S	&{-BJ2+,,#!	. . .k** .CJ2C. .k* )$ 	& 	& 	&"1%%%%%%%%	& wrw||CK4F'T'TUU UL.L'F %'W-=%>%>  D C+CCC4yy77  H 155O" d( d(W\\"2GUCC
#.//!3556W\\WeCRCj?&B
 

 w~~j)) 	 &j11 +,, D	 D	D$T*E  D5 ? ? $^ 4d8nE ; ;G")--"4"4Q"7K LE #m33*7*D*L*L $jd +M + +KD%% !*84   2 !'EG'E'Ee'E'E         #g;;;;;;;;;&.|(F(F-k:"j00#(DD&-#7#7#7#7D*-))++ & &JC"~~d33 &',T{1~(+ % T) 1&%/1 $Z 00   '$*KK 0 0 0 0<@JJsOO0 0 0 % %	 18-A-A%-A-A*$>AT#B B %	 !,% 9<OPU<V<V VI ' F%)EEI.:.A.A!%"4&/!*	 /B / /+ + (. #E $ xS$777HH???????	
9%%	+ 	+ 	+ 	+ 	+ x==1j#888 +C==??+ + + + + + + + + + + + + + +j#//// (3$ ( (D$yy$??HIIh''''(( ( ( ( ( ( ( ( ( ( ( ( ( ( (Cd( d(sU   AB((C9CC3!JK&KKQ::Q>	Q>	1SS	S	c                     |dS | j         j        }| j        j        dvrdS t                              dd           |d         }t          |t                    s|g}|D ]}t          | ||           dS )z4Embed hyperlinks to documentation into example code.N)htmlreadthedocsz%embedding documentation hyperlinks...white)r   gallery_dirs)	rV   r   r   r[   r   inforZ   rl   r  )r   	exceptionr   r  r2   s        r)   embed_code_linksr    s    :1L {666
KK7wKGGG/LlD)) &$~# : :#|[9999: :r6   )r   )+r   r   jsonr-   r   r   r0   urllib.parseparser   urllib.requestrequestr   ior   pathlibr   urllib.errorr   r   sphinx.utilsphinxsphinx.errorsr   sphinx.searchr   utilsr
   r   r   utillogging	getLoggerr   r*   r5   rO   rQ   r   r   r   r  r  r   r6   r)   <module>r-     s       				     				  # # # # # # ' ' ' ' ' '             , , , , , , , ,     ( ( ( ( ( ( " " " " " " 5 5 5 5 5 5 5 5 5 5			&	&'7	8	8  *  3 3 3lk6 k6 k6 k6 k6 k6 k6 k6\       8P( P( P(f: : : : :r6   