
    -Phq                     @   d 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  ed          Zd	Zd
ZdZ ej        eez   ej                  Zedz   Zedz   Z ej        e de dej                  Zd Zd Zd Z edg d          ZddZd Zd ZdS )zParser for python source files.    N)
namedtuple)BytesIO)dedent)ExtensionError)	getLoggerzsphinx-galleryzD
SyntaxError
===========

Example script with invalid Python syntax
z^[\ \t]*#\s*z6sphinx_gallery_([A-Za-z0-9_]+)(\s*=\s*(.+))?[\ \t]*\n?sphinx_gallery_start_ignoresphinx_gallery_end_ignorez(?:[\s\S]*?)z\n?c                     t          | dd          5 }|                                }ddd           n# 1 swxY w Y   	 t          j        |          }||fS # t          $ r d|fcY S w xY w)zParse source file into AST node.

    Parameters
    ----------
    filename : str
        File path

    Returns
    -------
    node : AST node
    content : utf-8 encoded string
    rzutf-8)encodingN)openreadastparseSyntaxError)filenamefidcontentnodes       _/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/sphinx_gallery/py_source_parser.pyparse_source_filer   .   s     
hg	.	.	. #((**              y!!W}   W}s   488 A A)(A)c                 ^   t          |           \  }}|t          |d|fS t          |t          j                  s,t          d                    |j        j                            |j	        rOt          |j	        d         t          j
                  r*t          |j	        d         j        t          j                  s"t          d                    |                     t          j        |          }|J |j	        d         j        j        dd         dk    rd|z   }t          j        t          |                                          j                  }|D ]}|j        dk    r|j        \  }} nd}d                    |                    d          |d                   }|dz  }||||fS )a  Separate ``filename`` content between docstring and the rest.

    Strongly inspired from ast.get_docstring.

    Returns
    -------
    docstring : str
        docstring of ``filename``
    rest : str
        ``filename`` content without the docstring
    lineno : int
        The line number.
    node : ast.Module
        The ast node. When `filename` parsed with `mode='exec'` node should be
        of type `ast.Module`.
    N   z4This function only supports modules. You provided {}r   zCould not find docstring in file "{}". A docstring is required by sphinx-gallery unless the file is ignored by "ignore_pattern"
   )r   SYNTAX_ERROR_DOCSTRING
isinstancer   Moduler   format	__class____name__bodyExprvalueConstantget_docstringtokenizer   encodereadline
exact_typeendjoinsplit)	r   r   r   	docstringtstklineno_rests	            r   _get_docstring_and_restr4   F   s   " &h//MD'|%w477dCJ'' 
BII' 
 
 	
 			
ty|SX..	
 ty|)3<88	

 ==CVH=M=M
 
 	
 !$''I   y|#t++9$			77>>#3#344=	>	>B  =AIFAE   99W]]4((122D
aKFdFD((    c                 8   i }t          j        t          |           D ]|}|                    d          }|                    d          }|/	 t	          j        |          }|||<   J# t          t          f$ r t          	                    d||           Y yw xY w|S )z=Pull out the file-specific config specified in the docstring.r   r   Nz4Sphinx-gallery option %s was passed invalid value %s)
refinditerINFILE_CONFIG_PATTERNgroupr   literal_evalr   
ValueErrorloggerwarning)r   	file_confmatchnamer$   s        r   extract_file_configrB      s    I2G<< $ ${{1~~A=	$$U++E $IdOO Z( 	 	 	NNFe    	 s   A''-BBBlock)typer   r1   Fc                    t          |           \  }}}}t          d|d          g}t          |          }t          j        dt          j                  }t          j        dt          j                  }	d}
t          j        ||          D ]"}||
|                                         }|                                r$|	                    t          d||                     ||
                    d          z  }|dz  }|                    d	          }t          t          j        |	d
|                                                    }|                                r$|	                    t          d||                     ||
                    d          z  }|                                }
$||
d         }|                                r$|	                    t          d||                     ||f}|r||fz  }|S )a~  Return list with source file separated into code and text blocks.

    Parameters
    ----------
    source_file : str
        Path to the source file.
    return_node : bool
        If True, return the ast node.

    Returns
    -------
    file_conf : dict
        File-specific settings given in source file comments as:
        ``# sphinx_gallery_<name> = <value>``
    blocks : list
        (label, content, line_number)
        List where each element is a tuple with the label ('text' or 'code'),
        the corresponding content string of block and the leading line number.
    node : ast.Module
        The parsed ast node.
    textr   zC(?P<header_line>^#{20,}.*|^# ?%%.*)\s(?P<text_content>(?:^#.*\s?)*))flagsz^#r   coder   text_content N)r4   rC   rB   r7   compileMr8   startstripappendcountr:   r   sublstripr+   )source_filereturn_noder.   rest_of_contentr1   r   blocksr?   patternsub_pat
pos_so_farr@   code_block_contentrI   text_block_contentremaining_contentouts                    r   split_code_and_text_blocksr^      s   , 0G{/S/S,IFIq))*F#O44IjNd  G jRT***GJWo66 ! !,Z%++---GH##%% 	EMM%(:FCCDDD$**4000!{{>22#BF7B$E$EFFMMOO##%% 	EMM%(:FCCDDD,$$T***YY[[

'
4   @eF$5v>>???f
C wJr5   c                 6   t          t          j        t          | t          j                            }t          t          j        t
          | t          j                            }||k    rt          d          t          j        t          d|           d         S )al  
    Return the content of *code_block* with ignored areas removed.

    An ignore block starts with # sphinx_gallery_start_ignore, and ends with
    # sphinx_gallery_end_ignore. These lines and anything in between them will
    be removed, but surrounding empty lines are preserved.

    Parameters
    ----------
    code_block : str
        A code segment.
    z^All "sphinx_gallery_start_ignore" flags must have a matching "sphinx_gallery_end_ignore" flag!rJ   r   )	lenr7   findallSTART_IGNORE_FLAG	MULTILINEEND_IGNORE_FLAGr   subnIGNORE_BLOCK_PATTERN)
code_blocknum_start_flagsnum_end_flagss      r   remove_ignore_blocksrj      s}     "*%6
BLQQRRO
?JMMNNM-''0
 
 	
 7'Z88;;r5   c                 B    t          j        t          d|           \  }}|S )a   
    Return the content of *code_block* with in-file config comments removed.

    Comment lines of the pattern '# sphinx_gallery_[option] = [val]' are
    removed, but surrounding empty lines are preserved.

    Parameters
    ----------
    code_block : str
        A code segment.
    rJ   )r7   re   r9   )rg   parsed_coder2   s      r   remove_config_commentsrm      s"     W2B
CCNKr5   )F)__doc__r   r7   r'   collectionsr   ior   textwrapr   sphinx.errorsr   sphinx.util.loggingr   r=   r   
FLAG_START	FLAG_BODYrK   rc   r9   rb   rd   rf   r   r4   rB   rC   r^   rj   rm    r5   r   <module>rw      s   & &
 


 				  " " " " " "             ( ( ( ( ( ( ) ) ) ) ) )	#	$	$ " 
E	"
:	#92<HH !>> ::!rz;;;;;R\  
  0;) ;) ;)|  & 	
7999::8 8 8 8v< < <0    r5   