3
tûœ]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_EXTSéP   Ú 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-fn42xldr/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|ƒr�ydjtj|ƒƒ}W n$ tk
rt   tt |ƒƒd }Y nX t|ƒrŠt|dƒ}qî|}n^|}ytj	|j
dƒƒ}|dkr°w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 �oŽt |ƒdk}|�s |�s |rxjtjt|ƒ||d�D ]R}||k�rÈ�q¶tt |ƒ|ƒ�rÜ�q¶t||ƒ}dj|ƒ}|j||d||d
œƒ �q¶W qW ||fS )z�take 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 }xÐt |ƒ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 )zŒload 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 )zöremove 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   Néÿÿÿÿr�   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
ƒ r”d
| }|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|ƒƒrÈd	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



