B
    2d                 @   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  dddg}x>|D ]6}x0tjD ]}||jkr6P q6W || t d|  q*W d|kr|d y*dd	l	m
} t d
dtt|j  W n* tk
r } zt d W dd}~X Y nX d|kryddl}t d|j  W n, tk
r } zt d W dd}~X Y nX t| dr2|  } x|D ]}t d|  d}yt| |d}	d}W n8 tk
r } zt d|  t  W dd}~X Y nX |rt d|  t |	  t d q8W dS )zDiagnostic 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 %szhtml.parserhtml5liblxmlz;I noticed that %s is not installed. Installing it may help.zlxml-xmlr   )etreezFound lxml version %s.z.lxml is not installed or couldn't be imported.NzFound html5lib version %sz2html5lib is not installed or couldn't be imported.readz#Trying to parse your markup with %sF)featuresTz%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nameZbuilderr
   er   parsersuccesssoup r!   Y/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/bs4/diagnose.pydiagnose   sR    






r#   Tc             K   st   ddl m} |dd}t| tr,| d} t| }x:|j|f||d|D ]\}}td||j	|j
f  qNW 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)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    

r0   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 )AnnouncingParserzSubclass 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)r4   )r2   r   attrsr!   r!   r"   handle_starttago   s    z AnnouncingParser.handle_starttagc             C   s   |  d|  d S )Nz%s END)r4   )r2   r   r!   r!   r"   handle_endtagr   s    zAnnouncingParser.handle_endtagc             C   s   |  d|  d S )Nz%s DATA)r4   )r2   r   r!   r!   r"   handle_datau   s    zAnnouncingParser.handle_datac             C   s   |  d|  d S )Nz
%s CHARREF)r4   )r2   r   r!   r!   r"   handle_charrefx   s    zAnnouncingParser.handle_charrefc             C   s   |  d|  d S )Nz%s ENTITYREF)r4   )r2   r   r!   r!   r"   handle_entityref{   s    z!AnnouncingParser.handle_entityrefc             C   s   |  d|  d S )Nz
%s COMMENT)r4   )r2   r   r!   r!   r"   handle_comment~   s    zAnnouncingParser.handle_commentc             C   s   |  d|  d S )Nz%s DECL)r4   )r2   r   r!   r!   r"   handle_decl   s    zAnnouncingParser.handle_declc             C   s   |  d|  d S )Nz%s UNKNOWN-DECL)r4   )r2   r   r!   r!   r"   unknown_decl   s    zAnnouncingParser.unknown_declc             C   s   |  d|  d S )Nz%s PI)r4   )r2   r   r!   r!   r"   	handle_pi   s    zAnnouncingParser.handle_piN)__name__
__module____qualname____doc__r4   r6   r7   r8   r9   r:   r;   r<   r=   r>   r!   r!   r!   r"   r1   d   s   r1   c             C   s   t  }||  dS )zPrint 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)r1   feed)r   r   r!   r!   r"   htmlparser_trace   s    rD   ZaeiouZbcdfghjklmnpqrstvwxyz   c             C   s>   d}x4t | D ](}|d dkr$t}nt}|t|7 }qW |S )z#Generate a random word-like string.    r   )range_consonants_vowelsrandomchoice)lengthr3   itr!   r!   r"   rword   s    rP      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 )rQ   	   N)rP   rK   randint).0rN   r!   r!   r"   	<genexpr>   s    zrsentence.<locals>.<genexpr>)r   rH   )rM   r!   r!   r"   	rsentence   s    rW     c             C   s   dddddddg}g }x~t | D ]r}tdd	}|dkrRt|}|d
|  q |dkrr|ttdd q |dkr t|}|d|  q W dd| d S )z+Randomly generate an invalid HTML document.pdivspanrN   bscripttabler      z<%s>   rQ   rG   z</%s>z<html>
z</html>)rH   rK   rT   rL   r   rW   r   )num_elementsZ	tag_nameselementsrN   rL   Ztag_namer!   r!   r"   rdoc   s    

rd   順 c       
      C   s(  t dt  t| }t dt|  xdddgddgD ]z}d}y"t }t||}t }d}W n6 tk
r } zt d	|  t  W d
d
}~X Y nX |r6t d||| f  q6W 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	   r&   r   zhtml.parserFTz%s could not parse the markup.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   rd   lentimer   r   r   r   r	   r
   HTMLr   r   parse)
rb   r   r   r   ar    r\   r   r
   r   r!   r!   r"   benchmark_parsers   s4    


rk   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|bs42   N)tempfileNamedTemporaryFiler   rd   dictrl   cProfileZrunctxpstatsZStatsZ
sort_statsZprint_stats)rb   r   Z
filehandlefilenamer   varsstatsr!   r!   r"   profile   s    

rv   __main__)T)rE   )rQ   )rX   )re   )re   r	   )!rB   __license__rq   ior   html.parserr   rl   r   r   Zbs4.builderr   osrr   rK   rn   rg   r   r   r#   r0   r1   rD   rJ   rI   rP   rW   rd   rk   rv   r?   stdinr   r!   r!   r!   r"   <module>   s8   8
&



 

