
    TiK                     D   S r SrSSKrSSKJr  SSKJr  SSKrSSKJrJ	r	  SSK
Jr  SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrS rSS	 jr " S
 S\5      rS rSrSrSS jrSS jrSS jrSS jrSS jr\S:X  a!  \" \R>                  RA                  5       5        gg)z=Diagnostic functions, mainly for use when doing tech support.MIT    N)StringIO)
HTMLParser)BeautifulSoup__version__)builder_registryc           	         [        S[        -  5        [        S[        R                  -  5        / SQnU HK  n[        R
                   H  nX#R                  ;   d  M    M*     UR                  U5        [        SU-  5        MM     SU;   aM  UR                  S5         SSK	J
n  [        S	S
R                  [        [        UR                  5      5      -  5        SU;   a   SSKn[        SUR                  -  5        [#        U S5      (       a  U R%                  5       n OU R'                  S5      (       d  U R'                  S5      (       a  [        SU -  5        [        S5        g [(        R*                  R-                  U 5      (       a3  [        SU -  5        [/        U 5       nUR%                  5       n SSS5        O [        S5        U HX  n[        SU-  5        Sn	 [3        XS9n
Sn	U	(       a'  [        SU-  5        [        W
R;                  5       5        [        S5        MZ     g! [         a  n[        S5         SnAGNiSnAff = f! [         a  n[        S5         SnAGNiSnAff = f! , (       d  f       N= f! [0         a     Nf = f! [4         a-  n[        SU-  5        [6        R8                  " 5          SnANSnAff = f)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http:zhttps:z<"%s" looks like a URL. Beautiful Soup is not an HTTP client.zpYou need to use some other library to get the document behind the URL, and feed that document to Beautiful Soup.z7"%s" looks like a filename. Reading data from the file. z#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   
startswithospathexistsopen
ValueErrorr   	Exception	traceback	print_excprettify)databasic_parsersnamebuilderr   er   fpparsersuccesssoups              ^/var/www/html/backend/Backoffice_Marketplace/venv/lib/python3.13/site-packages/bs4/diagnose.pydiagnoser4      sw    
4{BD	,.7M'00G''' 1   &M  Z(	B"*SXXc#e>P>P6Q-RRT ]"	F.1E1EEG
 tVyy{		!	!T__X%>%>MPTTV  A  	B	ww~~d##PSWWY$Z2779D  Z $ 	b	4v=?	" 7DG 86AC4==?$x  ?  	B@B B	B  	FDF F	F  Z 	 	  	"3f<>!!	"sx   ;H% I #=I<  I+1I< J%
I/I  I
I(I##I(+
I95I< 9I< <
J	J	
K#J>>Kc                     SSK Jn  UR                  " [        U 5      4SU0UD6 H0  u  pE[	        U< SUR
                  <S SUR                  < 35        M2     g)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   htmlz, z>4N)r   r   	iterparser   r   tagtext)r*   r6   kwargsr   eventelements         r3   
lxml_tracer=   ]   sB     //(4.NtNvNw{{GLLAC O    c                   T    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rS rSrg)AnnouncingParserm   zSubclass 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                     [        U5        g )N)r   )selfss     r3   _pAnnouncingParser._pu   s	    ar>   c                 ,    U R                  SU-  5        g )Nz%s STARTrE   )rC   r,   attrss      r3   handle_starttag AnnouncingParser.handle_starttagx   s    
T!"r>   c                 ,    U R                  SU-  5        g )Nz%s ENDrH   rC   r,   s     r3   handle_endtagAnnouncingParser.handle_endtag{   s    4 r>   c                 ,    U R                  SU-  5        g )Nz%s DATArH   rC   r*   s     r3   handle_dataAnnouncingParser.handle_data~       	D !r>   c                 ,    U R                  SU-  5        g )Nz
%s CHARREFrH   rM   s     r3   handle_charrefAnnouncingParser.handle_charref       t#$r>   c                 ,    U R                  SU-  5        g )Nz%s ENTITYREFrH   rM   s     r3   handle_entityref!AnnouncingParser.handle_entityref   s    %&r>   c                 ,    U R                  SU-  5        g )Nz
%s COMMENTrH   rQ   s     r3   handle_commentAnnouncingParser.handle_comment   rX   r>   c                 ,    U R                  SU-  5        g )Nz%s DECLrH   rQ   s     r3   handle_declAnnouncingParser.handle_decl   rT   r>   c                 ,    U R                  SU-  5        g )Nz%s UNKNOWN-DECLrH   rQ   s     r3   unknown_declAnnouncingParser.unknown_decl   s    !D()r>   c                 ,    U R                  SU-  5        g )Nz%s PIrH   rQ   s     r3   	handle_piAnnouncingParser.handle_pi   s    $r>    N)__name__
__module____qualname____firstlineno____doc__rE   rJ   rN   rR   rV   rZ   r]   r`   rc   rf   __static_attributes__rh   r>   r3   r@   r@   m   s9    #!"%'%"* r>   r@   c                 :    [        5       nUR                  U 5        g)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*   r0   s     r3   htmlparser_tracerq      s     F
KKr>   aeioubcdfghjklmnpqrstvwxyzc                     Sn[        U 5       H2  nUS-  S:X  a  [        nO[        nU[        R                  " U5      -  nM4     U$ )z#Generate a random word-like string.r      r   )range_consonants_vowelsrandomchoice)lengthrD   its       r3   rwordr~      sD    
A6]q5A:AA	V]]1  Hr>   c                 D    SR                  S [        U 5       5       5      $ )z'Generate a random sentence-like string. c              3   b   #    U  H%  n[        [        R                  " S S5      5      v   M'     g7f)   	   N)r~   ry   randint).0r|   s     r3   	<genexpr>rsentence.<locals>.<genexpr>   s#     F1E&..1-..s   -/)r   rv   )r{   s    r3   	rsentencer      s    88FfFFFr>   c           	         / SQn/ n[        U 5       H  n[        R                  " SS5      nUS:X  a,  [        R                  " U5      nUR	                  SU-  5        ML  US:X  a1  UR	                  [        [        R                  " SS5      5      5        M  US:X  d  M  [        R                  " U5      nUR	                  SU-  5        M     S	S
R                  U5      -   S-   $ )z+Randomly generate an invalid HTML document.)pdivspanr|   bscripttabler      z<%s>   r   ru   z</%s>z<html>
z</html>)rv   ry   r   rz   r   r   r   )num_elements	tag_nameselementsr|   rz   tag_names         r3   rdocr      s    AIH< !$Q;}}Y/HOOFX-.q[OOIfnnQq&9:;q[}}Y/HOOGh./ ! dii))I55r>   c                    [        S[        -  5        [        U 5      n[        S[        U5      -  5        SSS/SS4 HY  nSn [        R                  " 5       n[        X5      n[        R                  " 5       nSnU(       d  MF  [        SUWW-
  4-  5        M[     SSK	J
n  [        R                  " 5       nUR                  U5        [        R                  " 5       n[        SXd-
  -  5        SS
Kn	U	R                  5       n[        R                  " 5       nUR                  U5        [        R                  " 5       n[        SXd-
  -  5        g
! [         a.  n[        S	U-  5        [        R                  " 5          S
nAGNS
nAff = f)z.Very basic head-to-head performance benchmark.z1Comparative parser benchmark on Beautiful Soup %sz3Generated a large invalid HTML document (%d bytes).r   r6   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   r   lentimer   r&   r'   r(   r   r   HTMLr   r   parse)
r   r*   r0   r1   ar2   r   r.   r   r   s
             r3   benchmark_parsersr      s:   	>LND	@3t9LNFF+ZG	"		A .D		AG 7761Q3-GI H 		A	JJt		A	1QS9;  "F		A
LL		A	5=?#  	"3f<>!!	"s   7E
F#E>>Fc                    [         R                  " 5       nUR                  n[        U 5      n[	        [
        XAS9n[        R                  " SXUU5        [        R                  " U5      nUR                  S5        UR                  SS5        g)z7Use Python's profiler on a randomly generated document.)bs4r*   r0   zbs4.BeautifulSoup(data, parser)
cumulativez_html5lib|bs42   N)tempfileNamedTemporaryFiler,   r   dictr   cProfilerunctxpstatsStats
sort_statsprint_stats)r   r0   
filehandlefilenamer*   varsstatss          r3   profiler      sl    ,,.JHDCd2DOO5HMLL"E	\"	or*r>   __main__)T)   )r   )i  )順 )r   r   )!rm   __license__r   ior   html.parserr   r   r   r   bs4.builderr   r!   r   ry   r   r   r'   r   r4   r=   r@   rq   rx   rw   r~   r   r   r   r   ri   stdinr   rh   r>   r3   <module>r      s    C    " 
 * ( 	      
 END $ z $ L	 %	G6$@@+ zSYY^^ r>   