B
    `                 @   s   d Z ddlZddlZddlZddlmZmZ ddlm	Z	m
Z
mZmZ ddlmZmZ ddlmZ ddlmZmZ ddlmZ eeZe	d	d
dd Ze	dd
dd Ze	dd
dd Zdd Zdd ZG dd deZdS )z'Python source expertise for coverage.py    N)envfiles)contract	expensiveisolate_module
join_regex)CoverageExceptionNoSource)PythonParser)source_token_linessource_encoding)FileReporterbytes)returnsc          	   C   s@   t | d}| }W dQ R X tjr,t|}|ddddS )zFRead the Python source text from `filename`.

    Returns bytes.

    rbNs   
   
   )openreadr   Z
IRONPYTHONr   replace)filenamefsource r   W/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/coverage/python.pyread_python_source   s
    r   unicodec             C   s   t j| \}}|dkr(tjr(ddg}n|g}xV|D ]4}|| }t j|rVt|}P t|}|dk	r4P q4W d| f }|d7 }t||	dd}|
t|d}|r|d	 d
kr|d
7 }|S )z#Return the source code, as unicode.z.pyz.pywNzNo source for code: '%s'.
z*Aborting report output, consider using -i.       r   
)ospathsplitextr   WINDOWSexistsr   get_zip_bytesr	   r   decoder   )r   baseextextsZtry_filenamer   Zexc_msgr   r   r   get_python_source%   s(    


r+   z
bytes|Nonec          	   C   s   dt j dt j dt j g}x|D ]|}|| kr"| |}yt|d |dd  }W n tjk
rn   w"Y nX y||d }W n tk
r   w"Y nX |S q"W dS )zGet data from `filename` if it is a zip file path.

    Returns the bytestring data read from the zip file, or None if no zip file
    could be found or `filename` isn't in it.  The data returned will be
    an empty string if the file is empty.

    z.zipz.eggz.pexr   Nr      )r!   sepsplit	zipimportzipimporterZipImportErrorget_dataIOError)r   markersmarkerpartsZzidatar   r   r   r&   J   s    	

r&   c             C   st   |  dr| S |  drV| dd }tj|r4|S tjrR|d }tj|rR|S |S |  drp| dd d S | S )zReturn the source filename for `filename`.

    Given a file name being traced, return the best guess as to the source
    file to attribute it to.

    z.py)z.pycz.pyoNr   wz	$py.classi)endswithr!   r"   r%   r   r$   )r   Zpy_filenameZpyw_filenamer   r   r   source_for_filec   s    


r:   c             C   sJ   t | dr| jr| j}n t| tjr4td| n| }tt	|}|S )z6Get the source filename for the module-or-file `morf`.__file__zModule {} has no file)
hasattrr;   
isinstancetypes
ModuleTyper   formatr:   r   unicode_filename)morfr   r   r   r   source_for_morf   s    rC   c                   s   e Zd ZdZd# fdd	Zdd Zeddd	d
 Zedd Z	dd Z
dd Zdd Zdd Zedd Zedd Zedd Zd$ddZedddd Zdd  Zd!d" Z  ZS )%PythonFileReporterz!Report support for a Python file.Nc                s   || _ t|}tt| t| t|drr|j	dt
j}t
j|dr^|t
jd 7 }|d7 }t|}n
t|}|| _d | _d | _d | _d S )N__name__.z	__init__.__init__z.py)coveragerC   superrD   rG   r   Zcanonical_filenamer<   rE   r   r!   r-   r"   basename
startswithrA   relative_filenamerelname_source_parserZ	_excluded)selfrB   rH   r   name)	__class__r   r   rG      s    

zPythonFileReporter.__init__c             C   s   d | jS )Nz<PythonFileReporter {!r}>)r@   r   )rP   r   r   r   __repr__   s    zPythonFileReporter.__repr__r   )r   c             C   s   | j S )N)rM   )rP   r   r   r   rL      s    z$PythonFileReporter.relative_filenamec             C   s2   | j dkr,t| j| jdd| _ | j   | j S )z&Lazily create a :class:`PythonParser`.Nexclude)r   rT   )rO   r
   r   rH   Z_exclude_regexZparse_source)rP   r   r   r   parser   s    

zPythonFileReporter.parserc             C   s   | j jS )z2Return the line numbers of statements in the file.)rU   Z
statements)rP   r   r   r   lines   s    zPythonFileReporter.linesc             C   s   | j jS )z2Return the line numbers of statements in the file.)rU   Zexcluded)rP   r   r   r   excluded_lines   s    z!PythonFileReporter.excluded_linesc             C   s   | j |S )N)rU   translate_lines)rP   rV   r   r   r   rX      s    z"PythonFileReporter.translate_linesc             C   s   | j |S )N)rU   translate_arcs)rP   arcsr   r   r   rY      s    z!PythonFileReporter.translate_arcsc             C   s&   | j t| jjjt| jjj}|S )N)rU   Zlines_matchingr   rH   configZpartial_listZpartial_always_list)rP   Z	no_branchr   r   r   no_branch_lines   s    z"PythonFileReporter.no_branch_linesc             C   s
   | j  S )N)rU   rZ   )rP   r   r   r   rZ      s    zPythonFileReporter.arcsc             C   s
   | j  S )N)rU   exit_counts)rP   r   r   r   r]      s    zPythonFileReporter.exit_countsc             C   s   | j |||S )N)rU   missing_arc_description)rP   startendZexecuted_arcsr   r   r   r^      s    z*PythonFileReporter.missing_arc_descriptionc             C   s   | j d krt| j| _ | j S )N)rN   r+   r   )rP   r   r   r   r      s    
zPythonFileReporter.sourcec             C   s,   t j| j\}}|dr dS |s(dS dS )zDoes it seem like this file should contain Python?

        This is used to decide if a file reported as part of the execution of
        a program was really likely to have contained Python in the first
        place.

        z.pyTF)r!   r"   r#   r   rK   )rP   _r)   r   r   r   should_be_python   s    	
z#PythonFileReporter.should_be_pythonc             C   s   t |  S )N)r   r   )rP   r   r   r   r      s    z%PythonFileReporter.source_token_lines)N)N)rE   
__module____qualname____doc__rG   rS   r   rL   propertyrU   rV   rW   rX   rY   r   r\   rZ   r]   r^   r   rb   r   __classcell__r   r   )rR   r   rD      s    
rD   )re   os.pathr!   r>   r/   rH   r   r   Zcoverage.miscr   r   r   r   r   r	   Zcoverage.parserr
   Zcoverage.phystokensr   r   Zcoverage.pluginr   r   r+   r&   r:   rC   rD   r   r   r   r   <module>   s    %!