
    Je4S                     ,   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 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mZ ddl m!Z!m"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z(  G d d      Z) G d dee      Z*d Z+e,dk(  r e+        yy)zSpyder path manager.    )OrderedDictN)PYQT5)getexistingdirectory)QtSignalSlot)QDialogQDialogButtonBoxQHBoxLayoutQListWidgetQListWidgetItemQMessageBoxQVBoxLayoutQLabel)SpyderWidgetMixin)_)
check_pathget_system_pythonpath)get_user_envset_user_env)ima)getcwd_or_home)PANES_TOOLBAR_STYLESHEETc                   (    e Zd ZdZdZdZdZdZdZdZ	y)	PathManagerToolbuttonsmove_topmove_up	move_downmove_to_bottomadd_pathremove_pathexport_pathsN)
__name__
__module____qualname__MoveTopMoveUpMoveDownMoveToBottomAddPath
RemovePathExportPaths     O/usr/lib/python3/dist-packages/spyder/plugins/pythonpath/widgets/pathmanager.pyr   r   !   s%    GFH#LGJ Kr.   r   c                   B    e Zd ZdZ ee      Z ee      ZdZ		 	 d fd	Z
d Zd Zd Zd Zed        Zed	        Zd
 Z e       d        ZddZd ZddZd Z e       dd       Z e       dd       Zd dZd Zd Zd Zd Zd Z d Z! fdZ" fdZ# fdZ$ xZ%S )!PathManagerPath manager dialog.pythonpath_managerc                     t         rt        
 	  ||       n-t        j                   |       t	        j                   |       t        |t        t        d      f      sJ  j                  t        t                     |xs d _        |xs d _        |xs d _        t                _        d _        d _        g  _         j                  r j'                  t)                      d _        d _        d _        d _        d _        d _        d _        d _        d _        d _        g  _        g  _          jC                          _"        tG                _$        tK        tJ        jL                  tJ        jN                  z         _(         jP                  jS                  tJ        jL                         _*         jW                  tY        d              j[                  t]        j^                  d              ja                  dd        j6                  jc                  td        jf                  d	k(  xr |       ti        tY        d
            }|jk                  d       tm               } jo                   jD                  |       |jq                  d       ts               }|ju                   jH                         |jw                  |       tm               }	|	ju                  |       |	jy                  d       |	jw                  |       |	ju                   jP                          j{                  |	        jH                  j|                  j                   fd        jH                  j                  j                   fd        jP                  j                  j                   j                          jP                  j                  j                   j                          j                          y)r2   )class_parentNr-   system_pathzPYTHONPATH manager
pythonpathi  i  ntzThe paths listed below will be passed to IPython consoles and the Python language server as additional locations to search for Python modules.T      c                 $    j                         S Nrefreshxselfs    r/   <lambda>z&PathManager.__init__.<locals>.<lambda>   s    DLLN r.   c                 $    j                         S r=   r>   r@   s    r/   rC   z&PathManager.__init__.<locals>.<lambda>   s    dlln r.   )Fr   super__init__r	   r   
isinstancetupletypesetStyleSheetstrr   pathproject_pathnot_active_pathr   	last_pathoriginal_path_dictr7   	user_pathupdate_pathsr   
add_buttonremove_buttonmovetop_buttonmoveup_buttonmovedown_buttonmovebottom_buttonexport_buttonuser_headerproject_headersystem_headerheadersselection_widgets_setup_right_toolbarright_buttonsr   
listwidgetr
   OkCancelbboxbutton	button_oksetWindowTitler   setWindowIconr   iconresize
setVisibleosnamer   setWordWrapr   _add_buttons_to_layout
addStretchr   	addWidget	addLayout
addSpacing	setLayoutcurrentRowChangedconnectitemChangedacceptedacceptrejectedrejectsetup)rB   parentrL   rM   rN   syncdescriptionbuttons_layoutmiddle_layoutlayout	__class__s   `         r/   rF   zPathManager.__init__4   s.    GV&9T6*&&t&A$T
 3444 	3789JB	(.B.4"')"& 99*?*AB !"!#!%!"!!#!668%d+$%5%8%8'7'>'>&? @	))*:*=*=> 	A234388L12C%%bggo&>$?  $ %

 	% %##D$6$6G!!!$ $0/ %"'#v 	))112JK##++,DE		""4;;/		""4;;/ 	

r.   c                 4    |D ]  }|j                  |        y)z$Helper to add buttons to its layout.N)rq   )rB   widgetsr   widgets       r/   ro   z"PathManager._add_buttons_to_layout   s     	%FV$	%r.   c                      j                  t        j                  t        d       j	                  d       fd       _         j                  t        j                  t        d       j	                  d       fd       _         j                  t        j                  t        d	       j	                  d
       fd       _	         j                  t        j                  t        d       j	                  d       fd       _         j                  t        j                  t        d       j	                  d       fd       _         j                  t        j                  t        d       j	                  d       fd       _         j                  t        j                    j	                  d       j"                  t        d             _         j
                   j                   j                   j                  g _         j                   j                  g j&                  z    j$                  gz   S )zCreate top toolbar and actions.zMove path to the top2uparrowc                  (     j                  d      S )Nr   absolutemove_torB   s   r/   rC   z2PathManager._setup_right_toolbar.<locals>.<lambda>       dllAl6 r.   )textri   	triggeredzMove path up1uparrowc                  (     j                  d      S )Nrelativer   r   s   r/   rC   z2PathManager._setup_right_toolbar.<locals>.<lambda>   s    dllBl7 r.   )tipri   r   zMove path down
1downarrowc                  (     j                  d      S )Nr:   r   r   r   s   r/   rC   z2PathManager._setup_right_toolbar.<locals>.<lambda>   r   r.   zMove path to the bottom
2downarrowc                  (     j                  d      S )Nr:   r   r   r   s   r/   rC   z2PathManager._setup_right_toolbar.<locals>.<lambda>   r   r.   Add pathedit_addc                 $    j                         S r=   )r    r@   s    r/   rC   z2PathManager._setup_right_toolbar.<locals>.<lambda>   s     r.   Remove path	editclearc                 $    j                         S r=   )r!   r@   s    r/   rC   z2PathManager._setup_right_toolbar.<locals>.<lambda>   s     0 0 2 r.   
fileexportz)Export to PYTHONPATH environment variable)ri   r   r   )create_toolbuttonr   r&   r   create_iconrU   r'   rV   r(   rW   r)   rX   r*   rS   r+   rT   r,   export_pythonpathrY   r^   r   s   `r/   r_   z PathManager._setup_right_toolbar   s   "44"**)*!!*-6	 5 8
 "33")).!!!*-7	 4 9
  $55"++"#!!,/6	  6  8
 "&!7!7"//,-!!,/6	 "8 "8
 00"***!!*-/	 1 1
 "33"--- !!+.2	 4 4
 "33"..!!,/,,=>	 4 @ #'"5"5t7I7I"&"6"68N8N"P __d001""#&*&8&8%9:	
r.   c                 ~   t        |      }|j                  t        j                  d             || j                  v rQ|j                  t        j                  t        j                  z         |j                  t        j                         |S || j                  v rQ|j                  |j                         t        j                  z         |j                  t        j                         |S |j                  |j                         t        j                  z         |j                  t        j                         |S )z!Helper to create a new list item.DirClosedIcon)r   setIconr   ri   rM   setFlagsr   NoItemFlagsItemIsUserCheckablesetCheckStateCheckedrN   flags	UncheckedrB   rL   items      r/   _create_itemzPathManager._create_item   s    t$SXXo./4$$$MM"..2+A+AABrzz*  T)))MM$**,)?)??@r||,
  MM$**,)?)??@rzz*r.   c                     t        |      }|j                  t        j                         |j	                  t        j
                         |j                         }|j                  d       |j                  |       |S )z)Create a header for a given path section.T)	r   setTextAlignmentr   AlignHCenterr   ItemIsEnabledfontsetBoldsetFont)rB   r   headerr   s       r/   _create_headerzPathManager._create_header   s\     & 	0(() {{}Ttr.   c                     d}| j                   r|t        | j                        dz   z  }| j                  r|t        | j                        z  }|S )z*Maximum bottom row count that is editable.r   r:   )r[   lenrM   rZ   rQ   )rB   
bottom_rows     r/   editable_bottom_rowzPathManager.editable_bottom_row   sN     
#d//0144J#dnn--Jr.   c                 z    d}| j                   r|t        | j                        dz   z  }| j                  r|dz  }|S )z'Maximum top row count that is editable.r   r:   )r[   r   rM   rZ   )rB   top_rows     r/   editable_top_rowzPathManager.editable_top_row   sC     s4,,-11GqLGr.   c                 B   | j                   j                          | j                  j                          d| _        d| _        d| _        | j                  r| j                  t        d            | _        | j                  j                  | j                         | j                   j                  | j                         | j                  D ].  }| j                  |      }| j                   j                  |       0 | j                  r| j                  t        d            | _        | j                  j                  | j                         | j                   j                  | j                         | j                  D ].  }| j                  |      }| j                   j                  |       0 | j                  r| j                  t        d            | _        | j                  j                  | j
                         | j                   j                  | j
                         | j                  D ].  }| j                  |      }| j                   j                  |       0 | j                   j                  d       | j                         | _        | j#                          y)zPopulate list widget.NzProject path
User pathszSystem PYTHONPATHr   )ra   clearr]   r[   rZ   r\   rM   r   r   appendaddItemr   rQ   r7   setCurrentRowget_path_dictrP   r?   r   s      r/   r|   zPathManager.setup   s   "! "&"5"5a6G"HDLL 3 34OO##D$7$78)) .((.''-.
 >>#221\?CDLL 0 01OO##D$4$45 .((.''-.
 !%!4!4Q7J5K!LDLL 2 23OO##D$6$67(( .((.''-. 	%%a("&"4"4"6r.   c                 :   t        j                  | t        d      t        d      t         j                  t         j                  z  t         j
                  z        }|t         j
                  k(  ryt               }| j                         j                         D cg c]
  \  }}|s	| }}}|t         j                  k(  r|}n>|j                  dg       }t        |t              s|g}|D cg c]	  }||vs| }}||z   }t        j                  j                  |      t        j                  d<   | j!                  t#        |             | j%                  dt#        |             | j'                          t        |      |d<   t)        ||        yc c}}w c c}w )z`
        Export to PYTHONPATH environment variable
        Only apply to: current user.
        Exporta  This will export Spyder's path list to the <b>PYTHONPATH</b> environment variable for the current user, allowing you to run your Python modules outside Spyder without having to configure sys.path. <br><br>Do you want to clear the contents of PYTHONPATH before adding Spyder's path list?N
PYTHONPATHr6   r7   )r}   )r   questionr   YesNorc   r   r   itemsgetrG   listrl   pathsepjoinenvironrR   rH   set_confr|   r   )rB   answerenvkvactive_pathppathps           r/   r   zPathManager.export_pythonpath  sU    %%hK + , OOknn,{/A/AA
 ['''n
 &*%7%7%9%?%?%AGTQQqGG[__$EGGL"-EeT* %>1+)=Q>E>K'E#%::??5#9

<  	eEl3mU5\2

 KLS&) H ?s   
F%F+	F5Fc                 H   t               }t        | j                  j                               D ]q  }| j                  j	                  |      }|j                         }|| j                  vs=|| j                  v r|sN|j                         t        j                  k(  ||<   s |S )z
        Return an ordered dict with the path entries as keys and the active
        state as the value.

        If `project_path` is True, its entries are also included.
        )r   rangera   countr   r   r]   rM   
checkStater   r   )rB   rM   odictrowr   rL   s         r/   r   zPathManager.get_path_dictO  s     ..01 	>C??'',D99;D4<<'4,,,\"oo/2::=d	> r.   c                 (   g }t        | j                  j                               D ]i  }| j                  j                  |      }|j	                         }|| j
                  vs=|| j                  | j                  z   vsY|j                  |       k |S )z1Get current user path as displayed on listwidget.)	r   ra   r   r   r   r]   rM   r7   r   )rB   rQ   r   r   rL   s        r/   get_user_pathzPathManager.get_user_path`  s    	..01 	+C??'',D99;D4<<' 1 1D4D4D DE$$T*	+ r.   c                     ||| _         ||| _        ||| _        | j                  dd      }| j                   D cg c]  }|| j                  |z   vr| c}| _        yc c}w )zUpdate path attributes.Nr7   r-   )rL   rN   r7   get_confrQ   )rB   rL   rN   r7   previous_system_paths        r/   rR   zPathManager.update_pathsk  ss    DI&#2D "*D#}}]B?!YY
D,,/CCD 
 
s   Ac                    | j                   j                         }|du}| j                  D ]  }|j                  |        | j                   j	                         }g }|| j
                  k  r'|j                  | j                  | j                  g       || j                  k(  r'|j                  | j                  | j                  g       || j                  v s|| j                  kD  r=|j                  | j                  | j                  | j                  | j                  g       |D ]  }|j                  d        | j                  j                  || j                  vxr$ | j
                  |cxk  xr | j                  k  nc        | j                  j                  | j                   j                         dkD         | j                   j                  | j"                  | j%                         k7         y)zRefresh toolbar widgets.NFr   )ra   currentItemr^   
setEnabled
currentRowr   extendrU   rV   r   rX   rW   r]   rT   rY   r   rf   rP   r   )rB   current_itemenabledr   r   disable_widgetss         r/   r?   zPathManager.refreshz  s   224d*,, 	'Fg&	' oo((* $'''""D$7$79K9K#LM $***""D$:$:$($8$8$: ; 4<<'31I1I+I""$$d&8&8'')=)=?
 & 	%Fe$	% 	%%, K&&#I1I1II	

 	%%doo&;&;&=&AB 	!!##t'9'9';;	=r.   c                    |Y| j                   j                  d       t        | t        d      | j                        }| j                   j                  d       |syt        j                  |      }|| _        || j                         v r| j                  j                  |t        j                        d   }|j                  t        j                         t        j                  | t        d      t        d      t        j                   t        j"                  z        }|t        j                   k(  r| j                  j%                  | j                  j'                  |            }| j                  j)                  d|       | j                  j+                  d       n|t-        |      r8| j.                  sD| j1                  t        d	            | _        | j2                  j5                  | j.                         | j                  j'                  | j.                        dk  rG| j6                  dkD  r| j6                  dz
  }nd}| j                  j)                  || j.                         | j9                  |      }| j                  j)                  | j6                  |       | j                  j+                  | j6                         | j:                  j=                  d|       n8t        j>                  | t        d      t        d
      t        j@                        }tB        jD                  dk(  r0| jG                          | jI                          | jK                          | jM                          y)zp
        Add path to list widget.

        If `directory` is provided, the folder dialog is overridden.
        NFzSelect directoryTr   r   z\This directory is already included in the list.<br> Do you want to move it to the top of it?r:   r   zThis directory cannot be added to the path!<br><br>If you want to set a different Python interpreter, please go to <tt>Preferences > Main interpreter</tt>.darwin)'redirect_stdioemitr   r   rO   ospabspathr   ra   	findItemsr   MatchExactlyr   r   r   r   r   r   takeItemr   
insertItemr   r   rZ   r   r]   r   r   r   rQ   insertwarningrb   sysplatformactivateWindowraise_setFocusr?   )rB   	directoryr   r   
header_rows        r/   r    zPathManager.add_path  s    $$U+,T15G3H-1^^=I$$T*KK	*	"**,,??,,YHKDrzz* ))* = > +..02F (//0C0CD0IJ**1d3--a0)$'''+':':1\?'KD$LL''(8(89 ??&&t'7'781<,,q0%)%:%:Q%>
%&
OO..z/3/?/?A ((3**4+@+@$G--d.C.CD%%a3$,,jM  
  NN$ <<8#!KKMMMOr.   c                    | j                   j                         r>|sIt        j                  | t	        d      t	        d      t        j
                  t        j                  z        }|st        j
                  k(  r| j                   j                         }| j                  j                  |j                                | j                   j                  | j                   j                                t        | j                        dk(  r>| j                   j                  | j                   j                  | j                               | j                          yyy)zk
        Remove path from list widget.

        If `force` is True, the message box is overridden.
        r   z/Do you really want to remove the selected path?r   N)ra   r   r   r   r   r   r   rQ   remover   r   r   r   r   rZ   r?   )rB   forcer   r   s       r/   r!   zPathManager.remove_path  s     ??&&($,,m$GHOOknn4	6 +//1224%%diik2 (()C)C)EF t~~&!+OO,,++D,<,<=?  2 )r.   c                    | j                   j                         }||r| j                  }n| j                  }n||z   }t	        dt        | j                  |            }| j                   j                  |      }| j                   j                  ||       | j                   j                  |       | j                         | _
        | j                          y)zMove items of list widget.Nr:   )ra   r   r   r   maxminr   r   r   r   rQ   r?   )rB   r   r   index	new_indexr   s         r/   r   zPathManager.move_to  s    **, 44	 11	(I3t77CD	''.""9d3%%i0++-r.   c                 6    | j                   j                         S )z$Returns the current row of the list.)ra   r   r   s    r/   current_rowzPathManager.current_row   s    ))++r.   c                 :    | j                   j                  |       y)z Set the current row of the list.N)ra   r   )rB   r   s     r/   set_current_rowzPathManager.set_current_row$  s    %%c*r.   c                 X    | j                   j                  |      }|j                         S )z)Return the checked state for item in row.)ra   r   r   )rB   r   r   s      r/   row_check_statezPathManager.row_check_state(  s#    ##C(  r.   c                 \    | j                   j                  |      }|j                  |       y)z.Set the current checked state for item in row.N)ra   r   r   )rB   r   valuer   s       r/   set_row_check_statezPathManager.set_row_check_state-  s$    ##C(5!r.   c                 6    | j                   j                         S )zReturn the number of items.)ra   r   r   s    r/   r   zPathManager.count2  s    $$&&r.   c                     | j                   | j                  dd      k7  r)| j                  j                  | j	                                | j                  d| j                          y)zz
        Request to update path values on main window if current and previous
        system paths are different.
        r7   r-   )defaultN)r7   r   sig_path_changedr   r   r   r   s    r/   _update_system_pathzPathManager._update_system_path8  sP    
 t}}]B}GG!!&&t'9'9';<mT%5%56r.   c                     | j                         }| j                  |k7  r| j                  j                  |       t        |           y)zOverride Qt method.N)r   rP   r  r   rE   ry   )rB   	path_dictr   s     r/   ry   zPathManager.acceptA  s=    &&(	""i/!!&&y1r.   c                 B    | j                          t        | 	          y r=   )r  rE   r{   )rB   r   s    r/   r{   zPathManager.rejectH  s      "r.   c                 D    | j                          t        | 	  |       y r=   )r  rE   
closeEvent)rB   eventr   s     r/   r  zPathManager.closeEventL  s      "5!r.   )NNNT)F)NNNr=   )NN)&r#   r$   r%   __doc__r   boolr   objectr  CONF_SECTIONrF   ro   r_   r   r   propertyr   r   r|   r   r   r   r   rR   r?   r    r!   r   r  r  r  r  r   r  ry   r{   r  __classcell__)r   s   @r/   r1   r1   +   s   D\Nf~ (L7;,0Tl%
+
Z" 	 	 	 	(T 
V.' .'`"	
(=T 
VG GR 
V <&,+!
"
'7" "r.   r1   c            	      $   ddl m}   |        }t        dt        t        j
                  dd       t        t        j
                  dd             }d }|j                  j                  |       t	        j                  |j                                y)zRun path manager test.r   )qapplicationNr:   )rL   rM   c                 T    t         j                  j                  t        |              y r=   )r   stdoutwriterK   )r  s    r/   callbackztest.<locals>.callback\  s    

Y(r.   )
spyder.utils.qthelpersr'  r1   rH   r   rL   r  rv   exitexec_)r'  r   dlgr,  s       r/   testr1  Q  sj    3A
388BQ< 388BC=)C)   *HHSYY[r.   __main__)-r   collectionsr   rl   os.pathrL   r   r   qtpyr   qtpy.compatr   qtpy.QtCorer   r   r   qtpy.QtWidgetsr	   r
   r   r   r   r   r   r   spyder.api.widgets.mixinsr   spyder.config.baser   spyder.plugins.pythonpath.utilsr   r   spyder.utils.environr   r   spyder.utils.icon_managerr   spyder.utils.miscr   spyder.utils.stylesheetr   r   r1   r1  r#   r-   r.   r/   <module>r@     s     $ 	  
  , ( (1 1 1
 8   M ; ) , <! !c"', c"L$ zF r.   