U
    Pe                     @   s   d Z ddlZddlZddlmZ G dd deZdd Zdd	 Zd
d ZG dd dej	j
ZG dd dej	jZG dd dej	jZdd ZdS )as  Kivy coverage plugin
=======================

This provides a coverage plugin to measure code execution in kv files. To use,
create and add::

    [run]
    plugins =
        kivy.tools.coverage

to the ``.coveragerc`` file in the root of your project. Or::

    [coverage:run]
    plugins =
        kivy.tools.coverage

in ``setup.cfg``.

Then you can test your project with e.g. ``pip install coverage`` followed by
``coverage run --source=./ kivy_app.py`` and ``coverage report``.

Or to use with pytest, ``pip install pytest-cov`` followed by something like
``pytest --cov=./ .``

TODO: Expand kv statements measured.

Currently, it ignores rules such as Widget creation or graphics object
creation from being measured. Similarly for import statements.

KV code created as strings within a python file is also not measured. To
support the above, deeper changes will be required.
    N)Parserc                   @   s   e Zd Zdd Zdd ZdS )CoverageKVParserc                 C   s   d S N selfr   r   7/tmp/pip-unpacked-wheel-xzebddm3/kivy/tools/coverage.pyexecute_directives)   s    z#CoverageKVParser.execute_directivesc                 C   sL   t  }t| D ]8}t|j |jd dD ]\}}| r*|| q*q|S )N   )start)setwalk_parser	enumeratevalue
splitlineslinestripadd)r   linesZparser_propline_numr   r   r   r   get_coverage_lines-   s    z#CoverageKVParser.get_coverage_linesN)__name__
__module____qualname__r	   r   r   r   r   r   r   '   s   r   c                 c   s   | V  | j D ]}t|D ]
}|V  qq| jd k	rNt| jD ]
}|V  q:| jV  | jd k	rnt| jD ]
}|V  qb| jd k	rt| jD ]
}|V  qd S r   )childrenwalk_parser_rulesZcanvas_beforeZcanvas_rootZcanvas_after)parser_rulechildruler   r   r   r   9   s    




r   c                 c   s,   | j  D ]
}|V  q
| jD ]
}|V  qd S r   )Z
propertiesvalueshandlers)r   r   r   r   r   walk_parser_rules_propertiesL   s    
r!   c                 c   sd   | j d k	r.t| j D ]}t|D ]
}|V  q q| jD ]*\}}t|D ]}t|D ]
}|V  qPqDq4d S r   )rootr   r!   rules)parserr   prop_Zcls_ruler   r   r   r   S   s    

r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )KivyCoveragePluginc                 C   s   | drt|dS d S )N.kvfilename)endswithKivyFileTracerr   r*   r   r   r   file_tracera   s    

zKivyCoveragePlugin.file_tracerc                 C   s
   t |dS )Nr)   )KivyFileReporterr-   r   r   r   file_reporterf   s    z KivyCoveragePlugin.file_reporterc                 c   s>   t |D ].\}}}|D ]}|drt j||V  qq
d S )Nr(   )oswalkr+   pathjoin)r   src_dirdirpathdirnames	filenamesr*   r   r   r   find_executable_filesi   s    
z(KivyCoveragePlugin.find_executable_filesN)r   r   r   r.   r0   r9   r   r   r   r   r'   _   s   r'   c                       s(   e Zd ZdZ fddZdd Z  ZS )r,    c                    s   t t| jf | || _d S r   )superr,   __init__r*   )r   r*   kwargs	__class__r   r   r<   t   s    zKivyFileTracer.__init__c                 C   s   | j S r   r)   r   r   r   r   source_filenamex   s    zKivyFileTracer.source_filename)r   r   r   r*   r<   r@   __classcell__r   r   r>   r   r,   p   s   r,   c                   @   s   e Zd Zdd ZdS )r/   c              	   C   s4   t | j}| }W 5 Q R X t|| jd}| S )N)contentr*   )openr*   readr   r   )r   fhsourcer$   r   r   r   r   ~   s    zKivyFileReporter.linesN)r   r   r   r   r   r   r   r   r/   |   s   r/   c                 C   s   |  t  d S r   )Zadd_file_tracerr'   )regoptionsr   r   r   coverage_init   s    rI   )__doc__r1   ZcoverageZkivy.lang.parserr   r   r   r!   r   ZpluginZCoveragePluginr'   Z
FileTracerr,   ZFileReporterr/   rI   r   r   r   r   <module>   s   !
