B
    `7                 @   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	m
Z
 ddlmZ ddlmZmZmZmZ eeZdd Zdd	 Zed
ddd Zed
ddd ZdZed
d
ddd Ze
jri Zi Zdd Zndd Ze
jred
ddd Zned
d
ddd Zed
ddd Zdd Zda da!e  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(d1d+d,Z)G d-d. d.e$Z*d/d0 Z+dS )2zFile wrangling.    N)env)unicode_class)contractCoverageException
join_regexisolate_modulec               C   s    t jtt jt j ai adS )z?Set the directory that `relative_filename` will be relative to.N)ospathnormcaseabs_filecurdirsepRELATIVE_DIRCANONICAL_FILENAME_CACHE r   r   V/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/coverage/files.pyset_relative_directory   s    r   c               C   s   t S )z=Return the directory that `relative_filename` is relative to.)r   r   r   r   r   relative_directory#   s    r   unicode)returnsc             C   s.   t j| }|tr&| ttd } t| S )zReturn the relative form of `filename`.

    The file name will be relative to the current directory when the
    `set_relative_directory` was called.

    N)r   r	   r
   
startswithr   lenunicode_filename)filenameZfnormr   r   r   relative_filename(   s    
r   c          	   C   s   | t kr| }tj| s|xbtjgtj D ]P}|dkr6q(tj|| }ytj|}W n tk
rl   d}Y nX |r(|}P q(W t	|}|t | < t |  S )zzReturn a canonical file name for `filename`.

    An absolute path with no redundant components and normalized case.

    NF)
r   r   r	   isabsr   sysjoinexistsUnicodeErrorr   )r   cfr	   fr   r   r   r   canonical_filename6   s"    
r"      )r   r   c             C   sb   t | d }tdd|}t|tkr^t|d	 }|tt| d  d d | }|S )a(  A base for a flat file name to correspond to this file.

    Useful for writing files about the code where you want all the files in
    the same directory, but need to differentiate same-named files from
    different directories.

    For example, the file a/b/c.py will return 'a_b_c_py'

       z[\\/.:]_zUTF-8N)
ntpath
splitdriveresubr   MAX_FLAThashlibsha1encode	hexdigest)r   namehr   r   r   flat_rootnameR   s    "r1   c             C   s   t jrt| tr| t } | tkr.t|  S tj	
| \}}|sL| }n|sV|}nt|}|tkrpt| }n0yt|}W n tk
r   g }Y nX |t|< tj	|}x"|D ]}tj	||kr|}P qW tj	||}|t| < |S )z:Get the actual path of `path`, including the correct case.)r   PY2
isinstancer   r-   r   getfilesystemencoding_ACTUAL_PATH_CACHEr   r	   splitupperactual_path_ACTUAL_PATH_LIST_CACHElistdir	Exceptionr
   r   )r	   headtailZactpathfilesZnormtailr!   r   r   r   r8   j   s2    



r8   c             C   s   | S )z*The actual path for non-Windows platforms.r   )r   r   r   r   r8      s    c             C   s*   t | tr&t pt }| |d} | S )z'Return a Unicode version of `filename`.replace)r3   strr   r4   getdefaultencodingdecode)r   encodingr   r   r   r      s    
r   c             C   s   | S )z'Return a Unicode version of `filename`.r   )r   r   r   r   r      s    c             C   sF   yt j| } W n tk
r$   Y nX t j| } t| } t| } | S )z.Return the absolute normalized form of `path`.)r   r	   realpathr   abspathr8   r   )r	   r   r   r   r      s    r   c             C   s   t jjrtj| } | S )z:Return the string as Python would describe this file name.)r   Z
PYBEHAVIORZreport_absolute_filesr   r	   rE   )r   r   r   r   python_reported_file   s    rF   c             C   s   t | pt| S )z)Is `filename` an absolute path on any OS?)r&   r   	posixpath)r   r   r   r   isabs_anywhere   s    rH   c             C   s>   g }x4| pg D ](}| dr(|| q|t| qW |S )a"  Prepare the file patterns for use in a `FnmatchMatcher`.

    If a pattern starts with a wildcard, it is used as a pattern
    as-is.  If it does not start with a wildcard, then it is made
    absolute with the current directory.

    If `patterns` is None, an empty list is returned.

    )*?)r   appendr   )patternsZpreppedpr   r   r   prep_patterns   s    

rN   c               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )TreeMatcherzA matcher for files in a tree.

    Construct with a list of paths, either files or directories. Paths match
    with the `match` method if they are one of the files, or if they are
    somewhere in a subtree rooted at one of the directories.

    c             C   s   t || _d S )N)listpaths)selfrQ   r   r   r   __init__   s    zTreeMatcher.__init__c             C   s
   d| j  S )Nz<TreeMatcher %r>)rQ   )rR   r   r   r   __repr__   s    zTreeMatcher.__repr__c             C   s   | j S )z4A list of strings for displaying when dumping state.)rQ   )rR   r   r   r   info   s    zTreeMatcher.infoc             C   s@   x:| j D ]0}||r||kr"dS |t| tjkrdS qW dS )z1Does `fpath` indicate a file in one of our trees?TF)rQ   r   r   r   r   )rR   fpathrM   r   r   r   match   s    
zTreeMatcher.matchN)__name__
__module____qualname____doc__rS   rT   rU   rW   r   r   r   r   rO      s
   rO   c               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )ModuleMatcherz A matcher for modules in a tree.c             C   s   t || _d S )N)rP   modules)rR   Zmodule_namesr   r   r   rS      s    zModuleMatcher.__init__c             C   s
   d| j  S )Nz<ModuleMatcher %r>)r]   )rR   r   r   r   rT      s    zModuleMatcher.__repr__c             C   s   | j S )z4A list of strings for displaying when dumping state.)r]   )rR   r   r   r   rU      s    zModuleMatcher.infoc             C   sF   |sdS x8| j D ].}||r||kr*dS |t| dkrdS qW dS )z<Does `module_name` indicate a module in one of our packages?FT.)r]   r   r   )rR   module_namemr   r   r   rW      s    
zModuleMatcher.matchN)rX   rY   rZ   r[   rS   rT   rU   rW   r   r   r   r   r\      s
   r\   c               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )FnmatchMatcherz)A matcher for files by file name pattern.c             C   s    t || _t| jtjd| _d S )N)case_insensitive)rP   patsfnmatches_to_regexr   WINDOWSr(   )rR   rc   r   r   r   rS     s    
zFnmatchMatcher.__init__c             C   s
   d| j  S )Nz<FnmatchMatcher %r>)rc   )rR   r   r   r   rT     s    zFnmatchMatcher.__repr__c             C   s   | j S )z4A list of strings for displaying when dumping state.)rc   )rR   r   r   r   rU     s    zFnmatchMatcher.infoc             C   s   | j |dk	S )z1Does `fpath` match one of our file name patterns?N)r(   rW   )rR   rV   r   r   r   rW     s    zFnmatchMatcher.matchN)rX   rY   rZ   r[   rS   rT   rU   rW   r   r   r   r   ra     s
   ra   c             C   s&   t d| }|r|d}ntj}|S )z?Find the path separator used in this string, or os.sep if none.z[\\/]r   )r(   searchgroupr   r   )sZ	sep_matchZthe_sepr   r   r   r     s
    r   Fc             C   sV   dd | D }dd |D }|r.dd |D }d}|r@|t jO }t jt||d}|S )a  Convert fnmatch patterns to a compiled regex that matches any of them.

    Slashes are always converted to match either slash or backslash, for
    Windows support, even when running elsewhere.

    If `partial` is true, then the pattern will match if the target string
    starts with the pattern. Otherwise, it must match the entire string.

    Returns: a compiled regex object.  Use the .match method to compare target
    strings.

    c             s   s   | ]}t |V  qd S )N)fnmatch	translate).0patternr   r   r   	<genexpr>6  s    z%fnmatches_to_regex.<locals>.<genexpr>c             s   s   | ]}t d d|V  qdS )z\\?/z[\\\\/]N)r(   r)   )rk   regexr   r   r   rm   9  s    c             s   s   | ]}t d d|V  qdS )z\\Z(\(\?|$)z\1N)r(   r)   )rk   rn   r   r   r   rm   @  s    r   )flags)r(   
IGNORECASEcompiler   )rL   rb   partialZregexesro   Zcompiledr   r   r   rd   )  s    
rd   c               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )PathAliasesa  A collection of aliases for paths.

    When combining data files from remote machines, often the paths to source
    code are different, for example, due to OS differences, or because of
    serialized checkouts on continuous integration machines.

    A `PathAliases` object tracks a list of pattern/result pairs, and can
    map a path through those aliases to produce a unified path.

    c             C   s
   g | _ d S )N)aliases)rR   r   r   r   rS   U  s    zPathAliases.__init__c             C   s*   x$| j D ]\}}td|j| qW dS )z;Dump the important parts of the PathAliases, for debugging.z{!r} --> {!r}N)rt   printformatrl   )rR   rn   resultr   r   r   pprintX  s    zPathAliases.pprintc             C   s   t |}t|dkr|d}|dr0td|dsNt|| sNt|}||s`||7 }t|gddd}t |}|d| }| j	
||f dS )a  Add the `pattern`/`result` pair to the list of aliases.

        `pattern` is an `fnmatch`-style pattern.  `result` is a simple
        string.  When mapping paths, if a path starts with a match against
        `pattern`, then that match is replaced with `result`.  This models
        isomorphic source trees being rooted at different places on two
        different machines.

        `pattern` can't end with a wildcard component, since that would
        match an entire tree, and not just its root.

        r$   z\/rI   z$Pattern must not end with wildcards.T)rb   rr   N)r   r   rstripendswithr   r   rH   r   rd   rt   rK   )rR   rl   rw   Zpattern_seprn   Z
result_sepr   r   r   add]  s    


zPathAliases.addc             C   sX   xR| j D ]H\}}||}|r||d|}|t|t|}t|}|S qW |S )a?  Map `path` through the aliases.

        `path` is checked against all of the patterns.  The first pattern to
        match is used to replace the root of the path with the result root.
        Only one pattern is ever used.  If no patterns match, `path` is
        returned unchanged.

        The separator style in the result is made to match that of the result
        in the alias.

        Returns the mapped path.  If a mapping has happened, this is a
        canonical path.  If no mapping has happened, it is the original value
        of `path` unchanged.

        r   )rt   rW   r?   rg   r   r"   )rR   r	   rn   rw   r`   newr   r   r   map  s    
zPathAliases.mapN)rX   rY   rZ   r[   rS   rx   r{   r}   r   r   r   r   rs   J  s
   
&rs   c             c   sl   xft t| D ]T\}\}}}|dkr:d|kr:|dd= qx(|D ] }td|r@tj||V  q@W qW dS )a  Yield all of the importable Python files in `dirname`, recursively.

    To be importable, the files have to be in a directory with a __init__.py,
    except for `dirname` itself, which isn't required to have one.  The
    assumption is that `dirname` was specified directly, so the user knows
    best, but sub-directories are checked for a __init__.py to be sure we only
    find the importable files.

    r   z__init__.pyNz^[^.#~!$@%^&*()+=,]+\.pyw?$)	enumerater   walkr(   rW   r	   r   )dirnameidirpathdirnames	filenamesr   r   r   r   find_python_files  s    


r   )FF),r[   r+   ri   r&   r   os.pathrG   r(   r   Zcoverager   Zcoverage.backwardr   Zcoverage.miscr   r   r   r   r   r   r   r"   r*   r1   re   r5   r9   r8   r2   r   r   rF   r   r   rH   rN   objectrO   r\   ra   r   rd   rs   r   r   r   r   r   <module>   sP   
#

!S