o
    Ù­j¦  ã                	   @   s”  U d Z dZddlZddlmZ ddlmZ ddlZddlmZm	Z	 ddl
mZ ddlmZmZmZmZmZmZ er?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Zd/dd„Zd0d
ddededdfdd„ZG dd„ deƒZd
eddfdd„Z dZ!ee"d< dZ#ee"d< d1de$defdd„Z%d2de$defd!d"„Z&d3d$e$defd%d&„Z'd4d$e$ddfd(d)„Z(d5d$e$d+eddfd,d-„Z)e*d.krÈeej+ ,¡ ƒ dS dS )6z=Diagnostic functions, mainly for use when doing tech support.ÚMITé    N)ÚBytesIO)Ú
HTMLParser)ÚBeautifulSoupÚ__version__)Úbuilder_registry)ÚAnyÚIOÚListÚOptionalÚTupleÚTYPE_CHECKING)Ú_IncomingMarkupÚdatar   Úreturnc           	   	   C   s‚  t dt ƒ t dtj ƒ g d¢}|D ]}tjD ]	}||jv r! nq| |¡ t d| ƒ qd|v rZ| d¡ zddl	m
} t d	d
 tt|jƒ¡ ƒ W n tyY   t dƒ Y nw d|v ryzddl}t d|j ƒ W n tyx   t dƒ Y nw t| dƒr‚|  ¡ } |D ]:}t d| ƒ d}z
t| |d�}d}W n ty«   t d| ƒ t ¡  Y nw |rºt d| ƒ t | ¡ ƒ t dƒ q„dS )z­Diagnostic suite for isolating common problems.

    :param data: Some markup that needs to be explained.
    :return: None; diagnostics are printed to standard output.
    z'Diagnostic running on Beautiful Soup %szPython version %s)úhtml.parserÚhtml5libÚlxmlz;I noticed that %s is not installed. Installing it may help.r   zlxml-xmlr   ©ÚetreezFound lxml version %sÚ.z.lxml is not installed or couldn't be imported.r   NzFound html5lib version %sz2html5lib is not installed or couldn't be imported.Úreadz#Trying to parse your markup with %sF)ÚfeaturesTú%s could not parse the markup.z#Here's what %s did with the markup:zP--------------------------------------------------------------------------------)Úprintr   ÚsysÚversionr   Úbuildersr   ÚremoveÚappendr   r   ÚjoinÚmapÚstrÚLXML_VERSIONÚImportErrorr   Úhasattrr   r   Ú	ExceptionÚ	tracebackÚ	print_excÚprettify)	r   Úbasic_parsersÚnameÚbuilderr   r   ÚparserÚsuccessÚsoup© r0   úI/var/www/html/CropPilot/venv/lib/python3.10/site-packages/bs4/diagnose.pyÚdiagnose    sZ   

ÿ
ÿ€
ÿÿ
þ
ór2   TÚhtmlÚkwargsc                 K   sz   ddl m} | dd¡}t| tƒr|  d¡} t| tƒst| ƒ}|j|f||dœ|¤ŽD ]\}}t	d||j
|jf ƒ q+dS )	a´  Print out the lxml events that occur during parsing.

    This lets you see how lxml parses a document when no Beautiful
    Soup code is running. You can use this to determine whether
    an lxml-specific problem is in Beautiful Soup's lxml tree builders
    or in lxml itself.

    :param data: Some markup.
    :param html: If True, markup will be parsed with lxml's HTML parser.
       if False, lxml's XML parser will be used.
    r   r   ÚrecoverTÚutf8)r3   r5   z%s, %4s, %sN)r   r   ÚpopÚ
isinstancer"   Úencoder	   r   Ú	iterparser   ÚtagÚtext)r   r3   r4   r   r5   ÚreaderÚeventÚelementr0   r0   r1   Ú
lxml_traceX   s   


 ÿr@   c                	   @   sæ   e Zd ZdZdeddfdd„Z	dded	eeeee f  d
e	ddfdd„Z
ddede	ddfdd„Zdeddfdd„Zdeddfdd„Zdeddfdd„Zdeddfdd„Zdeddfdd„Zdeddfdd„Zdeddfdd„ZdS ) ÚAnnouncingParserzèSubclass of HTMLParser that announces parse events, without doing
    anything else.

    You can use this to get a picture of how html.parser sees a given
    document. The easiest way to do this is to call `htmlparser_trace`.
    Úsr   Nc                 C   s   t |ƒ d S )N)r   )ÚselfrB   r0   r0   r1   Ú_pw   s   zAnnouncingParser._pTr+   ÚattrsÚhandle_empty_elementc                 C   s   |   |› d|› d�¡ d S )Nú z START©rD   )rC   r+   rE   rF   r0   r0   r1   Úhandle_starttagz   s   z AnnouncingParser.handle_starttagÚcheck_already_closedc                 C   ó   |   d| ¡ d S )Nz%s ENDrH   )rC   r+   rJ   r0   r0   r1   Úhandle_endtag‚   ó   zAnnouncingParser.handle_endtagr   c                 C   rK   )Nz%s DATArH   ©rC   r   r0   r0   r1   Úhandle_data…   rM   zAnnouncingParser.handle_datac                 C   rK   )Nz
%s CHARREFrH   ©rC   r+   r0   r0   r1   Úhandle_charrefˆ   rM   zAnnouncingParser.handle_charrefc                 C   rK   )Nz%s ENTITYREFrH   rP   r0   r0   r1   Úhandle_entityref‹   rM   z!AnnouncingParser.handle_entityrefc                 C   rK   )Nz
%s COMMENTrH   rN   r0   r0   r1   Úhandle_commentŽ   rM   zAnnouncingParser.handle_commentc                 C   rK   )Nz%s DECLrH   rN   r0   r0   r1   Úhandle_decl‘   rM   zAnnouncingParser.handle_declc                 C   rK   )Nz%s UNKNOWN-DECLrH   rN   r0   r0   r1   Úunknown_decl”   rM   zAnnouncingParser.unknown_declc                 C   rK   )Nz%s PIrH   rN   r0   r0   r1   Ú	handle_pi—   rM   zAnnouncingParser.handle_pi©T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r"   rD   r
   r   r   ÚboolrI   rL   rO   rQ   rR   rS   rT   rU   rV   r0   r0   r0   r1   rA   o   s*    üþýü
ûrA   c                 C   s   t ƒ }| | ¡ dS )zÂPrint out the HTMLParser events that occur during parsing.

    This lets you see how HTMLParser parses a document when no
    Beautiful Soup code is running.

    :param data: Some markup.
    N)rA   Úfeed)r   r-   r0   r0   r1   Úhtmlparser_trace›   s   r^   ÚaeiouÚ_vowelsÚbcdfghjklmnpqrstvwxyzÚ_consonantsé   Úlengthc                 C   s:   d}t | ƒD ]}|d dkrt}nt}|t |¡7 }q|S )z<Generate a random word-like string.

    :meta private:
    Ú é   r   )Úrangerb   r`   ÚrandomÚchoice)rd   rB   ÚiÚtr0   r0   r1   Úrword«   s   rl   é   c                 C   s   d  dd„ t| ƒD ƒ¡S )z@Generate a random sentence-like string.

    :meta private:
    rG   c                 s   s    � | ]}t t d d¡ƒV  qdS )rm   é	   N)rl   rh   Úrandint)Ú.0rj   r0   r0   r1   Ú	<genexpr>¿   s   € zrsentence.<locals>.<genexpr>)r    rg   )rd   r0   r0   r1   Ú	rsentenceº   s   rr   éè  Únum_elementsc                 C   sš   g d¢}g }t | ƒD ]9}t dd¡}|dkr#t |¡}| d| ¡ q
|dkr3| tt dd¡ƒ¡ q
|dkrCt |¡}| d| ¡ q
d	d
 |¡ d S )zDRandomly generate an invalid HTML document.

    :meta private:
    )ÚpÚdivÚspanrj   ÚbÚscriptÚtabler   é   z<%s>é   rm   rf   z</%s>z<html>Ú
z</html>)rg   rh   ro   ri   r   rr   r    )rt   Ú	tag_namesÚelementsrj   ri   Útag_namer0   r0   r1   ÚrdocÂ   s   

€r�   é † c           	   	   C   s  t dt ƒ t| ƒ}t dt|ƒ ƒ dddgddfD ]5}d}zt ¡ }t||ƒ t ¡ }d}W n tyB   t d	| ƒ t ¡  Y nw |rOt d
||| f ƒ qddl	m
} t ¡ }| |¡ t ¡ }t d||  ƒ ddl}| ¡ }t ¡ }| |¡ t ¡ }t d||  ƒ dS )z.Very basic head-to-head performance benchmark.z1Comparative parser benchmark on Beautiful Soup %sz3Generated a large invalid HTML document (%d bytes).r   r3   r   r   FTr   z"BS4+%s parsed the markup in %.2fs.r   r   z$Raw lxml parsed the markup in %.2fs.Nz(Raw html5lib parsed the markup in %.2fs.)r   r   r�   ÚlenÚtimer   r&   r'   r(   r   r   ÚHTMLr   r   Úparse)	rt   r   Úparser_namer.   Úarx   r   r   r-   r0   r0   r1   Úbenchmark_parsersØ   s8   
þ€

r‰   r   r-   c                 C   sX   t  ¡ }|j}t| ƒ}tt||d�}t d|||¡ t 	|¡}| 
d¡ | dd¡ dS )z7Use Python's profiler on a randomly generated document.)Úbs4r   r-   zbs4.BeautifulSoup(data, parser)Ú
cumulativez_html5lib|bs4é2   N)ÚtempfileÚNamedTemporaryFiler+   r�   ÚdictrŠ   ÚcProfileÚrunctxÚpstatsÚStatsÚ
sort_statsÚprint_stats)rt   r-   Ú
filehandleÚfilenamer   ÚvarsÚstatsr0   r0   r1   Úprofileû   s   

rš   Ú__main__)r   r   r   NrW   )rc   )rm   )rs   )r‚   )r‚   r   )-r[   Ú__license__r�   Úior   Úhtml.parserr   rŠ   r   r   Úbs4.builderr   Útypingr   r	   r
   r   r   r   Úbs4._typingr   r’   rh   r�   r„   r'   r   r2   r\   r@   rA   r"   r^   r`   Ú__annotations__rb   Úintrl   rr   r�   r‰   rš   rX   Ústdinr   r0   r0   r0   r1   Ú<module>   s>     	
8,#ÿ