3
tûœ]r  ã            
   @   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
Z
ddlZddlmZmZmZ ddlmZ ejjdƒZdejkr²ejd ZedkrÎejjeƒZnedkrÀdZnejjedƒZdd	„ Zd
d„ Zdd„ Zdd„ Zeƒ Zdeƒ  ZG dd„ de ƒZ!dd„ Z"dd„ Z#dd„ Z$dd„ Z%dd„ Z&dd„ Z'dd „ Z(ej)e*e	j+e&e$e'd!d"„ d#d"„ e(d$œ	Z,d%d&„ Z-dAd(d)„Z.d*d+„ Z/d,d-„ Z0ej1d.d/„ ƒZ2d0d1„ Z3G d2d3„ d3ej4ƒZ4G d4d5„ d5ej5ƒZ5G d6d7„ d7ej6ƒZ7G d8d9„ d9ƒZ8G d:d;„ d;ƒZ9G d<d=„ d=e8e9ƒZ:d>e
j;dfd?d@„Z<dS )BzIutilities for Pylint configuration :

* pylintrc
* pylint.d (PYLINTHOME)
é    N)ÚAnyÚDictÚTuple)Úutilsú~Z
PYLINTHOMEz	.pylint.dc             C   s&   | j tjdƒ} tjjtd| |df ƒS )NÚ_z%s%s%sz.stats)ÚreplaceÚosÚsepÚpathÚjoinÚPYLINT_HOME)Ú	base_nameZrecurs© r   ú//tmp/pip-build-fn42xldr/pylint/pylint/config.pyÚ_get_pdata_path>   s    r   c             C   sD   t | dƒ}y t|dƒ�}tj|ƒS Q R X W n tk
r>   i S X d S )Né   Úrb)r   ÚopenÚpickleÚloadÚ	Exception)ÚbaseÚ	data_fileÚstreamr   r   r   Úload_resultsC   s    
r   c             C   s´   t jjtƒsByt jtƒ W n& tk
r@   tdt tjd� Y nX t	|dƒ}y&t
|dƒ�}tj| |ƒ W d Q R X W n< ttfk
r® } ztd||f tjd� W Y d d }~X nX d S )NzUnable to create directory %s)Úfiler   ÚwbzUnable to create file %s: %s)r	   r   Úexistsr   ÚmkdirÚOSErrorÚprintÚsysÚstderrr   r   r   ÚdumpÚIOError)Úresultsr   r   r   Úexr   r   r   Úsave_resultsL   s    
r(   c              C   s`  t jjdƒrt jjdƒS t jjdƒr0t jjdƒS t jjdƒrÆt jjt jƒ ƒ} xxt jjt jj| dƒƒrÄt jjt jj| dƒƒ} t jjt jj| dƒƒržt jj| dƒS t jjt jj| dƒƒrNt jj| dƒS qNW dt jkrît jjt jd ƒrît jd }nHt jjdƒ}|dk�r
d}n,t jj|dƒ}t jj|ƒ�s6t jj|ddƒ}t jj|ƒ�s\t jjd	ƒ�rXd	}nd
}|S )zKsearch the pylint rc file and return its path if it find it, else None
    Úpylintrcz	.pylintrcz__init__.pyz..ÚPYLINTRCr   ú/rootz.configz/etc/pylintrcN)r   r+   )	r	   r   r   ÚabspathÚisfileÚgetcwdr   ÚenvironÚ
expanduser)Úcurdirr)   Z	user_homer   r   r   Úfind_pylintrcZ   s2    
r2   af  
The following environment variables are used:
    * PYLINTHOME
    Path to the directory where persistent data for the run will be stored. If
not found, it defaults to ~/.pylint.d/ or .pylint.d (in the current working
directory).
    * PYLINTRC
    Path to the configuration file. See the documentation for the method used
to search for configuration file.
c               @   s   e Zd ZdZdS )ÚUnsupportedActionzBraised by set_option when it doesn't know what to do for an actionN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r3   �   s   r3   c             C   s<   t j|ƒ}x,|D ]$}|| krd}tj|||| f ƒ‚qW |S )Nz-option %s: invalid value: %r, should be in %s)r   Ú
_check_csvÚoptparseÚOptionValueError)ÚchoicesÚnameÚvalueÚvaluesZ	csv_valueÚmsgr   r   r   Ú_multiple_choice_validator‘   s    

r@   c             C   s$   || kr d}t j|||| f ƒ‚|S )Nz-option %s: invalid value: %r, should be in %s)r9   r:   )r;   r<   r=   r?   r   r   r   Ú_choice_validatorš   s    rA   c             C   s
   t j|ƒS )N)r   r8   )r   r<   r=   r   r   r   Ú_csv_validator¢   s    rB   c             C   s   t |dƒr|S tj|ƒS )NÚpattern)ÚhasattrÚreÚcompile)r   r<   r=   r   r   r   Ú_regexp_validator§   s    
rG   c                s   ‡ ‡fdd„t ˆ ˆ|ƒD ƒS )Nc                s   g | ]}t ˆ ˆ|ƒ‘qS r   )rG   )Ú.0Úval)r   r<   r   r   ú
<listcomp>¯   s    z)_regexp_csv_validator.<locals>.<listcomp>)rB   )r   r<   r=   r   )r   r<   r   Ú_regexp_csv_validator®   s    rK   c             C   sD   t |tƒrt|ƒS |dkrdS |d	kr*dS d}tj|| |f ƒ‚d S )
NÚyÚyesTÚnÚnoFz<option %s: invalid yn value %r, should be in (y, yes, n, no))rL   rM   )rN   rO   )Ú
isinstanceÚintÚboolr9   r:   )Úoptr   r=   r?   r   r   r   Ú_yn_validator²   s    
rT   c             C   s   |sd}t j|ƒ‚tj|ƒS )Nzindent string can't be empty.)r9   r:   r   Ú_unquote)rS   r   r=   r?   r   r   r   Ú_non_empty_string_validator½   s    
rV   c             C   s   t | d ||ƒS )Nr;   )rA   )rS   r<   r=   r   r   r   Ú<lambda>Ë   s    rW   c             C   s   t | d ||ƒS )Nr;   )r@   )rS   r<   r=   r   r   r   rW   Ì   s   )	ÚstringrQ   ÚregexpÚ
regexp_csvÚcsvÚynÚchoiceÚmultiple_choiceÚnon_empty_stringc             C   st   | t krtd|  ƒ‚yt |  |||ƒS  tk
rn   yt |  |ƒS  tk
rh   tjd||| f ƒ‚Y nX Y nX d S )NzUnsupported type "%s"z"%s value (%r) should be of type %s)Ú
VALIDATORSr   Ú	TypeErrorr9   r:   )ZopttypeÚoptdictÚoptionr=   r   r   r   Ú_call_validatorÓ   s    rd   Ú c             C   s0   y|d }W n t k
r    | S X t|||| ƒS )z‡return a validated value for an option according to its type

    optional argument name is only used for error message formatting
    Útype)ÚKeyErrorrd   )r=   rb   r<   Ú_typer   r   r   Ú	_validateá   s
    ri   c                s   ‡ fdd„| j D ƒS )Nc                s0   g | ](}t |d dƒpdˆ kr|jtjk	r|‘qS )Úlevelr   )ÚgetattrÚhelpr9   ÚSUPPRESS_HELP)rH   rc   )Úoutputlevelr   r   rJ   ï   s   z"_level_options.<locals>.<listcomp>)Úoption_list)Úgrouprn   r   )rn   r   Ú_level_optionsí   s    
rq   c             C   s°   | j dks| j r|jS |jd dd… }y| j jj| }W n tk
rT   d}Y n2X |j|ƒ}|j||ƒ}t	|j
||ƒ}tj||ƒ}|tjks–| rœ| j}|jj| jt|ƒƒS )z•Patch OptionParser.expand_default with custom behaviour

    This will handle defaults to avoid overriding values in the
    configuration file.
    Nr   é   )ÚparserÚdefault_tagrl   Ú
_long_optsÚoptions_managerÚ_all_optionsrg   Úget_option_defÚoption_attrnamerk   Úconfigr   Z_format_option_valuer9   Ú
NO_DEFAULTÚNO_DEFAULT_VALUEr   Ústr)Úselfrc   ÚoptnameÚproviderr=   rb   r   r   r   Ú_expand_defaultö   s    

r�   c              c   s(   t j} ztt j_d V  W d | t j_X d S )N)r9   ÚHelpFormatterr�   Úexpand_default)Zorig_defaultr   r   r   Ú_patch_optparse  s
    
r„   c             C   s   t | j||ƒS )N)r@   r;   )rS   r<   r=   r   r   r   Ú#_multiple_choices_validating_option  s    r…   c               @   sŠ   e Zd Zejjd Zejjddg ZejejjƒZe	ed< e
ed< eed< eed< eed< eed< d	d
„ Zdd„ Zeejjd< dd„ ZdS )ÚOptionrY   rZ   r[   r\   r^   r_   Úhiderj   c             O   s0   t jj| f|ž|Ž t| dƒr,| jr,t j| _d S )Nr‡   )r9   r†   Ú__init__rD   r‡   rm   rl   )r~   ÚoptsÚattrsr   r   r   rˆ   .  s    zOption.__init__c             C   sv   | j dkrV| jd kr tjd| ƒ‚t| jttfƒsrtjdtt | jƒƒjdƒd  | ƒ‚n| jd k	rrtjd| j  | ƒ‚d S )	Nr]   r^   z/must supply a list of choices for type 'choice'z1choices must be a list of strings ('%s' supplied)ú'r   z#must not supply choices for type %r)r]   r^   )	rf   r;   r9   ÚOptionErrorrP   ÚtupleÚlistr}   Úsplit)r~   r   r   r   Ú_check_choice3  s    


zOption._check_choicerr   c             C   sL   | j ||ƒ}| jdkr4t|| jƒ}|r4|j|ƒ |}| j| j| j||||ƒS )NÚnamed)Úconvert_valuerf   rk   ÚdestÚupdateÚtake_actionÚaction)r~   rS   r=   r>   rs   Zexistentr   r   r   ÚprocessG  s    

zOption.processN)rY   rZ   r[   r\   r^   r_   )r4   r5   r6   r9   r†   ÚTYPESÚATTRSÚcopyÚTYPE_CHECKERrG   rK   rB   rT   r…   rV   rˆ   r�   ÚCHECK_METHODSr—   r   r   r   r   r†     s&        r†   c               @   s&   e Zd Zdd„ Zddd„Zdd„ ZdS )	ÚOptionParserc             O   s    t jj| f|ždti|—Ž d S )NÚoption_class)r9   r�   rˆ   r†   )r~   rž   ÚargsÚkwargsr   r   r   rˆ   W  s    zOptionParser.__init__Nc             C   sÂ   |d kr| j }t|ddƒ}|j| ƒ g }|j|jdƒƒ |jƒ  | jrd|jtjj	| |ƒƒ |jdƒ xB| j
D ]8}|j|krl|jsŠt||ƒrl|j|j|ƒƒ |jdƒ qlW |jƒ  dj|d d… ƒS )NÚoutput_levelr   ÚOptionsÚ
re   r   éÿÿÿÿ)Ú	formatterrk   Ústore_option_stringsÚappendÚformat_headingÚindentro   r9   ÚOptionContainerÚformat_option_helpÚoption_groupsrj   Údescriptionrq   Úformat_helpÚdedentr   )r~   r¥   rn   Úresultrp   r   r   r   r«   Z  s"    


zOptionParser.format_option_helpc             C   s   || j krtj|ƒ‚|S )zDisable abbreviations.)Ú	_long_optr9   ÚBadOptionError)r~   rS   r   r   r   Ú_match_long_opto  s    

zOptionParser._match_long_opt)N)r4   r5   r6   rˆ   r«   r³   r   r   r   r   r�   V  s   
r�   c               @   st   e Zd Zddd„Zdd„ Zdd	„ Zd
d„ Zddd„Zedd„ ƒZ	edd„ ƒZ
edd„ ƒZedd„ ƒZedd„ ƒZdS )Ú_ManHelpFormatterr   é   éO   c             C   s   t jj| ||||ƒ d S )N)r9   r‚   rˆ   )r~   Úindent_incrementÚmax_help_positionÚwidthÚshort_firstr   r   r   rˆ   x  s    z_ManHelpFormatter.__init__c             C   s   d|j ƒ  S )Nz.SH %s
)Úupper)r~   Úheadingr   r   r   r¨     s    z _ManHelpFormatter.format_headingc             C   s   |S )Nr   )r~   r­   r   r   r   Úformat_description‚  s    z$_ManHelpFormatter.format_descriptionc             C   s|   y
|j }W n tk
r(   | j|ƒ}Y nX |jrl| j|ƒ}djdd„ |jƒ D ƒƒ}|jddƒ}|jddƒ}nd}d	||f S )
Nú c             S   s   g | ]}|j ƒ ‘qS r   )Ústrip)rH   Úlr   r   r   rJ   Œ  s    z3_ManHelpFormatter.format_option.<locals>.<listcomp>ú\z\\z	[current:z	[default:re   z.IP "%s"
%s
)Úoption_stringsÚAttributeErrorÚformat_option_stringsrl   rƒ   r   Ú
splitlinesr   )r~   rc   Ú	optstringÚ	help_textÚhelp_stringr   r   r   Úformat_option…  s    

z_ManHelpFormatter.format_optionr   c             C   sr   d}y|j ƒ }W n tk
r,   |jƒ }Y nX | j||jƒ}t|dƒrT| j||jƒ}d| j||ƒ|| j	|ƒ|f S )Nre   Ú	long_descz%s
%s
%s
%s)
Z_get_prog_namerÃ   Úget_prog_nameÚformat_short_descriptionr­   rD   Úformat_long_descriptionrÊ   Úformat_titleÚformat_synopsis)r~   Ú	optparserÚpkginfoÚsectionrÊ   ÚpgmÚ
short_descr   r   r   Úformat_head˜  s    

z_ManHelpFormatter.format_headc             C   s$   dt jƒ d d…  }d| ||| f S )Nz%d-%02d-%02dé   z.TH %s %s "%s" %s)ÚtimeÚ	localtime)rÓ   rÒ   Údater   r   r   rÎ   ©  s    z_ManHelpFormatter.format_titlec             C   s   d| |j ƒ f S )Nz.SH NAME
.B %s
\- %s
)r¿   )rÓ   rÔ   r   r   r   rÌ   ®  s    z*_ManHelpFormatter.format_short_descriptionc             C   s   d|  S )Nz6.SH SYNOPSIS
.B  %s
[
.I OPTIONS
] [
.I <arguments>
]
r   )rÓ   r   r   r   rÏ   ¸  s    
z!_ManHelpFormatter.format_synopsisc             C   sR   dj dd„ |jƒ D ƒƒ}|jddƒ}|jƒ j| ƒrB|t| ƒd … }d| |jƒ f S )Nr£   c             s   s   | ]}|j ƒ V  qd S )N)Úlstrip)rH   Úliner   r   r   ú	<genexpr>È  s    z<_ManHelpFormatter.format_long_description.<locals>.<genexpr>z
.
z

z.SH DESCRIPTION
.B %s
%s
)r   rÅ   r   ÚlowerÚ
startswithÚlenr¿   )rÓ   rÊ   r   r   r   rÍ   Æ  s    z)_ManHelpFormatter.format_long_descriptionc             C   s<   dt | d| jƒ| j| j| jf }t| dƒr8|d| j 7 }|S )Nz}.SH SEE ALSO
/usr/share/doc/pythonX.Y-%s/

.SH BUGS
Please report bugs on the project's mailing list:
%s

.SH AUTHOR
%s <%s>
Zdebian_nameÚ	copyrightz
.SH COPYRIGHT
%s
)rk   ÚmodnameZmailinglistÚauthorÚauthor_emailrD   rà   )rÑ   Útailr   r   r   Úformat_tailÔ  s    


z_ManHelpFormatter.format_tailN)r   rµ   r¶   r   )r   )r4   r5   r6   rˆ   r¨   r½   rÉ   rÕ   ÚstaticmethodrÎ   rÌ   rÏ   rÍ   rå   r   r   r   r   r´   w  s   


r´   c               @   s¸   e Zd ZdZd+dd„Zd,dd„Zd-d	d
„Zdd„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdf dfdd„Zd.dd„Zdd„ Zd/dd„Zdd„ Zd d!„ Zd"d#„ Zd0d$d%„Zd1d'd(„Zd2d)d*„ZdS )3ÚOptionsManagerMixInzLHandle configuration from both a configuration file and command line optionsNc             C   s@   || _ | j||d� g | _tjƒ | _i | _i | _i | _d| _	d S )N)Úversionr   )
Úconfig_fileÚreset_parsersÚoptions_providersÚcollectionsÚOrderedDictrw   Ú_short_optionsÚ_nocallback_optionsÚ	_mygroupsÚ	_maxlevel)r~   Úusageré   rè   r   r   r   rˆ   õ  s    
zOptionsManagerMixIn.__init__re   c             C   s:   t jdd�| _tt||d�| _| | j_t| jjj	ƒ| _
d S )Nú#ú;)Úinline_comment_prefixes)rò   rè   )ró   rô   )ÚconfigparserÚConfigParserÚcfgfile_parserr�   r†   Úcmdline_parserrv   Úsetrž   r™   Ú_optik_option_attrs)r~   rò   rè   r   r   r   rê     s
    
z!OptionsManagerMixIn.reset_parsersTc       
         sü   |j dkstdƒ‚xDtt| jƒƒD ]&}|j | j| j kr"| jj||ƒ P q"W | jj|ƒ dd„ |jD ƒ}t|df ƒ}|r–|r–| j	|j
jƒ |j||ƒ n$x"|D ]\}}| j|| j||ƒ qœW x<|D ]4\‰ }ˆ jƒ ‰ ‡ fdd„|jD ƒ}	| j	ˆ ||	|ƒ qÀW dS )zregister an options providerr   z!provider's priority can't be >= 0c             S   s   g | ]}d |d kr|‘qS )rp   r   r   )rH   rc   r   r   r   rJ     s    zAOptionsManagerMixIn.register_options_provider.<locals>.<listcomp>r¬   c                s(   g | ] }|d  j ddƒjƒ ˆ kr|‘qS )r   rp   re   )Úgetr»   )rH   rc   )Úgnamer   r   rJ   &  s   N)ÚpriorityÚAssertionErrorÚrangerß   rë   Úinsertr§   Úoptionsrk   Úadd_option_groupr<   r»   r7   Úadd_optik_optionrù   )
r~   r€   Ú	own_groupÚiZnon_group_spec_optionsÚgroupsrS   rb   ZgdocZgoptionsr   )rý   r   Úregister_options_provider  s*    

z-OptionsManagerMixIn.register_options_providerc             C   sŽ   || j kr| j | }nRtj| j|jƒ d�}| jj|ƒ |j|_|| j |< |dkrh|| jjkrh| jj	|ƒ x |D ]\}}| j
||||ƒ qnW d S )N)ÚtitleÚDEFAULT)rð   r9   ÚOptionGrouprù   Ú
capitalizer  rj   rø   Ú	_sectionsÚadd_sectionr  )r~   Z
group_namer   r  r€   rp   rS   rb   r   r   r   r  ,  s    

z$OptionsManagerMixIn.add_option_groupc             C   s@   | j |||ƒ\}}|j||Ž}|| j|< t| j|jp6dƒ| _d S )Nr   )Úoptik_optionÚ
add_optionrw   Úmaxrñ   rj   )r~   r€   ZoptikcontainerrS   rb   rŸ   rc   r   r   r   r  A  s    
z$OptionsManagerMixIn.add_optik_optionc             C   sÞ   t j |ƒ}d|kr|| j|< nd|d< | j|d< d|krpd|krj|jdƒdk	rj|d dkrj|d  d7  < |d= d	t|ƒ g}d
|kr¬|| j|d
 < |jd|d
  ƒ |d
= x(t|jƒ ƒD ]}|| j	krº|j
|ƒ qºW ||fS )zjget our personal option definition and return a suitable form for
        use with optik/optparse
        r–   ÚcallbackÚdefaultrl   NÚ
store_trueÚstore_falsez [current: %default]z--Úshortú-)r  r  )rš   rï   Úcb_set_provider_optionrü   r}   rî   r§   rŽ   Úkeysrû   Úpop)r~   r€   rS   rb   rŸ   Úkeyr   r   r   r  G  s(    


z OptionsManagerMixIn.optik_optionc             C   sF   |j dƒr|dd… }n| j|dd…  }|dkr6d}| j||ƒ dS )z!optik callback for option settingz--rr   Nr   )rÞ   rî   Úglobal_set_option)r~   rc   rS   r=   rs   r   r   r   r  f  s    
z*OptionsManagerMixIn.cb_set_provider_optionc             C   s   | j | j||ƒ dS )z)set option on the correct option providerN)rw   Ú
set_option)r~   rS   r=   r   r   r   r  s  s    z%OptionsManagerMixIn.global_set_optionc             C   sÎ   i }g }xv| j D ]l}xf|jƒ D ]Z\}}|dkr4|j}||kr>qdd„ |D ƒ}|sRq||krd|j|ƒ |j|g ƒ}	|	|7 }	qW qW |pˆtj}d}
x:|D ]2}|
r¨td|d� tj	||j
ƒ t|| ƒƒ d}
q”W dS )zrwrite a configuration file according to the current configuration
        into the given stream or stdout
        Nc             S   s6   g | ].\}}}|j d ƒdk	r|j dƒ r|||f‘qS )rf   NZ
deprecated)rü   )rH   rN   ÚdÚvr   r   r   rJ   „  s   z7OptionsManagerMixIn.generate_config.<locals>.<listcomp>Fr£   )r   T)rë   Úoptions_by_sectionr<   r§   Ú
setdefaultr"   Ústdoutr!   r   Zformat_sectionr»   Úsorted)r~   r   ZskipsectionsÚencodingr   Úsectionsr€   rÒ   r  Z
alloptionsZprintedr   r   r   Úgenerate_configw  s0    


z#OptionsManagerMixIn.generate_configr   c             C   s2   t ƒ �" t| j|||ptj| jd� W d Q R X d S )N)r   rj   )r„   Ú_generate_manpagerù   r"   r"  rñ   )r~   rÑ   rÒ   r   r   r   r   Úgenerate_manpage˜  s    z$OptionsManagerMixIn.generate_manpagec             C   s   x| j D ]}|jƒ  qW dS )z-initialize configuration using default valuesN)rë   Úload_defaults)r~   r€   r   r   r   Úload_provider_defaults¢  s    z*OptionsManagerMixIn.load_provider_defaultsc                s’  d}x–|ˆ j kršdjdg| ƒd }|ˆ jkr0P |f‡ fdd„	}ddjd	g| ƒ }d
||dœ}ˆ jd }ˆ j|ˆ j||ƒ | j||ff7  _|d7 }qW |dkrªˆ j}|dk	rØtj	j
|ƒ}tj	j|ƒsØtdj|ƒƒ‚|oætj	j|ƒ}	|	�rXˆ j}
tj|ddd��}|
j|ƒ W dQ R X x<t|
jjƒ ƒD ]*\}}|jƒ  �r*|�r*||
j|jƒ < �q*W |�sbdS |	�r|djtj	j|ƒƒ}nd}t|tjd� dS )zrread the configuration file but do not load it (i.e. dispatching
        values to each options provider)
        r   r  Úlongz-helpc                s   t ˆ j|ƒƒ tjdƒ d S )Nr   )r!   rl   r"   Úexit)rc   rS   rI   Úprj   )r~   r   r   Úhelpfunc±  s    z6OptionsManagerMixIn.read_config_file.<locals>.helpfuncz%s verbose help.r¾   Zmorer  )r–   r  rl   r   Nz#The config file {:s} doesn't exist!ÚrZ	utf_8_sig)r$  zUsing config file {}z1No config file found, using default configuration)r   )rñ   r   rw   rë   r  rù   r  ré   r	   r   r0   r   r%   Úformatrø   Úior   Ú	read_filerŽ   r  ÚitemsÚisupperr»   r,   r!   r"   r#   )r~   ré   ÚverboseZ	helplevelrS   r.  Zhelpmsgrb   r€   Zuse_config_filers   ÚfpÚsectr>   r?   r   )r~   r   Úread_config_file§  s@    

z$OptionsManagerMixIn.read_config_filec             C   sb   | j }xV|jƒ D ]J}xD|j|ƒD ]6\}}y| j||ƒ W q  ttjfk
rT   w Y q X q W qW dS )zddispatch values previously read from a configuration file to each
        options provider)
        N)rø   r%  r3  r  rg   r9   rŒ   )r~   rs   rÒ   rc   r=   r   r   r   Úload_config_fileØ  s    z$OptionsManagerMixIn.load_config_filec             K   s
   | j |ƒS )z4override configuration according to given parameters)Úload_configuration_from_config)r~   r    r   r   r   Úload_configurationä  s    z&OptionsManagerMixIn.load_configurationc             C   s<   x6|j ƒ D ]*\}}|jddƒ}| j| }|j||ƒ q
W d S )Nr   r  )r3  r   rw   r  )r~   rz   rS   Z	opt_valuer€   r   r   r   r:  è  s    
z2OptionsManagerMixIn.load_configuration_from_configc             C   s”   t ƒ �„ |dkr tjdd… }nt|ƒ}| jj|d�\}}xJ| jD ]@}|j}x4|jj	ƒ D ]&}t
||dƒ}|dkrrqXt|||ƒ qXW qBW |S Q R X dS )ziOverride configuration according to command line parameters

        return additional arguments
        Nr   )rŸ   )r„   r"   ÚargvrŽ   rù   Ú
parse_argsrï   rz   Ú__dict__r  rk   Úsetattr)r~   rŸ   r  r€   rz   Úattrr=   r   r   r   Úload_command_line_configurationî  s    z3OptionsManagerMixIn.load_command_line_configurationr   c             C   s:   t j| j|jƒ |d�}||_t| j|ƒ| _| jj|ƒ dS )z,add a dummy option section for help purpose )r	  r­   N)r9   r  rù   r  rj   r  rñ   r  )r~   r	  r­   rj   rp   r   r   r   Úadd_help_section  s
    z$OptionsManagerMixIn.add_help_sectionc          	   C   s&   || j j_tƒ � | j jƒ S Q R X dS )z.return the usage string for available options N)rù   r¥   r¡   r„   r®   )r~   rj   r   r   r   rl     s    
zOptionsManagerMixIn.help)NN)re   N)T)r   N)NN)N)r   )r   )r4   r5   r6   r7   rˆ   rê   r  r  r  r  r  r  r&  r(  r*  r8  r9  r;  r:  rA  rB  rl   r   r   r   r   rç   ò  s&   



 !


1

	rç   c               @   sf   e Zd ZdZdZdZf ZdZdd„ Zdd„ Z	dd
d„Z
dd„ Zddd„Zdd„ Zdd„ Zddd„Zd	S )ÚOptionsProviderMixInz-Mixin to provide options to an OptionsManagerr   r  r   c             C   s   t jƒ | _| jƒ  d S )N)r9   ÚValuesrz   r)  )r~   r   r   r   rˆ     s    
zOptionsProviderMixIn.__init__c             C   sV   xP| j D ]F\}}|jdƒ}|dkr|dkr4| j|ƒ}|jdƒ}| j||||ƒ qW dS )z,initialize the provider using default valuesr–   r  Nr  )r  rü   rx   r  )r~   rS   rb   r–   r  r   r   r   r)    s    


z"OptionsProviderMixIn.load_defaultsNc             C   s&   |dkr| j |ƒ}|jd|jddƒƒS )z-get the config attribute corresponding to optNr“   r  r   )rx   rü   r   )r~   rS   rb   r   r   r   ry   *  s    
z$OptionsProviderMixIn.option_attrnamec             C   s   t | j| j|ƒdƒS )z*get the current value for the given optionN)rk   rz   ry   )r~   rS   r   r   r   Úoption_value0  s    z!OptionsProviderMixIn.option_valuec             C   s\  |dkr| j |ƒ}|dk	r&t|||ƒ}|dkr:|jddƒ}|dkr\t| j| j||ƒ|ƒ � nü|dkr|t| j| j||ƒdƒ nÜ|dkrœt| j| j||ƒdƒ n¼|d	k�r2| j||ƒ}t| j|dƒ}|dk�rt|tt	fƒrÞ|}n|dk	rôg }|j
|ƒ t| j||ƒ n,t|t	ƒ�r&t| j|||f ƒ n
|j
|ƒ n&|d
k�rP|d
 d||dƒ nt|ƒ‚dS )z?method called to set an option (registered in the options list)Nr–   Ústorer  Úcountr   r  r   r§   r  )r  rG  )rx   ri   rü   r?  rz   ry   rk   rP   rŽ   r�   r§   r3   )r~   r   r=   r–   rb   Z_listr   r   r   r  4  s8    




zOptionsProviderMixIn.set_optionc             C   sH   | j s
t‚x"| j D ]}|d |kr|d S qW tjd|| jf |ƒ‚dS )z7return the dictionary defining an option given its namer   r   zno such option %s in section %rN)r  rÿ   r9   rŒ   r<   )r~   rS   rc   r   r   r   rx   U  s    
z#OptionsProviderMixIn.get_option_defc             c   s€   i }x6| j D ],\}}|j|jdƒg ƒj||| j|ƒfƒ qW d|krTd|jdƒfV  x&t|jƒ ƒD ]\}}|jƒ |fV  qbW dS )ztreturn an iterator on options grouped by section

        (section, [list of (optname, optdict, optvalue)])
        rp   N)	r  r!  rü   r§   rE  r  r#  r3  r»   )r~   r%  r   rb   rÒ   r  r   r   r   r   _  s    z'OptionsProviderMixIn.options_by_sectionc             c   s6   |d kr| j }x"|D ]\}}||| j|ƒfV  qW d S )N)r  rE  )r~   r  r   rb   r   r   r   Úoptions_and_valuesn  s    z'OptionsProviderMixIn.options_and_valuesr¤   )N)NN)N)r4   r5   r6   r7   rþ   r<   r  rj   rˆ   r)  ry   rE  r  rx   r   rH  r   r   r   r   rC    s   

!
rC  c               @   s   e Zd ZdZdd„ ZdS )ÚConfigurationMixInz]basic mixin for simple configurations which don't need the
    manager / providers model
    c             O   s¦   |s|j ddƒ tj| f|ž|Ž tj| ƒ t| dd ƒs”g | _xT| jD ]J\}}y|d jƒ df}W n tk
rx   wFY nX || jkrF| jj	|ƒ qFW | j
| dd� d S )Nrò   re   r¬   rp   F)r  )r!  rç   rˆ   rC  rk   r¬   r  r»   rg   r§   r  )r~   rŸ   r    r   rb   Zgdefr   r   r   rˆ   z  s    

zConfigurationMixIn.__init__N)r4   r5   r6   r7   rˆ   r   r   r   r   rI  u  s   rI  r   c             C   sP   t ƒ }||_| |_t|j| ||ƒ|d� t| j|ƒ|d� t|j|ƒ|d� d S )N)r   )r´   r¡   rs   r!   rÕ   r«   rå   )rÐ   rÑ   rÒ   r   rj   r¥   r   r   r   r'  ‹  s    r'  )re   )=r7   rì   rö   Ú
contextlibrš   r1  r9   r	   r   rE   r"   r×   Útypingr   r   r   Zpylintr   r   r0   Z	USER_HOMEr/   r   Údirnamer   r   r   r(   r2   r*   ÚglobalsZENV_HELPr   r3   r@   rA   rB   rG   rK   rT   rV   rU   rQ   rF   r`   rd   ri   rq   r�   Úcontextmanagerr„   r…   r†   r�   r‚   r´   rç   rC  rI  r"  r'  r   r   r   r   Ú<module>#   sx   

	"	
	
:!{  "c