Current File : //lib/python3.6/site-packages/bs4/__pycache__/diagnose.cpython-36.pyc
3

#6]��@sdZdZddlZddlmZddlmZddlZddlmZm	Z	ddl
mZddlZddl
Z
ddlZddlZddlZddlZddlZddlZdd	�Zd#dd�ZGd
d�de�Zdd�ZdZdZd$dd�Zd%dd�Zd&dd�Zd'dd�Zd(d d!�Zed"k�reejj ��dS))z=Diagnostic functions, mainly for use when doing tech support.ZMIT�N)�StringIO)�
HTMLParser)�
BeautifulSoup�__version__)�builder_registryc;CsXtdt�tdtj�dddg}x>|D]6}x0tjD]}||jkr6Pq6W|j|�td|�q*Wd|kr�|jd�y*dd	l	m
}td
djtt
|j���Wn*tk
r�}ztd�WYd
d
}~XnXd|k�rydd
l}td|j�Wn,tk
�r}ztd�WYd
d
}~XnXt|d��r4|j�}n�|jd��sL|jd��rdtd|�td�d
Sy:tjj|��r�td|�t|��}|j�}Wd
QRXWntk
�r�YnXt�x�|D]�}td|�d}	yt||d�}
d}	Wn8tk
�r"}ztd|�tj�WYd
d
}~XnX|	�rBtd|�t|
j��tdd��q�Wd
S)z/Diagnostic suite for isolating common problems.z'Diagnostic running on Beautiful Soup %szPython version %szhtml.parser�html5lib�lxmlz;I noticed that %s is not installed. Installing it may help.zlxml-xmlr)�etreezFound lxml version %s�.z.lxml is not installed or couldn't be imported.NzFound html5lib version %sz2html5lib is not installed or couldn't be imported.�readzhttp:zhttps: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)�featuresTz%s could not parse the markup.z#Here's what %s did with the markup:�-�P)�printr�sys�versionrZbuildersr�remove�appendrr	�join�map�strZLXML_VERSION�ImportErrorr�hasattrr�
startswith�os�path�exists�open�
ValueErrorr�	Exception�	traceback�	print_excZprettify)�dataZ
basic_parsers�nameZbuilderr	�er�fp�parser�success�soup�r)�/usr/lib/python3.6/diagnose.py�diagnosesj








r+TcKsNddlm}x<|jt|�fd|i|��D]\}}td||j|jf�q(WdS)z�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.
    r)r	�htmlz%s, %4s, %sN)rr	Z	iterparserr�tag�text)r"r,�kwargsr	Zevent�elementr)r)r*�
lxml_traceZs$r1c@s`eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dS)�AnnouncingParserz?Announces HTMLParser parse events, without doing anything else.cCst|�dS)N)r)�self�sr)r)r*�_pgszAnnouncingParser._pcCs|jd|�dS)Nz%s START)r5)r3r#Zattrsr)r)r*�handle_starttagjsz AnnouncingParser.handle_starttagcCs|jd|�dS)Nz%s END)r5)r3r#r)r)r*�
handle_endtagmszAnnouncingParser.handle_endtagcCs|jd|�dS)Nz%s DATA)r5)r3r"r)r)r*�handle_datapszAnnouncingParser.handle_datacCs|jd|�dS)Nz
%s CHARREF)r5)r3r#r)r)r*�handle_charrefsszAnnouncingParser.handle_charrefcCs|jd|�dS)Nz%s ENTITYREF)r5)r3r#r)r)r*�handle_entityrefvsz!AnnouncingParser.handle_entityrefcCs|jd|�dS)Nz
%s COMMENT)r5)r3r"r)r)r*�handle_commentyszAnnouncingParser.handle_commentcCs|jd|�dS)Nz%s DECL)r5)r3r"r)r)r*�handle_decl|szAnnouncingParser.handle_declcCs|jd|�dS)Nz%s UNKNOWN-DECL)r5)r3r"r)r)r*�unknown_declszAnnouncingParser.unknown_declcCs|jd|�dS)Nz%s PI)r5)r3r"r)r)r*�	handle_pi�szAnnouncingParser.handle_piN)�__name__�
__module__�__qualname__�__doc__r5r6r7r8r9r:r;r<r=r>r)r)r)r*r2dsr2cCst�}|j|�dS)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.
    N)r2Zfeed)r"r&r)r)r*�htmlparser_trace�srCZaeiouZbcdfghjklmnpqrstvwxyz�cCs>d}x4t|�D](}|ddkr$t}nt}|tj|�7}qW|S)z#Generate a random word-like string.��r)�range�_consonants�_vowels�random�choice)�lengthr4�i�tr)r)r*�rword�srO�cCsdjdd�t|�D��S)z'Generate a random sentence-like string.� css|]}ttjdd��VqdS)rP�	N)rOrJ�randint)�.0rMr)r)r*�	<genexpr>�szrsentence.<locals>.<genexpr>)rrG)rLr)r)r*�	rsentence�srV��cCs�dddddddg}g}x~t|�D]r}tjdd	�}|dkrRtj|�}|jd
|�q |dkrr|jttjdd���q |d
kr tj|�}|jd|�q Wddj|�dS)z+Randomly generate an invalid HTML document.�pZdiv�spanrM�bZscript�tabler�z<%s>�rPrFz</%s>z<html>�
z</html>)rGrJrSrKrrVr)�num_elementsZ	tag_names�elementsrMrKZtag_namer)r)r*�rdoc�s

ra順c
Cs(tdt�t|�}tdt|��x�dddgddgD]z}d}y"tj�}t||�}tj�}d}Wn6tk
r�}ztd	|�tj�WYd
d
}~XnX|r6td|||f�q6Wdd
l	m
}tj�}|j|�tj�}td||�dd
l}	|	j
�}tj�}|j|�tj�}td||�d
S)z.Very basic head-to-head performance benchmark.z1Comparative parser benchmark on Beautiful Soup %sz3Generated a large invalid HTML document (%d bytes).rr,rzhtml.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.)rrra�len�timerrr r!rr	ZHTMLrr�parse)
r_r"r&r'�ar(rZr$r	rr)r)r*�benchmark_parsers�s4


rgrcCsXtj�}|j}t|�}tt||d�}tjd|||�tj	|�}|j
d�|jdd�dS)N)�bs4r"r&zbs4.BeautifulSoup(data, parser)Z
cumulativez
_html5lib|bs4�2)�tempfileZNamedTemporaryFiler#ra�dictrh�cProfileZrunctx�pstatsZStatsZ
sort_statsZprint_stats)r_r&Z
filehandle�filenamer"�varsZstatsr)r)r*�profile�s

rp�__main__)T)rD)rP)rW)rb)rbr)!rBZ__license__rl�iorZhtml.parserrrhrrZbs4.builderrrrmrJrjrdr rr+r1r2rCrIrHrOrVrargrpr?�stdinrr)r)r)r*�<module>s8C

!	



 


Page not found – Hello World !