3
^^h0                 @   s*  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mZ d dlm	Z	m
Z
mZmZmZmZmZ d dlmZmZ d dlmZ d2dd	Zd
d Zdd Zd3ddZdd Zd4ddZdd Zdd Zdd Zdd Zdd Zd5dd Z d6d!d"Z!d7d$d%Z"d&d' Z#d(d) Z$d*d+ Z%d,d- Z&d8d.d/Z'd0d1 Z(dS )9    N)lineseplistdir)basenamedirnameexistsisdirjoinnormpathsplitext)Modulemodutils)PY_EXTSP    c             C   s   dj tj| |||dS )z'Wrap the text on the given line length.
)widthinitial_indentsubsequent_indent)r   textwrapwrap)textline_lenindent r   4/tmp/pip-build-8app2_gc/pylint/pylint/utils/utils.pynormalize_text   s    r   c             C   s~   | j  }dg  }}xT|rft|tr*|j}n|jt|dd y|jj  }W q tk
rb   d}Y qX qW |j  |dj	|fS )z5return the module name and the frame id in the moduler   namez<lambda>N.)
frame
isinstancer   r   appendgetattrparentAttributeErrorreverser   )noder   moduleobjr   r   r   get_module_and_frameid   s    

r(   c             C   s   d| |t |  f S )z^Permit to get a title formatted as ReStructuredText test (underlined with a chosen character).z%s
%s
)len)title	characterr   r   r   get_rst_title-   s    r,   c       
      C   s   d}| r|t | d7 }|r4t|ddd}|d| 7 }xl|D ]d\}}}|jd}|d| 7 }|rxt|ddd}	|d	|	 7 }|r:tt||}|d
|jdd 7 }q:W |S )zFformat an options section using as a ReStructuredText formatted outputr   'O   )r   r   z%s

helpz:%s:
z  z%s
z
  Default: ``%s``
z`` z```` ``)r,   r   getstr_format_option_valuereplace)
sectionoptionsdocresultZformatted_docoptnameoptdictvaluehelp_optZformatted_helpr   r   r   get_rst_section2   s     
r<   c             O   sF   y| j |ptj f||S  tk
r@   | j tj f||S X dS )zAreturn decoded line from encoding or decode with default encodingN)decodesysgetdefaultencodingLookupError)lineencodingargskwargsr   r   r   safe_decodeF   s    rE   strictc             C   sD   yt j|ptj }W n" tk
r8   t jtj }Y nX || |S )N)codecs	getreaderr>   r?   r@   )streamrB   errorsZ
reader_clsr   r   r   decoding_streamN   s
    rK   c          
   C   s(   | j  }|j}ttj|S Q R X d S )N)rI   readlinelisttokenize)r&   rI   rL   r   r   r   tokenize_moduleV   s    
rO   c             C   s    x|D ]}|j | rdS qW dS )aI  Determines if the basename is matched in a regex blacklist

    :param str base_name: The basename of the file
    :param list black_list_re: A collection of regex patterns to match against.
        Successful matches are blacklisted.

    :returns: `True` if the basename is blacklisted, `False` otherwise.
    :rtype: bool
    TF)match)	base_nameblack_list_reZfile_patternr   r   r   _basename_in_blacklist_re\   s    


rS   c                s    fdd}t j| |dS )Nc                s   t j| |p S )N)r   Zcheck_modpath_has_init)pathparts)is_namespacer   r   _is_package_cbm   s    z*_modpath_from_file.<locals>._is_package_cb)Zis_package_cb)r   Zmodpath_from_file_with_callback)filenamerV   rW   r   )rV   r   _modpath_from_filel   s    rY   c             C   s  g }g }x| D ]}t ||kr$qtt ||r4qt|rydjtj|}W n$ tk
rt   tt |d }Y nX t|rt|d}q|}n^|}ytj	|j
d}|dkrwW n: ttfk
r } z|jd||d wW Y dd}~X nX t|}|p|j
d}	ytj|	tjd}
W n" tk
r:   d}t|}Y nX tj|
}tj|
}|sl|j||d	||d
 |jdp~|dk ot |dk}|s|s|rxjtjt|||dD ]R}||krȐqtt ||rܐqt||}dj|}|j||d||d
 qW qW ||fS )ztake a list of files/modules/packages and return the list of tuple
    (file, module name) which have to be actually checked
    r   r   z__init__.pyNfatal)keymodex)rT   FT)rT   r   ZisargZbasepathr   z	.__init____init__)Zlist_all)r   rS   r   r   r   Zmodpath_from_fileImportErrorr
   r   Zfile_from_modpathsplitSyntaxErrorr    r	   Zfile_info_from_modpathr>   rT   rV   is_directoryendswithZget_module_filesr   rY   )Zfiles_or_modulesZ
black_listrR   r7   rJ   Z	somethingmodnamefilepathr]   ZmodpartsspecrV   rb   Zhas_initZsubfilepathZmodpathZ
submodnamer   r   r   expand_modulesu   sr    





rg   c             C   s   i }xt |D ]}t|\}}||ks|dkr0q|tkr@|dksT| rtt||rytjt||}W nL tk
r~   wY q tk
r } zt	d||f t
jd W Y dd}~X qX t|dr|j|  d||< qW dS )zload all module and package in the given directory, looking for a
    'register' function in each one, used to register pylint checkers
    __pycache__r^   zProblem importing module %s: %s)fileNregister   )r   r
   r   r   r   r   Zload_module_from_file
ValueErrorr_   printr>   stderrhasattrrj   )linter	directoryZimportedrX   base	extensionr&   excr   r   r   register_plugins   s$    &

ru   c             C   sp   yt | j|jddS  tk
r(   Y nX x@| jjD ]4}x.|jD ]$}|d |kr@t |j|jddS q@W q4W |S )a   Retrieve an option defined by the given *checker* or
    by all known option providers.

    It will look in the list of all options providers
    until the given *option* will be found.
    If the option wasn't found, the *default* value will be returned.
    -_r   )r!   configr3   r#   rp   Zoptions_providersr5   )checkeroptiondefaultZproviderr5   r   r   r   get_global_option   s    r|   c                s.    fdd}|d|d|dd}| r*| |d< |S )Nc                s    rt jj |f  d S )N)r>   rn   write)rz   r8   rC   )deprecation_msgr   r   _warn_deprecated  s    z+deprecated_option.<locals>._warn_deprecatedTcallback)r/   Zhidetypeactionr   Z
deprecated	shortnamer   )r   Zopt_typeZhelp_msgr~   r   rz   r   )r~   r   deprecated_option   s    r   ,c             C   s   dd | j |D S )a%  return a list of stripped string by splitting the string given as
    argument on `sep` (',' by default). Empty string are discarded.

    >>> _splitstrip('a, b, c   ,  4,,')
    ['a', 'b', 'c', '4']
    >>> _splitstrip('a')
    ['a']
    >>> _splitstrip('a,
b,
c,')
    ['a', 'b', 'c']

    :type string: str or unicode
    :param string: a csv line

    :type sep: str or unicode
    :param sep: field separator, default to the comma (',')

    :rtype: str or unicode
    :return: the unquoted string (or the input string if it wasn't quoted)
    c             S   s   g | ]}|j  r|j  qS r   )strip).0wordr   r   r   
<listcomp>&  s    z_splitstrip.<locals>.<listcomp>)r`   )stringsepr   r   r   _splitstrip  s    r   c             C   s<   | s| S | d dkr | dd } | d dkr8| dd } | S )zremove optional quotes (simple or double) from the string

    :type string: str or unicode
    :param string: an optionally quoted string

    :rtype: str or unicode
    :return: the unquoted string (or the input string if it wasn't quoted)
    r   z"'rk   Nr   r   )r   r   r   r   _unquote)  s    	r   c             C   s   t | ttfr| S t| S )N)r   rM   tupler   )r:   r   r   r   
_check_csv;  s    r   c             C   s$   dd | j  D }ddt j| S )zreturn string as a commentc             S   s   g | ]}|j  qS r   )r   )r   rA   r   r   r   r   C  s    z_comment.<locals>.<listcomp>z# z%s# )
splitlinesr   r   )r   linesr   r   r   _commentA  s    r   c                s   t |ttfr(dj fdd|D }nlt |trLdjdd |j D }nHt|dr^|j}n6 jddkrz|rtdnd	}nt |t	r|j
 rd
| }|S )z5return the user input's value from a 'compiled' valuer   c             3   s   | ]}t  |V  qd S )N)r2   )r   item)r9   r   r   	<genexpr>J  s    z'_format_option_value.<locals>.<genexpr>c             s   s   | ]\}}d ||f V  qdS )z%s:%sNr   )r   kvr   r   r   r   L  s    rP   r   Zynyesnoz'%s')r   rM   r   r   dictitemsro   patternr0   r1   isspace)r9   r:   r   )r9   r   r2   G  s    

r2   c             C   s2   |rt t|| d t d| | d t| | dS )z.format an options section using the INI format)ri   z[%s]N)rm   r   _ini_format)rI   r4   r5   r6   r   r   r   format_sectionW  s    r   c             C   s   x|D ]\}}}t ||}|jd}|rNt|ddd}t| d t|| d n
t| d |dkrrtd| | d qt|j }tjdt|rd	d
t|  }|j	dd t|j
dD }|dd }td||f | d qW dS )z#format options using the INI formatr/   r.   z# )r   r   )ri   Nz#%s=z^([\w-]+,)+[\w-]+$z
  c             s   s   | ]}|d  V  qdS )r   Nr   )r   xr   r   r   r   p  s    z_ini_format.<locals>.<genexpr>r   rk   z%s=%sr   )r2   r0   r   rm   r1   r   rerP   r)   r   r`   )rI   r5   r8   r9   r:   r;   	separatorr   r   r   r   _  s     



r   )r   r   )N)rF   )N)NNNN)r   )N))rG   r   r>   r   rN   osr   r   os.pathr   r   r   r   r   r	   r
   Zastroidr   r   Zpylint.constantsr   r   r(   r,   r<   rE   rK   rO   rS   rY   rg   ru   r|   r   r   r   r   r   r2   r   r   r   r   r   r   <module>   s8   $
	

	V



