o
    Rh                     @   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reej   dS 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 ]}tjD ]	}||jv r! nq|| t d|  qd|v rc|d zddl	m
} t d	d
tt|j  W n tyb } z
t d W Y d}~nd}~ww d|v rzddl}t d|j  W n ty } z
t d W Y d}~nd}~ww t| dr|  } |D ]C}t d|  d}z
t| |d}	d}W n ty } zt d|  t  W Y d}~nd}~ww |rt d|  t |	  t d qd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 %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   buildersr   removeappendr
   r   joinmapstrLXML_VERSIONImportErrorr	   hasattrr   r   	Exception	traceback	print_excprettify)
databasic_parsersnamebuilderr   er	   parsersuccesssoup r)   N/home/air/sanwanet/backup_V2/venv/lib/python3.10/site-packages/bs4/diagnose.pydiagnose   sl   





r+   Tc                 K   sp   ddl m} |dd}t| tr| d} 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)htmlr,   z%s, %4s, %sN)r
   r   pop
isinstancer   encoder   	iterparser   tagtext)r!   r.   kwargsr   r,   readereventelementr)   r)   r*   
lxml_traceN   s   

r9   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      |  d|  d S )Nz%s STARTr=   )r;   r#   attrsr)   r)   r*   handle_starttago      z AnnouncingParser.handle_starttagc                 C   r>   )Nz%s ENDr?   r;   r#   r)   r)   r*   handle_endtagr   rB   zAnnouncingParser.handle_endtagc                 C   r>   )Nz%s DATAr?   r;   r!   r)   r)   r*   handle_datau   rB   zAnnouncingParser.handle_datac                 C   r>   )Nz
%s CHARREFr?   rC   r)   r)   r*   handle_charrefx   rB   zAnnouncingParser.handle_charrefc                 C   r>   )Nz%s ENTITYREFr?   rC   r)   r)   r*   handle_entityref{   rB   z!AnnouncingParser.handle_entityrefc                 C   r>   )Nz
%s COMMENTr?   rE   r)   r)   r*   handle_comment~   rB   zAnnouncingParser.handle_commentc                 C   r>   )Nz%s DECLr?   rE   r)   r)   r*   handle_decl   rB   zAnnouncingParser.handle_declc                 C   r>   )Nz%s UNKNOWN-DECLr?   rE   r)   r)   r*   unknown_decl   rB   zAnnouncingParser.unknown_declc                 C   r>   )Nz%s PIr?   rE   r)   r)   r*   	handle_pi   rB   zAnnouncingParser.handle_piN)__name__
__module____qualname____doc__r=   rA   rD   rF   rG   rH   rI   rJ   rK   rL   r)   r)   r)   r*   r:   d   s    r:   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)r:   feed)r!   r&   r)   r)   r*   htmlparser_trace   s   rR   aeiou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)lengthr<   itr)   r)   r*   rword   s   r`      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 )ra   	   N)r`   r[   randint).0r^   r)   r)   r*   	<genexpr>   s    zrsentence.<locals>.<genexpr>)r   rX   )r]   r)   r)   r*   	rsentence   s   rg     c                 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 )z+Randomly generate an invalid HTML document.)pdivspanr^   bscripttabler      z<%s>   ra   rW   z</%s>z<html>
z</html>)rX   r[   rd   r\   r   rg   r   )num_elements	tag_nameselementsr^   r\   tag_namer)   r)   r*   rdoc   s   

rv   順 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 n tyK } zt d	|  t  W Y d
}~nd
}~ww |rXt 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
   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   rv   lentimer   r   r   r   r
   r   HTMLr	   r   parse)
rr   r!   r&   r'   ar(   rl   r%   r   r	   r)   r)   r*   benchmark_parsers   s:   


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|bs42   N)tempfileNamedTemporaryFiler#   rv   dictr~   cProfilerunctxpstatsStats
sort_statsprint_stats)rr   r&   
filehandlefilenamer!   varsstatsr)   r)   r*   profile   s   

r   __main__)T)rU   )ra   )rh   )rw   )rw   r
   )!rP   __license__r   ior   html.parserr   r~   r   r   bs4.builderr   osr   r[   r   ry   r   r   r+   r9   r:   rR   rZ   rY   r`   rg   rv   r}   r   rM   stdinr   r)   r)   r)   r*   <module>   s<    
8&




 