B
    ê¹`ÑE  ã               @   s,  d 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mZ ddlm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZmZmZmZmZ ddlmZ dd	lmZ dd
lmZ eeƒZdej ej e¡d¡gZ ddd„Z!dd„ Z"dd„ Z#G dd„ de$ƒZ%G dd„ de$ƒZ&G dd„ de$ƒZ'dd„ Z(dd„ Z)dS )zHTML reporting for coverage.py.é    N)Úenv)ÚiitemsÚSimpleNamespaceÚformat_local_datetime)Úadd_data_to_hash)Úflat_rootname)ÚCoverageExceptionÚ
ensure_dirÚfile_be_goneÚHasherÚisolate_module)Úget_analysis_to_report)ÚNumbers)ÚTemplitez/usr/share/javascriptZ	htmlfilesÚ c             C   s‚   g }xbt D ]Z}tj || ¡}tj |¡r,|S | |¡ |r
tj ||| ¡}tj |¡rZ|S | |¡ q
W td| t ¡ |f ƒ‚dS )a  Return the path to a data file of ours.

    The file is searched for on `STATIC_PATH`, and the first place it's found,
    is returned.

    Each directory in `STATIC_PATH` is searched as-is, and also, if `pkgdir`
    is provided, at that sub-directory.

    z/Couldn't find static file %r from %r, tried: %rN)ÚSTATIC_PATHÚosÚpathÚjoinÚexistsÚappendr   Úgetcwd)ÚfnameÚpkgdirZtriedZ
static_dirZstatic_filename© r   úU/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/coverage/html.pyÚdata_filename#   s    


r   c          	   C   s    t t| ƒƒ
}| ¡ S Q R X dS )z+Return the contents of a data file of ours.N)Úopenr   Úread)r   Z	data_filer   r   r   Ú	read_data?   s    r   c          	   C   sD   t jdd|t jdd }t| dƒ}| | dd¡¡ W dQ R X dS )	z*Write `html` to `fname`, properly encoded.z(\A\s+)|(\s+$)r   )ÚflagsÚ
ÚwbÚasciiÚxmlcharrefreplaceN)ÚreÚsubÚ	MULTILINEr   ÚwriteÚencode)r   ÚhtmlÚfoutr   r   r   Ú
write_htmlE   s    r,   c               @   s$   e Zd ZdZdZdd„ Zdd„ ZdS )ÚHtmlDataGenerationz8Generate structured data to be turned into HTML reports.z(empty)c             C   sX   || _ | j j| _| j  ¡ }| ¡ | _| jjrF| ¡ dhkrF| j  d¡ | | jj¡ d S )Nr   zNo contexts were measured)	ÚcoverageÚconfigÚget_dataÚhas_arcsÚshow_contextsZmeasured_contextsÚ_warnZset_query_contextsZreport_contexts)ÚselfÚcovÚdatar   r   r   Ú__init__Q   s    


zHtmlDataGeneration.__init__c                s€  ˆ j r| ¡ }| ¡ }ˆ jjr,|j |j¡}g }x4t| 	¡ ddD ]\}}d}	g }
g }||j
krjd}	nv||jkrzd}	nfˆ j rÒ||krÒd}	xR|| D ]6}|dk r®|
 d¡ n
|
 |¡ | | |||¡¡ q–W n||jkràd	}	d }}d}|	r>ˆ jjr>t‡ fd
d„|| D ƒƒ}|ˆ jgkr,ˆ j}nd t|ƒ¡}|}| t|||	||jk||||
|d	¡ qDW t| ¡ |j|d}|S )z.Produce the data needed for one file's report.é   )ÚstartNÚexcÚmisÚparr   ÚexitÚrunc             3   s   | ]}|pˆ j V  qd S )N)ÚEMPTY)Ú.0Úc)r4   r   r   ú	<genexpr>~   s    z3HtmlDataGeneration.data_for_file.<locals>.<genexpr>z{} ctx)	ÚtokensÚnumberÚcategoryZ	statementÚcontextsÚcontexts_labelÚcontext_listÚshort_annotationsÚlong_annotations)Úrelative_filenameÚnumsÚlines)r1   Úmissing_branch_arcsÚarcs_executedr/   r2   r6   Úcontexts_by_linenoÚfilenameÚ	enumerateZsource_token_linesZexcludedÚmissingr   Zmissing_arc_descriptionZ
statementsÚsortedr?   ÚformatÚlenr   rK   Únumbers)r4   ÚfrÚanalysisrN   rO   rP   rM   ÚlinenorC   rE   rI   rJ   ÚbrF   rG   rH   Ú	file_datar   )r4   r   Údata_for_file[   s\    



z HtmlDataGeneration.data_for_fileN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r?   r7   r]   r   r   r   r   r-   L   s   
r-   c            
   @   sP   e Zd ZdZdddddddd	d
dg
Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z	dS )ÚHtmlReporterzHTML reporting.)z	style.cssr   )zjquery.min.jsZjquery)z"jquery.ba-throttle-debounce.min.jszjquery-throttle-debounce)zjquery.hotkeys.jszjquery-hotkeys)zjquery.isonscreen.jszjquery-isonscreen)zjquery.tablesorter.min.jszjquery-tablesorter)zcoverage_html.jsr   )zkeybd_closed.pngr   )zkeybd_open.pngr   )zfavicon_32.pngr   c             C   s0  || _ | j j| _| jj| _| jj| _| jd kr8| jj| _| jj| _| jd krV| jj| _| jj}t	j
rn| d¡}| jjrŠtj | jj¡| _nd | _| j  ¡ | _| j ¡ | _g | _g | _t| jƒ| _t| j ƒ| _tƒ | _tttt jt j|t t!j! "¡ ƒ| j| j| jj#dddddœdœ| _$t%dƒ| _&t'| j&| j$ƒ| _(d S )	NÚutf8zexc show_exczmis show_miszpar run show_parr>   )r:   r;   r<   r>   )ÚescapeÚpairrV   Ú__url__Ú__version__ÚtitleZ
time_stampÚ	extra_cssr1   r2   rE   zpyfile.html))r.   r/   Zhtml_dirÚ	directoryZhtml_skip_coveredÚskip_coveredZhtml_skip_emptyÚ
skip_emptyZ
html_titler   ÚPY2Údecoderi   r   r   Úbasenamer0   r6   r1   Úfile_summariesÚall_files_numsÚIncrementalCheckerÚincrr-   Údatagenr   Útotalsrd   re   rV   rf   rg   r   ÚdatetimeÚnowr2   Útemplate_globalsr   Úpyfile_html_sourcer   Úsource_tmpl)r4   r5   rh   r   r   r   r7   ¬   sL    









zHtmlReporter.__init__c             C   s|   | j  ¡  | j  | j| j¡ x$t| j|ƒD ]\}}|  ||¡ q*W | jsPt	dƒ‚t
| jƒ| _|  ¡  |  ¡  | jjoz| jjS )zcGenerate an HTML report for `morfs`.

        `morfs` is a list of modules or file names.

        zNo data to report.)rs   r   Úcheck_global_datar/   ry   r   r.   Ú	html_filerq   r   Úsumru   Ú
index_fileÚmake_local_static_report_filesÚn_statementsZ
pc_covered)r4   ZmorfsrX   rY   r   r   r   Úreportå   s    
zHtmlReporter.reportc             C   sZ   x0| j D ]&\}}t t||ƒtj | j|¡¡ qW | jrVt | j	jtj | j| j¡¡ dS )z5Make local instances of static files for HTML report.N)
ÚSTATIC_FILESÚshutilÚcopyfiler   r   r   r   rj   ri   r/   )r4   Zstaticr   r   r   r   r   ÿ   s    z+HtmlReporter.make_local_static_report_filesc                s2  t | ¡ ƒ}|d }t| jƒ tj | j|¡}|j}| j 	|¡ | j
rn|jdk}|jdk}|rn|rnt|ƒ dS | jrŠ|jdkrŠt|ƒ dS | j | j||¡r´| j 	| j |¡¡ dS | j ||¡}	x&|	jD ]‰ g }
xJˆ jD ]@\}}|dkrþ|
 	t|ƒ¡ qÞt|ƒp
d}|
 	d ||¡¡ qÞW d |
¡ˆ _ˆ jrTd ‡ fd	d
„ˆ jD ƒ¡ˆ _ndˆ _ˆ jr®ˆ j}t|ƒdkr‚|d ˆ _ n*d t|ƒd dd
„ t!|ddD ƒ¡¡ˆ _ ndˆ _ g }ˆ j"rÖ| 	| j#d ˆ j" ¡ d |¡pädˆ _$qÌW | j% &|	j'¡}
t(||
ƒ ||| ¡ dœ}| j 	|¡ | j )||¡ dS )z*Generate an HTML file for one source file.z.htmlr   NÚwsz&nbsp;z<span class="{}">{}</span>r   z,&nbsp;&nbsp; c             3   s   | ]}d   ˆ j|¡V  qdS )z{}&#x202F;&#x219B;&#x202F;{}N)rU   rD   )r@   Úd)Úldatar   r   rB   A  s   z)HtmlReporter.html_file.<locals>.<genexpr>r8   z{:d} missed branches: {}z, c             s   s   | ]\}}d   ||¡V  qdS )z{:d}) {}N)rU   )r@   ÚnumZann_longr   r   r   rB   O  s   )r9   rE   ú Zpln)rL   Úhtml_filenamerK   )*r   rK   r	   rj   r   r   r   rW   rq   r   rk   Z	n_missingZn_partial_branchesr
   rl   r€   rs   Úcan_skip_filer6   rp   Ú
index_infort   r]   rM   rC   rd   rU   r*   rI   ZannotaterJ   rV   Zannotate_longrR   rE   rx   Z	css_classrz   ÚrenderÚ__dict__r,   Úset_index_info)r4   rX   rY   ÚrootnamerŠ   Z	html_pathrL   Zno_missing_linesZno_missing_branchesr\   r*   Útok_typeZtok_textZtok_htmlZlongsZcss_classesrŒ   r   )r‡   r   r|     sn    





zHtmlReporter.html_filec             C   sH   t tdƒ| jƒ}| | j| jdœ¡}ttj 	| j
d¡|ƒ | j ¡  dS )z*Write the index.html file for this report.z
index.html)Úfilesru   N)r   r   rx   r   rp   ru   r,   r   r   r   rj   rs   r(   )r4   Z
index_tmplr*   r   r   r   r~   g  s    zHtmlReporter.index_fileN)
r^   r_   r`   ra   r‚   r7   r   r   r|   r~   r   r   r   r   rb   š   s    9Xrb   c               @   sh   e Zd Zd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 )rr   z0Logic and data to support incremental reporting.zstatus.jsoné   c             C   s   || _ |  ¡  d S )N)rj   Úreset)r4   rj   r   r   r   r7   ˜  s    zIncrementalChecker.__init__c             C   s   d| _ i | _dS )z5Initialize to empty. Causes all files to be reported.r   N)Úglobalsr’   )r4   r   r   r   r”   œ  s    zIncrementalChecker.resetc          	   C   sÞ   d}y4t j | j| j¡}t|ƒ}t |¡}W dQ R X W n tt	fk
rT   d}Y n,X d}|d | j
krnd}n|d tjkr€d}|rÒi | _x:t|d ƒD ]*\}}t|d d Ž |d d< || j|< q˜W |d	 | _n|  ¡  dS )
z)Read the information we stored last time.FNTrU   Úversionr’   ÚindexrL   r•   )r   r   r   rj   ÚSTATUS_FILEr   ÚjsonÚloadÚIOErrorÚ
ValueErrorÚSTATUS_FORMATr.   rg   r’   r   r   r•   r”   )r4   ÚusableÚstatus_fileZfstatusÚstatusrQ   Úfileinfor   r   r   r   ¡  s&    

zIncrementalChecker.readc          	   C   sŒ   t j | j| j¡}i }x6t| jƒD ](\}}|d d  ¡ |d d< |||< q"W | jt	j
| j|dœ}t|dƒ}tj||dd W dQ R X dS )zWrite the current status.r—   rL   )rU   r–   r•   r’   Úw)ú,ú:)Ú
separatorsN)r   r   r   rj   r˜   r   r’   Z	init_argsr   r.   rg   r•   r   r™   Údump)r4   rŸ   r’   rQ   r¡   r    r+   r   r   r   r(   º  s    zIncrementalChecker.writec             G   sB   t ƒ }x|D ]}| |¡ qW | ¡ }| j|kr>|  ¡  || _dS )z<Check the global data that can affect incremental reporting.N)r   ÚupdateÚ	hexdigestr•   r”   )r4   r6   Úmr†   Zthese_globalsr   r   r   r{   Ë  s    

z$IncrementalChecker.check_global_datac             C   sZ   t ƒ }| | ¡  d¡¡ t||j|ƒ | ¡ }|  |¡}||krFdS |  ||¡ dS dS )z©Can we skip reporting this file?

        `data` is a CoverageData object, `fr` is a `FileReporter`, and
        `rootname` is the name being used for the file.
        zutf-8TFN)	r   r§   Úsourcer)   r   rQ   r¨   Ú	file_hashÚset_file_hash)r4   r6   rX   r   r©   Z	this_hashZ	that_hashr   r   r   r‹   Õ  s    
z IncrementalChecker.can_skip_filec             C   s   | j  |i ¡ dd¡S )z#Get the hash of `fname`'s contents.Úhashr   )r’   Úget)r4   r   r   r   r   r«   é  s    zIncrementalChecker.file_hashc             C   s   || j  |i ¡d< dS )z#Set the hash of `fname`'s contents.r­   N)r’   Ú
setdefault)r4   r   Úvalr   r   r   r¬   í  s    z IncrementalChecker.set_file_hashc             C   s   | j  |i ¡ di ¡S )z/Get the information for index.html for `fname`.r—   )r’   r®   )r4   r   r   r   r   rŒ   ñ  s    zIncrementalChecker.index_infoc             C   s   || j  |i ¡d< dS )z/Set the information for index.html for `fname`.r—   N)r’   r¯   )r4   r   Úinfor   r   r   r   õ  s    z!IncrementalChecker.set_index_infoN)r^   r_   r`   ra   r˜   r   r7   r”   r   r(   r{   r‹   r«   r¬   rŒ   r   r   r   r   r   rr   v  s   
rr   c             C   s   |   dd¡  dd¡S )z\HTML-escape the text in `t`.

    This is only suitable for HTML text, not attributes.

    ú&z&amp;ú<z&lt;)Úreplace)Útr   r   r   rd   ü  s    rd   c             C   s   d|  S )zEFormat a pair of numbers so JavaScript can read them in an attribute.z%s %sr   )Zratior   r   r   re     s    re   )r   )*ra   rv   r™   r   r%   rƒ   r.   r   Zcoverage.backwardr   r   r   Zcoverage.datar   Zcoverage.filesr   Zcoverage.miscr   r	   r
   r   r   Zcoverage.reportr   Zcoverage.resultsr   Zcoverage.templiter   r   r   ÚdirnameÚ__file__r   r   r   r,   Úobjectr-   rb   rr   rd   re   r   r   r   r   Ú<module>   s6   
N ] 
