a
    ÷£Be  ã                   @   s  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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Zdd	„ Zd#dd„ZG dd„ deƒZdd„ ZdZdZd$dd„Zd%dd„Zd&dd„Zd'dd„Zd(d d!„Zed"k�reej  ¡ ƒ dS ))z=Diagnostic functions, mainly for use when doing tech support.ÚMITé    N)ÚBytesIO)Ú
HTMLParser)ÚBeautifulSoupÚ__version__)Úbuilder_registryc           
      C   sÄ  t dt ƒ t dtj ƒ g d¢}|D ]4}tjD ]}||jv r0 q&q0| |¡ t d| ƒ q&d|v rÆ| d¡ z*ddl	m
} t d	d
 tt|jƒ¡ ƒ W n, tyÄ } zt dƒ W Y d}~n
d}~0 0 d|v �rzddl}t d|j ƒ W n. t�y } zt dƒ W Y d}~n
d}~0 0 t| dƒ�r.|  ¡ } |D ]Œ}t d| ƒ d}zt| |d�}	d}W n: t�y” } z t d| ƒ t ¡  W Y d}~n
d}~0 0 |�r´t d| ƒ t |	 ¡ ƒ t dƒ �q2dS )z¼Diagnostic suite for isolating common problems.

    :param data: A string containing 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.Nr	   z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   Zbuildersr   ÚremoveÚappendr
   r   ÚjoinÚmapÚstrZLXML_VERSIONÚImportErrorr	   Úhasattrr   r   Ú	ExceptionÚ	tracebackÚ	print_excZprettify)
ÚdataZbasic_parsersÚnameÚbuilderr   Úer	   ÚparserÚsuccessÚsoup© r%   úO/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/bs4/diagnose.pyÚdiagnose   sZ    


ÿÿ
ÿ
ÿr'   Tc                 K   sp   ddl m} | dd¡}t| tƒr,|  d¡} t| ƒ}|j|f||dœ|¤ŽD ]\}}td||j	|j
f ƒ qLd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)Úhtmlr(   z%s, %4s, %sN)r
   r   ÚpopÚ
isinstancer   Úencoder   Z	iterparser   ÚtagÚtext)r   r*   Úkwargsr   r(   ÚreaderÚeventÚelementr%   r%   r&   Ú
lxml_traceN   s    

ÿÿÿr4   c                   @   s`   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Z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`.
    c                 C   s   t |ƒ d S )N)r   )ÚselfÚsr%   r%   r&   Ú_pl   s    zAnnouncingParser._pc                 C   s   |   d| ¡ d S )Nz%s START©r8   )r6   r   Úattrsr%   r%   r&   Úhandle_starttago   s    z AnnouncingParser.handle_starttagc                 C   s   |   d| ¡ d S )Nz%s ENDr9   ©r6   r   r%   r%   r&   Úhandle_endtagr   s    zAnnouncingParser.handle_endtagc                 C   s   |   d| ¡ d S )Nz%s DATAr9   ©r6   r   r%   r%   r&   Úhandle_datau   s    zAnnouncingParser.handle_datac                 C   s   |   d| ¡ d S )Nz
%s CHARREFr9   r<   r%   r%   r&   Úhandle_charrefx   s    zAnnouncingParser.handle_charrefc                 C   s   |   d| ¡ d S )Nz%s ENTITYREFr9   r<   r%   r%   r&   Úhandle_entityref{   s    z!AnnouncingParser.handle_entityrefc                 C   s   |   d| ¡ d S )Nz
%s COMMENTr9   r>   r%   r%   r&   Úhandle_comment~   s    zAnnouncingParser.handle_commentc                 C   s   |   d| ¡ d S )Nz%s DECLr9   r>   r%   r%   r&   Úhandle_decl�   s    zAnnouncingParser.handle_declc                 C   s   |   d| ¡ d S )Nz%s UNKNOWN-DECLr9   r>   r%   r%   r&   Úunknown_decl„   s    zAnnouncingParser.unknown_declc                 C   s   |   d| ¡ d S )Nz%s PIr9   r>   r%   r%   r&   Ú	handle_pi‡   s    zAnnouncingParser.handle_piN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r8   r;   r=   r?   r@   rA   rB   rC   rD   rE   r%   r%   r%   r&   r5   d   s   r5   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)r5   Úfeed)r   r"   r%   r%   r&   Úhtmlparser_traceŠ   s    rK   ZaeiouZbcdfghjklmnpqrstvwxyzé   c                 C   s:   d}t | ƒD ](}|d dkr"t}nt}|t |¡7 }q|S )z#Generate a random word-like string.Ú é   r   )ÚrangeÚ_consonantsÚ_vowelsÚrandomÚchoice)Úlengthr7   ÚiÚtr%   r%   r&   Úrword˜   s    rW   é   c                 C   s   d  dd„ t| ƒD ƒ¡S )z'Generate a random sentence-like string.ú c                 s   s   | ]}t t d d¡ƒV  qdS )rX   é	   N)rW   rR   Úrandint)Ú.0rU   r%   r%   r&   Ú	<genexpr>¥   ó    zrsentence.<locals>.<genexpr>)r   rO   )rT   r%   r%   r&   Ú	rsentence£   s    r_   éè  c                 C   sš   g d¢}g }t | ƒD ]r}t dd¡}|dkrFt |¡}| d| ¡ q|dkrf| tt dd¡ƒ¡ q|dkrt |¡}| d| ¡ qd	d
 |¡ d S )z+Randomly generate an invalid HTML document.)ÚpÚdivÚspanrU   ÚbÚscriptÚtabler   é   z<%s>é   rX   rN   z</%s>z<html>Ú
z</html>)rO   rR   r[   rS   r   r_   r   )Únum_elementsZ	tag_namesÚelementsrU   rS   Ztag_namer%   r%   r&   Úrdoc§   s    

rl   é † c           
      C   s&  t dt ƒ t| ƒ}t dt|ƒ ƒ dddgddfD ]|}d}z"t ¡ }t||ƒ}t ¡ }d}W n8 ty– } z t d	| ƒ t ¡  W Y d
}~n
d
}~0 0 |r4t d||| f ƒ q4d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
   r*   r	   r   FTr   Nz"BS4+%s parsed the markup in %.2fs.r   r   z$Raw lxml parsed the markup in %.2fs.z(Raw html5lib parsed the markup in %.2fs.)r   r   rl   ÚlenÚtimer   r   r   r   r
   r   ÚHTMLr	   r   Úparse)
rj   r   r"   r#   Úar$   rd   r!   r   r	   r%   r%   r&   Úbenchmark_parsers¹   s4    


rs   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)Z
cumulativez_html5lib|bs4é2   N)ÚtempfileÚNamedTemporaryFiler   rl   Údictrt   ÚcProfileZrunctxÚpstatsZStatsZ
sort_statsZprint_stats)rj   r"   Z
filehandleÚfilenamer   ÚvarsÚstatsr%   r%   r&   ÚprofileÙ   s    

r~   Ú__main__)T)rL   )rX   )r`   )rm   )rm   r
   )!rI   Ú__license__ry   Úior   Úhtml.parserr   rt   r   r   Zbs4.builderr   Úosrz   rR   rv   ro   r   r   r'   r4   r5   rK   rQ   rP   rW   r_   rl   rs   r~   rF   Ústdinr   r%   r%   r%   r&   Ú<module>   s8   8
&



 

