B
    nôb\®1  ã               @   sÚ   d Z ddlmZ ddlZddlmZ ddlZddlm	Z	 ddl
mZmZmZ ddlmZmZmZmZmZmZmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddl m!Z! G dd„ deƒZ"dd„ Z#e$dkrÖe#ƒ  dS )zSpyder path manageré    )Úprint_functionN)Úgetexistingdirectory)ÚQtÚSignalÚSlot)ÚQDialogÚQDialogButtonBoxÚQHBoxLayoutÚQListWidgetÚQListWidgetItemÚQMessageBoxÚQVBoxLayoutÚ	QCheckBox)Ú_)Úgetcwd_or_home)Úicon_manager)Úcreate_toolbutton)ÚPY2c               @   sª   e Zd ZeeƒZd!dd„Zedd„ ƒZdd„ Z	d	d
„ Z
d"dd„Zeƒ d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eƒ dd„ ƒZeƒ dd „ ƒZdS )%ÚPathManagerNTc       
      C   sD  t  | |¡ |  tj¡ t|tƒs&t‚|| _|d kr8g }|| _	|d krJg }|| _
tƒ | _|  tdƒ¡ |  t d¡¡ |  dd¡ g | _tƒ }|  |¡ tƒ }| |¡ |  |¡| _t| ƒ| _| jj | j¡ | jj | j ¡ | !| j¡ tƒ }| |¡ d | _"|  #||¡| _$t%t%j&ƒ}	|	j' | j(¡ | !|	¡ |  )¡  |  ¡  d S )NzPYTHONPATH managerZ
pythonpathiô  i,  )*r   Ú__init__ZsetAttributer   ZWA_DeleteOnCloseÚ
isinstanceÚlistÚAssertionErrorÚpathlistÚnot_active_pathlistÚro_pathlistr   Ú	last_pathZsetWindowTitler   ZsetWindowIconÚimaÚiconZresizeÚselection_widgetsr   Z	setLayoutr	   Z	addLayoutÚsetup_top_toolbarZtoolbar_widgets1r
   Ú
listwidgetZcurrentRowChangedZconnectÚrefreshZitemChangedÚupdate_not_active_pathlistÚ	addWidgetÚsync_buttonÚsetup_bottom_toolbarZtoolbar_widgets2r   ZCloseZrejectedZrejectÚupdate_list)
ÚselfÚparentr   r   r   ÚsyncÚlayoutZ
top_layoutZbottom_layoutZbbox© r,   ú9lib/python3.7/site-packages/spyder/widgets/pathmanager.pyr   !   sB    





zPathManager.__init__c                s   ‡ fdd„ˆ j D ƒS )Nc                s   g | ]}|ˆ j kr|‘qS r,   )r   )Ú.0Úpath)r(   r,   r-   ú
<listcomp>W   s    z/PathManager.active_pathlist.<locals>.<listcomp>)r   )r(   r,   )r(   r-   Úactive_pathlistU   s    zPathManager.active_pathlistc             C   s(   |  tj¡ x|D ]}| |¡ qW d S )N)ZsetAlignmentr   Z	AlignLeftr$   )r(   r+   ZwidgetsÚwidgetr,   r,   r-   Ú_add_widgets_to_layoutZ   s    
z"PathManager._add_widgets_to_layoutc                sØ   g }t ˆ tdƒt d¡‡ fdd„dd}| |¡ t ˆ tdƒt d¡‡ fd	d„dd}| |¡ t ˆ td
ƒt d¡‡ fdd„dd}| |¡ t ˆ tdƒt d¡‡ fdd„dd}| |¡ ˆ j |¡ ˆ  ||¡ |S )NzMove to topZ2uparrowc                  s   ˆ j ddS )Nr   )Úabsolute)Úmove_tor,   )r(   r,   r-   Ú<lambda>d   s    z/PathManager.setup_top_toolbar.<locals>.<lambda>T)Útextr   Ú	triggeredÚtext_beside_iconzMove upZ1uparrowc                  s   ˆ j ddS )Néÿÿÿÿ)Úrelative)r5   r,   )r(   r,   r-   r6   j   s    z	Move downZ
1downarrowc                  s   ˆ j ddS )Né   )r;   )r5   r,   )r(   r,   r-   r6   p   s    zMove to bottomZ
2downarrowc                  s   ˆ j ddS )Nr<   )r4   )r5   r,   )r(   r,   r-   r6   v   s    )r   r   r   r   Úappendr   Úextendr3   )r(   r+   ÚtoolbarZmovetop_buttonZmoveup_buttonZmovedown_buttonZmovebottom_buttonr,   )r(   r-   r    _   s8    







zPathManager.setup_top_toolbarc             C   sº   g }t | tdƒt d¡| jdd}| |¡ t | tdƒt d¡| jdd}| |¡ | j |¡ |  ||¡ | 	d¡ t
jdkr¶|r¶t | td	ƒt d
¡| jtdƒdd| _| | j¡ |S )NzAdd pathZedit_addT)r7   r   r8   r9   zRemove pathZedit_remover<   ÚntzSynchronize...Z
fileimportzCSynchronize Spyder's path list with PYTHONPATH environment variable)r7   r   r8   Ztipr9   )r   r   r   r   Úadd_pathr=   Úremove_pathr   r3   Z
addStretchÚosÚnameÚsynchronizer%   r$   )r(   r+   r*   r?   Z
add_buttonZremove_buttonr,   r,   r-   r&   }   s,    





z PathManager.setup_bottom_toolbarc                sÐ   t  ˆ tdƒtdƒt jt jB t jB ¡}|t jkr4dS |t jkrDd}nd}ddlm}m}m	} |ƒ }|rtˆ j
ˆ j }n@| dg ¡}t|tƒs|g}‡ fd	d
„|D ƒ}| ˆ j
ˆ j ¡ ||d< |||ƒˆ d dS )z
        Synchronize Spyder's path list with PYTHONPATH environment variable
        Only apply to: current user, on Windows platforms
        ZSynchronizea  This will synchronize Spyder's path list with <b>PYTHONPATH</b> environment variable for current user, allowing you to run your Python modules outside Spyder without having to configure sys.path. <br>Do you want to clear contents of PYTHONPATH before adding Spyder's path list?NTFr   )Úget_user_envÚset_user_envÚlistdict2envdictZ
PYTHONPATHc                s    g | ]}|ˆ j ˆ j kr|‘qS r,   )r1   r   )r.   r/   )r(   r,   r-   r0   ³   s    z+PathManager.synchronize.<locals>.<listcomp>)r)   )r   Úquestionr   ÚYesÚNoZCancelZspyder.utils.environrF   rG   rH   r1   r   Úgetr   r   r>   )r(   ÚanswerÚremoverF   rG   rH   ÚenvZppathr,   )r(   r-   rE   –   s&    


zPathManager.synchronizec             C   s   | j S )z;Return path list (does not include the read-only path list))r   )r(   r,   r,   r-   Úget_path_list¹   s    zPathManager.get_path_listc             C   s2   |  ¡ }t| ¡ ƒdkr$|  |¡ n
|  |¡ d S )NT)r7   ÚboolZ
checkStateÚremove_from_not_active_pathlistÚadd_to_not_active_pathlist)r(   Úitemr/   r,   r,   r-   r#   ½   s    z&PathManager.update_not_active_pathlistc             C   s   || j kr| j  |¡ d S )N)r   r=   )r(   r/   r,   r,   r-   rS   Ä   s    
z&PathManager.add_to_not_active_pathlistc             C   s   || j kr| j  |¡ d S )N)r   rN   )r(   r/   r,   r,   r-   rR   È   s    
z+PathManager.remove_from_not_active_pathlistc             C   sÆ   | j  ¡  x®| j| j D ]ž}t|ƒ}| t d¡¡ || jkr^| t	j
t	jB ¡ | t	j¡ nL|| jkrŠ| | ¡ t	jB ¡ | t	j¡ n | | ¡ t	jB ¡ | t	j¡ | j  |¡ qW |  ¡  dS )zUpdate path listZDirClosedIconN)r!   Úclearr   r   r   ZsetIconr   r   ZsetFlagsr   ZNoItemFlagsZItemIsUserCheckableÚsetCheckStateÚCheckedr   ÚflagsZ	UncheckedZaddItemr"   )r(   rD   rT   r,   r,   r-   r'   Ì   s    


zPathManager.update_listc             C   sL   x"| j D ]}| | j ¡ dk	¡ qW | j ¡ dk}| jdk	rH| j |¡ dS )zRefresh widgetNr   )r   Z
setEnabledr!   ÚcurrentItemÚcountr%   )r(   Úrowr2   Z	not_emptyr,   r,   r-   r"   Þ   s
    
zPathManager.refreshc             C   s€   | j  ¡ }|d k	r,|r&t| jƒd }q4d}n|| }tdtt| jƒd |ƒƒ}| j |¡}| j ||¡ |  ¡  | j  	|¡ d S )Nr<   r   )
r!   Ú
currentRowÚlenr   ÚmaxÚminÚpopÚinsertr'   ZsetCurrentRow)r(   r4   r;   ÚindexZ	new_indexr/   r,   r,   r-   r5   æ   s    
zPathManager.move_toc             C   s\   t  | tdƒtdƒt jt jB ¡}|t jkrX| j | j ¡ ¡ |  	| j 
¡  ¡ ¡ |  ¡  d S )NzRemove pathz+Do you really want to remove selected path?)r   Úwarningr   rJ   rK   r   r`   r!   r\   rR   rY   r7   r'   )r(   rM   r,   r,   r-   rB   õ   s    
zPathManager.remove_pathc             C   s  | j  d¡ t| tdƒ| jƒ}| j  d¡ |rd}tr‚y| d¡ W n tk
r^   d}Y nX |r‚t 	| tdƒtdƒtj
¡ d S t |¡}|| _|| jkrø| j |tj¡d }| tj¡ t | tdƒtdƒtjtjB ¡}|tjkrô| j |¡ nd S | j d|¡ |  ¡  d S )	NFzSelect directoryTÚasciizAdd pathzdYou are using Python 2 and the path selected has Unicode characters. The new path will not be added.r   ziThis directory is already included in Spyder path list.<br>Do you want to move it to the top of the list?)Úredirect_stdioÚemitr   r   r   r   ÚdecodeÚUnicodeEncodeErrorr   rc   ZOkÚospÚabspathr   r!   Z	findItemsr   ZMatchExactlyrV   rW   rI   rJ   rK   rN   ra   r'   )r(   Z	directoryZ
is_unicoderT   rM   r,   r,   r-   rA      s<    




zPathManager.add_path)NNNNT)T)N)NN)Ú__name__Ú
__module__Ú__qualname__r   rQ   re   r   Úpropertyr1   r3   r    r&   r   rE   rP   r#   rS   rR   r'   r"   r5   rB   rA   r,   r,   r,   r-   r      s"    
3
#

r   c              C   sL   ddl m}  | ƒ }tdtjdd… tjdd… d}| ¡  t| ¡ ƒ dS )zRun path manager testr   )ÚqapplicationNiöÿÿÿ)r   r   )Úspyder.utils.qthelpersro   r   Úsysr/   Zexec_ÚprintrP   )ro   Z_appÚtestr,   r,   r-   rs   '  s    rs   Ú__main__)%Ú__doc__Z
__future__r   rC   Úos.pathr/   ri   rq   Zqtpy.compatr   Zqtpy.QtCorer   r   r   Zqtpy.QtWidgetsr   r   r	   r
   r   r   r   r   Zspyder.config.baser   Zspyder.utils.miscr   Zspyder.utilsr   r   rp   r   Zspyder.py3compatr   r   rs   rk   r,   r,   r,   r-   Ú<module>   s$   (  
