B
    ê¹`Y«  ã               @   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ZddlZddl	Z	ddl
mZ ddlmZmZmZmZ ddlmZmZ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mZm Z  dd	l!m"Z" eeƒZd
Z#dZ$G dd„ deƒZ%G dd„ deƒZ&dd„ Z'dS )zSqlite coverage data.é    N)Úenv)Úget_thread_idÚiitemsÚto_bytesÚ	to_string)ÚNoDebuggingÚSimpleReprMixinÚclipped_repr)ÚPathAliases)ÚCoverageExceptionÚcontractÚfile_be_goneÚfilename_suffixÚisolate_module)Únumbits_to_numsÚnumbits_unionÚnums_to_numbits)Ú__version__é   aË  CREATE TABLE coverage_schema (
    -- One row, to record the version of the schema in this db.
    version integer
);

CREATE TABLE meta (
    -- Key-value pairs, to record metadata about the data
    key text,
    value text,
    unique (key)
    -- Keys:
    --  'has_arcs' boolean      -- Is this data recording branches?
    --  'sys_argv' text         -- The coverage command line that recorded the data.
    --  'version' text          -- The version of coverage.py that made the file.
    --  'when' text             -- Datetime when the file was created.
);

CREATE TABLE file (
    -- A row per file measured.
    id integer primary key,
    path text,
    unique (path)
);

CREATE TABLE context (
    -- A row per context measured.
    id integer primary key,
    context text,
    unique (context)
);

CREATE TABLE line_bits (
    -- If recording lines, a row per context per file executed.
    -- All of the line numbers for that file/context are in one numbits.
    file_id integer,            -- foreign key to `file`.
    context_id integer,         -- foreign key to `context`.
    numbits blob,               -- see the numbits functions in coverage.numbits
    foreign key (file_id) references file (id),
    foreign key (context_id) references context (id),
    unique (file_id, context_id)
);

CREATE TABLE arc (
    -- If recording branches, a row per context per from/to line transition executed.
    file_id integer,            -- foreign key to `file`.
    context_id integer,         -- foreign key to `context`.
    fromno integer,             -- line number jumped from.
    tono integer,               -- line number jumped to.
    foreign key (file_id) references file (id),
    foreign key (context_id) references context (id),
    unique (file_id, context_id, fromno, tono)
);

CREATE TABLE tracer (
    -- A row per file indicating the tracer used for that file.
    file_id integer primary key,
    tracer text,
    foreign key (file_id) references file (id)
);
c               @   sb  e Zd ZdZdRdd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZeZedddd„ ƒZedddd„ ƒZdSdd„Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ ZdTd+d,„Zd-d.„ ZdUd0d1„ZdVd2d3„ZdWd4d5„ZdXd6d7„Zd8d9„ Zd:d;„ Zd<d=„ Z d>d?„ Z!d@dA„ Z"dBdC„ Z#dDdE„ Z$dFdG„ Z%dHdI„ Z&dJdK„ Z'dLdM„ Z(dNdO„ Z)e*dPdQ„ ƒZ+dS )YÚCoverageDataaZ  Manages collected coverage data, including file storage.

    This class is the public supported API to the data that coverage.py
    collects during program execution.  It includes information about what code
    was executed. It does not include information from the analysis phase, to
    determine what lines could have been executed, or what lines were not
    executed.

    .. note::

        The data file is currently a SQLite database file, with a
        :ref:`documented schema <dbschema>`. The schema is subject to change
        though, so be careful about querying it directly. Use this API if you
        can to isolate yourself from changes.

    There are a number of kinds of data that can be collected:

    * **lines**: the line numbers of source lines that were executed.
      These are always available.

    * **arcs**: pairs of source and destination line numbers for transitions
      between source lines.  These are only available if branch coverage was
      used.

    * **file tracer names**: the module names of the file tracer plugins that
      handled each file in the data.

    Lines, arcs, and file tracer names are stored for each source file. File
    names in this API are case-sensitive, even on platforms with
    case-insensitive file systems.

    A data file either stores lines, or arcs, but not both.

    A data file is associated with the data when the :class:`CoverageData`
    is created, using the parameters `basename`, `suffix`, and `no_disk`. The
    base name can be queried with :meth:`base_filename`, and the actual file
    name being used is available from :meth:`data_filename`.

    To read an existing coverage.py data file, use :meth:`read`.  You can then
    access the line, arc, or file tracer data with :meth:`lines`, :meth:`arcs`,
    or :meth:`file_tracer`.

    The :meth:`has_arcs` method indicates whether arc data is available.  You
    can get a set of the files in the data with :meth:`measured_files`.  As
    with most Python containers, you can determine if there is any data at all
    by using this object as a boolean value.

    The contexts for each line in a file can be read with
    :meth:`contexts_by_lineno`.

    To limit querying to certain contexts, use :meth:`set_query_context` or
    :meth:`set_query_contexts`. These will narrow the focus of subsequent
    :meth:`lines`, :meth:`arcs`, and :meth:`contexts_by_lineno` calls. The set
    of all measured context names can be retrieved with
    :meth:`measured_contexts`.

    Most data files will be created by coverage.py itself, but you can use
    methods here to create data files if you like.  The :meth:`add_lines`,
    :meth:`add_arcs`, and :meth:`add_file_tracers` methods add data, in ways
    that are convenient for coverage.py.

    To record data for contexts, use :meth:`set_context` to set a context to
    be used for subsequent :meth:`add_lines` and :meth:`add_arcs` calls.

    To add a source file without any measured data, use :meth:`touch_file`,
    or :meth:`touch_files` for a list of such files.

    Write the data to its file with :meth:`write`.

    You can clear the data in memory with :meth:`erase`.  Two data collections
    can be combined by using :meth:`update` on one :class:`CoverageData`,
    passing it the other.

    Data in a :class:`CoverageData` can be serialized and deserialized with
    :meth:`dumps` and :meth:`loads`.

    NFc             C   sv   || _ tj |pd¡| _|| _|| _|p,tƒ | _|  	¡  i | _
i | _t ¡ | _d| _d| _d| _d| _d| _d| _dS )aV  Create a :class:`CoverageData` object to hold coverage-measured data.

        Arguments:
            basename (str): the base name of the data file, defaulting to
                ".coverage".
            suffix (str or bool): has the same meaning as the `data_suffix`
                argument to :class:`coverage.Coverage`.
            no_disk (bool): if True, keep all data in memory, and don't
                write any disk file.
            warn: a warning callback function, accepting a warning message
                argument.
            debug: a `DebugControl` object (optional)

        z	.coverageFN)Ú_no_diskÚosÚpathÚabspathÚ	_basenameÚ_suffixÚ_warnr   Ú_debugÚ_choose_filenameÚ	_file_mapÚ_dbsÚgetpidÚ_pidÚ
_have_usedÚ
_has_linesÚ	_has_arcsÚ_current_contextÚ_current_context_idÚ_query_context_ids)ÚselfÚbasenameÚsuffixZno_diskÚwarnÚdebug© r.   úX/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/coverage/sqldata.pyÚ__init__¸   s    
zCoverageData.__init__c             C   s:   | j rd| _n(| j| _t| jƒ}|r6|  jd| 7  _dS )z.Set self._filename based on inited attributes.z:memory:Ú.N)r   Ú	_filenamer   r   r   )r)   r+   r.   r.   r/   r   Ý   s    
zCoverageData._choose_filenamec             C   s>   | j r"x| j  ¡ D ]}| ¡  qW i | _ i | _d| _d| _dS )zReset our attributes.FN)r    ÚvaluesÚcloser   r#   r'   )r)   Údbr.   r.   r/   Ú_resetç   s    zCoverageData._resetc          
   C   sœ   | j  d¡r | j  d | j¡¡ t| j| j ƒ | jtƒ < }|T | t	¡ | 
dtf¡ | ddtttddƒƒfdtfd	tj ¡  d
¡fg¡ W dQ R X dS )zgCreate a db file that doesn't exist yet.

        Initializes the schema and certain metadata.
        ÚdataiozCreating data file {!r}z0insert into coverage_schema (version) values (?)z+insert into meta (key, value) values (?, ?)Zsys_argvÚargvNÚversionÚwhenz%Y-%m-%d %H:%M:%S)r   ÚshouldÚwriteÚformatr2   ÚSqliteDbr    r   ÚexecutescriptÚSCHEMAÚexecuteÚSCHEMA_VERSIONÚexecutemanyÚstrÚgetattrÚsysr   ÚdatetimeÚnowÚstrftime)r)   r5   r.   r.   r/   Ú
_create_dbñ   s    
zCoverageData._create_dbc             C   sB   | j  d¡r | j  d | j¡¡ t| j| j ƒ| jtƒ < |  ¡  dS )z0Open an existing db file, and read its metadata.r7   zOpening data file {!r}N)	r   r;   r<   r=   r2   r>   r    r   Ú_read_db)r)   r.   r.   r/   Ú_open_db  s    zCoverageData._open_dbc             C   sÐ   | j tƒ  º}y| d¡\}W n4 tk
rR } ztd | j|¡ƒ‚W dd}~X Y nX |tkrptd | j|t¡ƒ‚x.| d¡D ] }t	t
|d ƒƒ| _| j | _q|W x | d¡D ]\}}|| j|< q¬W W dQ R X dS )zARead the metadata from a database so that we are ready to use it.z#select version from coverage_schemaz:Data file {!r} doesn't seem to be a coverage data file: {}Nz;Couldn't use data file {!r}: wrong schema: {} instead of {}z-select value from meta where key = 'has_arcs'r   zselect path, id from file)r    r   Úexecute_oneÚ	Exceptionr   r=   r2   rB   rA   ÚboolÚintr%   r$   r   )r)   r5   Zschema_versionÚexcÚrowr   Úfile_idr.   r.   r/   rK     s     zCoverageData._read_dbc             C   s8   t ƒ | jkr,tj | j¡r$|  ¡  n|  ¡  | jt ƒ  S )zGet the SqliteDb object to use.)r   r    r   r   Úexistsr2   rL   rJ   )r)   r.   r.   r/   Ú_connect&  s
    
zCoverageData._connectc          	   C   sb   t ƒ | jkrtj | j¡sdS y*|  ¡ }| d¡}tt	|ƒƒS Q R X W n t
k
r\   dS X d S )NFzselect * from file limit 1)r   r    r   r   rT   r2   rU   rA   rO   Úlistr   )r)   ÚconÚrowsr.   r.   r/   Ú__nonzero__/  s    

zCoverageData.__nonzero__Úbytes)Zreturnsc          	   C   sJ   | j  d¡r | j  d | j¡¡ |  ¡ }dt t| 	¡ ƒ¡ S Q R X dS )a6  Serialize the current data to a byte string.

        The format of the serialized data is not documented. It is only
        suitable for use with :meth:`loads` in the same version of
        coverage.py.

        Returns:
            A byte string of serialized data.

        .. versionadded:: 5.0

        r7   z Dumping data from data file {!r}ó   zN)
r   r;   r<   r=   r2   rU   ÚzlibÚcompressr   Údump)r)   rW   r.   r.   r/   Údumps;  s    
zCoverageData.dumps)Údatac          	   C   s¨   | j  d¡r | j  d | j¡¡ |dd… dkrLtd |dd… t|ƒ¡ƒ‚tt 	|dd… ¡ƒ}t
| j| j ƒ | jtƒ < }| | |¡ W dQ R X |  ¡  d| _dS )	a?  Deserialize data from :meth:`dumps`

        Use with a newly-created empty :class:`CoverageData` object.  It's
        undefined what happens if the object already has data in it.

        Arguments:
            data: A byte string of serialized data produced by :meth:`dumps`.

        .. versionadded:: 5.0

        r7   z Loading data into data file {!r}Né   r[   z3Unrecognized serialization: {!r} (head of {} bytes)é(   T)r   r;   r<   r=   r2   r   Úlenr   r\   Ú
decompressr>   r    r   r?   rK   r#   )r)   r`   Úscriptr5   r.   r.   r/   ÚloadsN  s    zCoverageData.loadsc          	   C   sH   || j kr<|r<|  ¡  }| d|f¡}|j| j |< W dQ R X | j  |¡S )zGet the file id for `filename`.

        If filename is not in the database yet, add it if `add` is True.
        If `add` is not True, return None.
        z-insert or replace into file (path) values (?)N)r   rU   rA   Ú	lastrowidÚget)r)   ÚfilenameÚaddrW   Úcurr.   r.   r/   Ú_file_idh  s    

zCoverageData._file_idc          	   C   sN   |dk	st ‚|  ¡  |  ¡ (}| d|f¡}|dk	r<|d S dS W dQ R X dS )zGet the id for a context.Nz(select id from context where context = ?r   )ÚAssertionErrorÚ_start_usingrU   rM   )r)   ÚcontextrW   rR   r.   r.   r/   Ú_context_idu  s    
zCoverageData._context_idc             C   s.   | j  d¡r| j  d|f ¡ || _d| _dS )zýSet the current context for future :meth:`add_lines` etc.

        `context` is a str, the name of the context to use for the next data
        additions.  The context persists until the next :meth:`set_context`.

        .. versionadded:: 5.0

        ÚdataopzSetting context: %rN)r   r;   r<   r&   r'   )r)   ro   r.   r.   r/   Úset_context€  s    	zCoverageData.set_contextc          	   C   sR   | j pd}|  |¡}|dk	r$|| _n*|  ¡ }| d|f¡}|j| _W dQ R X dS )z4Use the _current_context to set _current_context_id.Ú Nz(insert into context (context) values (?))r&   rp   r'   rU   rA   rg   )r)   ro   Z
context_idrW   rk   r.   r.   r/   Ú_set_context_idŽ  s    


zCoverageData._set_context_idc             C   s   | j S )zLThe base filename for storing data.

        .. versionadded:: 5.0

        )r   )r)   r.   r.   r/   Úbase_filename™  s    zCoverageData.base_filenamec             C   s   | j S )zBWhere is the data stored?

        .. versionadded:: 5.0

        )r2   )r)   r.   r.   r/   Údata_filename¡  s    zCoverageData.data_filenamec       	   	   C   sâ   | j  d¡r6| j  dt|ƒtdd„ | ¡ D ƒƒf ¡ |  ¡  | jdd |sRdS |  ¡ ~}|  	¡  xnt
|ƒD ]b\}}t|ƒ}| j|dd}d	}t| ||| jf¡ƒ}|r¼t||d
 d
 ƒ}| d|| j|f¡ qnW W dQ R X dS )z Add measured line data.

        `line_data` is a dictionary mapping file names to dictionaries::

            { filename: { lineno: None, ... }, ...}

        rq   z&Adding lines: %d files, %d lines totalc             s   s   | ]}t |ƒV  qd S )N)rc   )Ú.0Úlinesr.   r.   r/   ú	<genexpr>³  s    z)CoverageData.add_lines.<locals>.<genexpr>T)rx   N)rj   zBselect numbits from line_bits where file_id = ? and context_id = ?r   zQinsert or replace into line_bits  (file_id, context_id, numbits) values (?, ?, ?))r   r;   r<   rc   Úsumr3   rn   Ú_choose_lines_or_arcsrU   rt   r   r   rl   rV   rA   r'   r   )	r)   Z	line_datarW   ri   ZlinenosZlinemaprS   ÚqueryÚexistingr.   r.   r/   Ú	add_lines©  s&    "
zCoverageData.add_linesc          	      s¶   ˆj  d¡r6ˆj  dt|ƒtdd„ | ¡ D ƒƒf ¡ ˆ ¡  ˆjdd |sRdS ˆ ¡ R}ˆ 	¡  xBt
|ƒD ]6\}}ˆj|dd‰ ‡ ‡fd	d
„|D ƒ}| d|¡ qnW W dQ R X dS )zŸAdd measured arc data.

        `arc_data` is a dictionary mapping file names to dictionaries::

            { filename: { (l1,l2): None, ... }, ...}

        rq   z$Adding arcs: %d files, %d arcs totalc             s   s   | ]}t |ƒV  qd S )N)rc   )rw   Úarcsr.   r.   r/   ry   Ó  s    z(CoverageData.add_arcs.<locals>.<genexpr>T)r   N)rj   c                s   g | ]\}}ˆ ˆj ||f‘qS r.   )r'   )rw   ÚfromnoÚtono)rS   r)   r.   r/   ú
<listcomp>Ý  s    z)CoverageData.add_arcs.<locals>.<listcomp>zQinsert or ignore into arc (file_id, context_id, fromno, tono) values (?, ?, ?, ?))r   r;   r<   rc   rz   r3   rn   r{   rU   rt   r   rl   rC   )r)   Zarc_datarW   ri   r   r`   r.   )rS   r)   r/   Úadd_arcsÉ  s    "
zCoverageData.add_arcsc          	   C   s„   |s|st ‚|r|rt ‚|r*| jr*tdƒ‚|r<| jr<tdƒ‚| js€| js€|| _|| _|  ¡ }| ddtt|ƒƒf¡ W dQ R X dS )z5Force the data file to choose between lines and arcs.z3Can't add line measurements to existing branch dataz3Can't add branch measurements to existing line dataz+insert into meta (key, value) values (?, ?)Úhas_arcsN)rm   r%   r   r$   rU   rA   rD   rP   )r)   rx   r   rW   r.   r.   r/   r{   ä  s    


z"CoverageData._choose_lines_or_arcsc          	   C   s¾   | j  d¡r"| j  dt|ƒf ¡ |s*dS |  ¡  |  ¡ z}xrt|ƒD ]f\}}|  |¡}|dkrntd|f ƒ‚|  	|¡}|r˜||kr¬td|||f ƒ‚qF|rF| 
d||f¡ qFW W dQ R X dS )zdAdd per-file plugin information.

        `file_tracers` is { filename: plugin_name, ... }

        rq   zAdding file tracers: %d filesNz3Can't add file tracer data for unmeasured file '%s'z/Conflicting file tracer name for '%s': %r vs %rz2insert into tracer (file_id, tracer) values (?, ?))r   r;   r<   rc   rn   rU   r   rl   r   Úfile_tracerrA   )r)   Zfile_tracersrW   ri   Úplugin_namerS   Zexisting_pluginr.   r.   r/   Úadd_file_tracersõ  s*    


zCoverageData.add_file_tracersrs   c             C   s   |   |g|¡ dS )zÎEnsure that `filename` appears in the data, empty if needed.

        `plugin_name` is the name of the plugin responsible for this file. It is used
        to associate the right filereporter, etc.
        N)Útouch_files)r)   ri   r†   r.   r.   r/   Ú
touch_file  s    zCoverageData.touch_filec          	   C   s€   | j  d¡r| j  d|f ¡ |  ¡  |  ¡ H | jsD| jsDtdƒ‚x,|D ]$}| j|dd |rJ|  	||i¡ qJW W dQ R X dS )zÐEnsure that `filenames` appear in the data, empty if needed.

        `plugin_name` is the name of the plugin responsible for these files. It is used
        to associate the right filereporter, etc.
        rq   zTouching %rz*Can't touch files in an empty CoverageDataT)rj   N)
r   r;   r<   rn   rU   r%   r$   r   rl   r‡   )r)   Ú	filenamesr†   ri   r.   r.   r/   rˆ     s    

zCoverageData.touch_filesc          	      s&  | j  d¡r&| j  dt|ddƒf ¡ | jr:|jr:tdƒ‚| jrN|jrNtdƒ‚ˆ pVtƒ ‰ |  ¡  | 	¡  | 
¡ ¶}| d¡}‡ fdd	„|D ƒ‰| ¡  | d
¡}dd„ |D ƒ}| ¡  | d¡}‡fdd„|D ƒ}| ¡  | d¡}‡fdd	„|D ƒ}| ¡  | d¡}‡fdd	„|D ƒ}| ¡  W dQ R X |  
¡ Ö}d|j_dd	„ | d¡D ƒ}	|	 ‡ fdd	„| d¡D ƒ¡ | ddd„ ˆ ¡ D ƒ¡ dd	„ | d¡D ƒ‰| ddd„ |D ƒ¡ dd	„ | d¡D ƒ‰i }
xVˆ ¡ D ]J}|	 |¡}| |d ¡}|dk	r||krtd!|||f ƒ‚||
|< qØW ‡‡fd"d„|D ƒ}| d¡}xB|D ]:\}}}ˆ  |¡|f}||krzt|| |ƒ}|||< qJW | ¡  |r®| jd#d$ | d%|¡ |rê| jd#d& | d'¡ | d(‡‡fd)d„| ¡ D ƒ¡ | d*‡fd+d„|
 ¡ D ƒ¡ W dQ R X |  ¡  |  	¡  dS ),zÙUpdate this data with data from several other :class:`CoverageData` instances.

        If `aliases` is provided, it's a `PathAliases` object that is used to
        re-map paths to match the local machine's.
        rq   zUpdating with data from %rr2   z???z%Can't combine arc data with line dataz%Can't combine line data with arc datazselect path from filec                s   i | ]\}ˆ   |¡|“qS r.   )Úmap)rw   r   )Úaliasesr.   r/   ú
<dictcomp>K  s    z'CoverageData.update.<locals>.<dictcomp>zselect context from contextc             S   s   g | ]
\}|‘qS r.   r.   )rw   ro   r.   r.   r/   r‚   P  s    z'CoverageData.update.<locals>.<listcomp>z›select file.path, context.context, arc.fromno, arc.tono from arc inner join file on file.id = arc.file_id inner join context on context.id = arc.context_idc                s$   g | ]\}}}}ˆ | |||f‘qS r.   r.   )rw   r   ro   r€   r   )Úfilesr.   r/   r‚   Z  s    zªselect file.path, context.context, line_bits.numbits from line_bits inner join file on file.id = line_bits.file_id inner join context on context.id = line_bits.context_idc                s    i | ]\}}}|ˆ | |f“qS r.   r.   )rw   r   ro   Únumbits)rŽ   r.   r/   r   d  s   zPselect file.path, tracer from tracer inner join file on file.id = tracer.file_idc                s   i | ]\}}|ˆ | “qS r.   r.   )rw   r   Útracer)rŽ   r.   r/   r   p  s    NZ	IMMEDIATEc             S   s   i | ]\}d |“qS )rs   r.   )rw   r   r.   r.   r/   r   z  s    c                s   i | ]\}}|ˆ   |¡“qS r.   )r‹   )rw   r   r   )rŒ   r.   r/   r   {  s   z,insert or ignore into file (path) values (?)c             s   s   | ]}|fV  qd S )Nr.   )rw   Úfiler.   r.   r/   ry   †  s    z&CoverageData.update.<locals>.<genexpr>c             S   s   i | ]\}}||“qS r.   r.   )rw   Úidr   r.   r.   r/   r   ˆ  s   zselect id, path from filez2insert or ignore into context (context) values (?)c             s   s   | ]}|fV  qd S )Nr.   )rw   ro   r.   r.   r/   ry   Ž  s    c             S   s   i | ]\}}||“qS r.   r.   )rw   r’   ro   r.   r.   r/   r     s   zselect id, context from contextrs   z/Conflicting file tracer name for '%s': %r vs %rc             3   s*   | ]"\}}}}ˆ| ˆ | ||fV  qd S )Nr.   )rw   r‘   ro   r€   r   )Úcontext_idsÚfile_idsr.   r/   ry   ©  s   T)r   zQinsert or ignore into arc (file_id, context_id, fromno, tono) values (?, ?, ?, ?))rx   zdelete from line_bitszEinsert into line_bits (file_id, context_id, numbits) values (?, ?, ?)c                s&   g | ]\\}}}ˆ| ˆ | |f‘qS r.   r.   )rw   r‘   ro   r   )r“   r”   r.   r/   r‚   Ì  s   z<insert or ignore into tracer (file_id, tracer) values (?, ?)c             3   s   | ]\}}ˆ | |fV  qd S )Nr.   )rw   ri   r   )r”   r.   r/   ry   Ò  s    )r   r;   r<   rE   r$   r%   r   r
   rn   ÚreadrU   rA   r4   rW   Zisolation_levelÚupdaterC   r3   rh   r‹   r   r{   Úitemsr6   )r)   Z
other_datarŒ   Úconnrk   Úcontextsr   rx   ZtracersZthis_tracersZ
tracer_mapr   Zthis_tracerZother_tracerZarc_rowsro   r   Úkeyr.   )rŒ   r“   r”   rŽ   r/   r–   1  s¤    







"zCoverageData.updatec             C   s®   |   ¡  | jrdS | j d¡r2| j d | j¡¡ t| jƒ |rªtj	 
| j¡\}}|d }tj	 tj	 |¡|¡}x8t |¡D ]*}| j d¡rž| j d |¡¡ t|ƒ q|W dS )z™Erase the data in this object.

        If `parallel` is true, then also deletes data files created from the
        basename by parallel-mode.

        Nr7   zErasing data file {!r}z.*zErasing parallel data file {!r})r6   r   r   r;   r<   r=   r2   r   r   r   ÚsplitÚjoinr   Úglob)r)   ÚparallelÚdata_dirÚlocalZlocaldotÚpatternri   r.   r.   r/   ÚeraseÙ  s    
zCoverageData.erasec          	   C   s   |   ¡  d| _W dQ R X dS )z"Start using an existing data file.TN)rU   r#   )r)   r.   r.   r/   r•   ï  s    
zCoverageData.readc             C   s   dS )z,Ensure the data is written to the data file.Nr.   )r)   r.   r.   r/   r<   ô  s    zCoverageData.writec             C   s@   | j t ¡ kr(|  ¡  |  ¡  t ¡ | _ | js6|  ¡  d| _dS )z+Call this before using the database at all.TN)r"   r   r!   r6   r   r#   r¢   )r)   r.   r.   r/   rn   ø  s    
zCoverageData._start_usingc             C   s
   t | jƒS )z4Does the database have arcs (True) or lines (False).)rO   r%   )r)   r.   r.   r/   r„     s    zCoverageData.has_arcsc             C   s
   t | jƒS )z*A set of all files that had been measured.)Úsetr   )r)   r.   r.   r/   Úmeasured_files  s    zCoverageData.measured_filesc          	   C   s4   |   ¡  |  ¡ }dd„ | d¡D ƒ}W dQ R X |S )zWA set of all contexts that have been measured.

        .. versionadded:: 5.0

        c             S   s   h | ]}|d  ’qS )r   r.   )rw   rR   r.   r.   r/   ú	<setcomp>  s    z1CoverageData.measured_contexts.<locals>.<setcomp>z%select distinct(context) from contextN)rn   rU   rA   )r)   rW   r™   r.   r.   r/   Úmeasured_contexts  s    
zCoverageData.measured_contextsc          	   C   sX   |   ¡  |  ¡ >}|  |¡}|dkr(dS | d|f¡}|dk	rJ|d pHdS dS Q R X dS )a  Get the plugin name of the file tracer for a file.

        Returns the name of the plugin that handles this file.  If the file was
        measured, but didn't use a plugin, then "" is returned.  If the file
        was not measured, then None is returned.

        Nz+select tracer from tracer where file_id = ?r   rs   )rn   rU   rl   rM   )r)   ri   rW   rS   rR   r.   r.   r/   r…     s    

zCoverageData.file_tracerc          	   C   sB   |   ¡  |  ¡ (}| d|f¡}dd„ | ¡ D ƒ| _W dQ R X dS )ad  Set a context for subsequent querying.

        The next :meth:`lines`, :meth:`arcs`, or :meth:`contexts_by_lineno`
        calls will be limited to only one context.  `context` is a string which
        must match a context exactly.  If it does not, no exception is raised,
        but queries will return no data.

        .. versionadded:: 5.0

        z(select id from context where context = ?c             S   s   g | ]}|d  ‘qS )r   r.   )rw   rR   r.   r.   r/   r‚   6  s    z2CoverageData.set_query_context.<locals>.<listcomp>N)rn   rU   rA   Úfetchallr(   )r)   ro   rW   rk   r.   r.   r/   Úset_query_context(  s    
zCoverageData.set_query_contextc          	   C   sd   |   ¡  |rZ|  ¡ >}d dgt|ƒ ¡}| d| |¡}dd„ | ¡ D ƒ| _W dQ R X nd| _dS )aÌ  Set a number of contexts for subsequent querying.

        The next :meth:`lines`, :meth:`arcs`, or :meth:`contexts_by_lineno`
        calls will be limited to the specified contexts.  `contexts` is a list
        of Python regular expressions.  Contexts will be matched using
        :func:`re.search <python:re.search>`.  Data will be included in query
        results if they are part of any of the contexts matched.

        .. versionadded:: 5.0

        z or zcontext regexp ?zselect id from context where c             S   s   g | ]}|d  ‘qS )r   r.   )rw   rR   r.   r.   r/   r‚   I  s    z3CoverageData.set_query_contexts.<locals>.<listcomp>N)rn   rU   rœ   rc   rA   r§   r(   )r)   r™   rW   Zcontext_clauserk   r.   r.   r/   Úset_query_contexts8  s    
 zCoverageData.set_query_contextsc          	   C   sî   |   ¡  |  ¡ r@|  |¡}|dk	r@tj |¡}tdd„ |D ƒƒS |  ¡ œ}|  |¡}|dkr`dS d}|g}| j	dk	r¢d 
dt| j	ƒ ¡}|d| d 7 }|| j	7 }t| ||¡ƒ}	tƒ }
x|	D ]}|
 t|d	 ƒ¡ q¾W t|
ƒS W dQ R X dS )
ac  Get the list of lines executed for a file.

        If the file was not measured, returns None.  A file might be measured,
        and have no lines executed, in which case an empty list is returned.

        If the file was executed, returns a list of integers, the line numbers
        executed in the file. The list is in no particular order.

        Nc             S   s   h | ]}|d kr|’qS )r   r.   )rw   Úlr.   r.   r/   r¥   \  s    z%CoverageData.lines.<locals>.<setcomp>z/select numbits from line_bits where file_id = ?z, ú?z and context_id in (ú)r   )rn   r„   r   Ú	itertoolsÚchainÚfrom_iterablerV   rU   rl   r(   rœ   rc   rA   r£   r–   r   )r)   ri   r   Z	all_linesrW   rS   r|   r`   Ú	ids_arrayZbitmapsÚnumsrR   r.   r.   r/   rx   M  s*    






zCoverageData.linesc          	   C   sŒ   |   ¡  |  ¡ r}|  |¡}|dkr(dS d}|g}| jdk	rjd dt| jƒ ¡}|d| d 7 }|| j7 }| ||¡}t|ƒS W dQ R X dS )aÆ  Get the list of arcs executed for a file.

        If the file was not measured, returns None.  A file might be measured,
        and have no arcs executed, in which case an empty list is returned.

        If the file was executed, returns a list of 2-tuples of integers. Each
        pair is a starting line number and an ending line number for a
        transition from one line to another. The list is in no particular
        order.

        Negative numbers have special meaning.  If the starting line number is
        -N, it represents an entry to the code object that starts at line N.
        If the ending ling number is -N, it's an exit from the code object that
        starts at line N.

        Nz7select distinct fromno, tono from arc where file_id = ?z, r«   z and context_id in (r¬   )rn   rU   rl   r(   rœ   rc   rA   rV   )r)   ri   rW   rS   r|   r`   r°   r   r.   r.   r/   r   o  s    



zCoverageData.arcsc          	   C   s`  t  t¡}|  ¡  |  ¡ :}|  |¡}|dkr4|S |  ¡ rÐd}|g}| jdk	r~d dt	| jƒ ¡}|d| d 7 }|| j7 }xÒ| 
||¡D ]>\}}	}
|
|| kr°||  |
¡ |
||	 krŒ||	  |
¡ qŒW n‚d}|g}| jdk	rd dt	| jƒ ¡}|d| d 7 }|| j7 }x<| 
||¡D ],\}}
x t|ƒD ]}||  |
¡ q4W q"W W dQ R X |S )	z¨Get the contexts for each line in a file.

        Returns:
            A dict mapping line numbers to a list of context names.

        .. versionadded:: 5.0

        Nztselect arc.fromno, arc.tono, context.context from arc, context where arc.file_id = ? and arc.context_id = context.idz, r«   z and arc.context_id in (r¬   zaselect l.numbits, c.context from line_bits l, context c where l.context_id = c.id and file_id = ?z and l.context_id in ()ÚcollectionsÚdefaultdictrV   rn   rU   rl   r„   r(   rœ   rc   rA   Úappendr   )r)   ri   Zlineno_contexts_maprW   rS   r|   r`   r°   r€   r   ro   r   Úlinenor.   r.   r/   Úcontexts_by_lineno  s8    	




$zCoverageData.contexts_by_linenoc          	   C   sb   t dtƒ d.}dd„ | d¡D ƒ}dd„ | d¡D ƒ}W dQ R X d	tjfd
tjfd|fd|fgS )zaOur information for `Coverage.sys_info`.

        Returns a list of (key, value) pairs.

        z:memory:)r-   c             S   s   g | ]}|d  ‘qS )r   r.   )rw   rR   r.   r.   r/   r‚   Æ  s    z)CoverageData.sys_info.<locals>.<listcomp>zpragma temp_storec             S   s   g | ]}|d  ‘qS )r   r.   )rw   rR   r.   r.   r/   r‚   Ç  s    zpragma compile_optionsNZsqlite3_versionZsqlite3_sqlite_versionZsqlite3_temp_storeZsqlite3_compile_options)r>   r   rA   Úsqlite3r9   Zsqlite_version)Úclsr5   Z
temp_storeZcompile_optionsr.   r.   r/   Úsys_info¾  s    zCoverageData.sys_info)NNFNN)F)FF)rs   )rs   )N)F),Ú__name__Ú
__module__Ú__qualname__Ú__doc__r0   r   r6   rJ   rL   rK   rU   rY   Ú__bool__r   r_   rf   rl   rp   rr   rt   ru   rv   r~   rƒ   r{   r‡   r‰   rˆ   r–   r¢   r•   r<   rn   r„   r¤   r¦   r…   r¨   r©   rx   r   r¶   Úclassmethodr¹   r.   r.   r.   r/   r   i   sP   M
%

	

 
!


 )
" /r   c               @   sd   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zddd„Z	ddd„Z
dd„ Zdd„ Zdd„ ZdS )r>   a(  A simple abstraction over a SQLite database.

    Use as a context manager, then you can use it like a
    :class:`python:sqlite3.Connection` object::

        with SqliteDb(filename, debug_control) as db:
            db.execute("insert into schema (version) values (?)", (SCHEMA_VERSION,))

    c             C   s*   |  d¡r|nd | _|| _d| _d | _d S )NÚsqlr   )r;   r-   ri   ÚnestrW   )r)   ri   r-   r.   r.   r/   r0   Û  s    zSqliteDb.__init__c             C   s¢   | j dk	rdS | j}tjrHtjrHytj | j¡}W n tk
rF   Y nX | j	rb| j	 
d | j¡¡ tj|dd| _ | j  ddt¡ |  d¡ ¡  |  d¡ ¡  dS )	z2Connect to the db and do universal initialization.NzConnecting to {!r}F)Zcheck_same_threadÚREGEXPé   zpragma journal_mode=offzpragma synchronous=off)rW   ri   r   ÚWINDOWSÚPY2r   r   ÚrelpathÚ
ValueErrorr-   r<   r=   r·   ÚconnectZcreate_functionÚ_regexprA   r4   )r)   ri   r.   r.   r/   rU   á  s    
zSqliteDb._connectc             C   s(   | j dk	r$| jdkr$| j  ¡  d| _ dS )z If needed, close the connection.Nz:memory:)rW   ri   r4   )r)   r.   r.   r/   r4     s    
zSqliteDb.closec             C   s.   | j dkr|  ¡  | j ¡  |  j d7  _ | S )Nr   ra   )rÁ   rU   rW   Ú	__enter__)r)   r.   r.   r/   rÊ     s
    

zSqliteDb.__enter__c          
   C   sv   |  j d8  _ | j dkrry| j |||¡ |  ¡  W n< tk
rp } z| jr^| j d |¡¡ ‚ W d d }~X Y nX d S )Nra   r   zEXCEPTION from __exit__: {})rÁ   rW   Ú__exit__r4   rN   r-   r<   r=   )r)   Úexc_typeÚ	exc_valueÚ	tracebackrQ   r.   r.   r/   rË     s    
zSqliteDb.__exit__r.   c             C   s  | j r,|rd |¡nd}| j  d ||¡¡ y2y| j ||¡S  tk
rZ   | j ||¡S X W n¤ tjk
r } z‚t|ƒ}y6t	| j
dƒ }d}| t|ƒ¡|kr¦d}W dQ R X W n tk
rÆ   Y nX | j rà| j  d |¡¡ td	 | j
|¡ƒ‚W dd}~X Y nX dS )
z2Same as :meth:`python:sqlite3.Connection.execute`.z
 with {!r}rs   zExecuting {!r}{}Úrbs&   !coverage.py: This is a private formatzILooks like a coverage 4.x data file. Are you mixing versions of coverage?NzEXCEPTION from execute: {}zCouldn't use data file {!r}: {})r-   r=   r<   rW   rA   rN   r·   ÚErrorrD   Úopenri   r•   rc   r   )r)   rÀ   Ú
parametersÚtailrQ   ÚmsgZbad_fileZcov4_sigr.   r.   r/   rA     s(    zSqliteDb.executec             C   sL   t |  ||¡ƒ}t|ƒdkr dS t|ƒdkr4|d S td |t|ƒ¡ƒ‚dS )a6  Execute a statement and return the one row that results.

        This is like execute(sql, parameters).fetchone(), except it is
        correct in reading the entire result set.  This will raise an
        exception if more than one row results.

        Returns a row, or None if there were no rows.
        r   Nra   z!Sql {!r} shouldn't return {} rows)rV   rA   rc   r   r=   )r)   rÀ   rÒ   rX   r.   r.   r/   rM   <  s    	zSqliteDb.execute_onec             C   s4   | j r&t|ƒ}| j  d |t|ƒ¡¡ | j ||¡S )z6Same as :meth:`python:sqlite3.Connection.executemany`.z Executing many {!r} with {} rows)r-   rV   r<   r=   rc   rW   rC   )r)   rÀ   r`   r.   r.   r/   rC   M  s    zSqliteDb.executemanyc             C   s4   | j r$| j  d t|ƒt|dƒ¡¡ | j |¡ dS )z8Same as :meth:`python:sqlite3.Connection.executescript`.z"Executing script with {} chars: {}éd   N)r-   r<   r=   rc   r	   rW   r?   )r)   re   r.   r.   r/   r?   T  s    
zSqliteDb.executescriptc             C   s   d  | j ¡ ¡S )z9Return a multi-line string, the SQL dump of the database.Ú
)rœ   rW   Ziterdump)r)   r.   r.   r/   r^   \  s    zSqliteDb.dumpN)r.   )r.   )rº   r»   r¼   r½   r0   rU   r4   rÊ   rË   rA   rM   rC   r?   r^   r.   r.   r.   r/   r>   Ñ  s   	$

r>   c             C   s   t  | |¡dk	S )zA regexp function for SQLite.N)ÚreÚsearch)Útextr¡   r.   r.   r/   rÉ   a  s    rÉ   )(r½   r²   rG   r   r­   r   r×   r·   rF   r\   Zcoverager   Zcoverage.backwardr   r   r   r   Zcoverage.debugr   r   r	   Zcoverage.filesr
   Zcoverage.miscr   r   r   r   r   Zcoverage.numbitsr   r   r   Zcoverage.versionr   rB   r@   r   r>   rÉ   r.   r.   r.   r/   Ú<module>   s:   G      n 