
    Je"                         d 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
 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  ej*                  e      Z G d d      Z G d de      Zy)zPythopath container.    )OrderedDictN)Signal)_)PluginMainContainer)get_conf_path)get_system_pythonpath)PathManagerencodingc                       e Zd ZdZy)PythonpathActionsmanager_actionN)__name__
__module____qualname__Manager     E/usr/lib/python3/dist-packages/spyder/plugins/pythonpath/container.pyr   r      s    Gr   r   c                        e Zd Z ed      Z ed      Z eee      Z fdZ	d Z
d Zd Zd Zd Zd	 Zd
 Zd Zd ZddZ xZS )PythonpathContainerpathnot_active_pathc                 N    t        |   |i | d| _        d| _        d| _        y )Nr   )super__init__r   r   project_path)selfargskwargs	__class__s      r   r   zPythonpathContainer.__init__+   s,    $)&)	!r   c                    | j                          | j                  d| j                                t        | d      | _        | j                  j
                  j                  | j                         | j                  j                  j                  | j                         | j                  t        j                  t        d      | j                  d      | j                        | _        y )Nspyder_pythonpathT)parentsynczPYTHONPATH manager
pythonpath)icon	triggered)_load_pythonpathset_confget_spyder_pythonpathr	   path_manager_dialogsig_path_changedconnect_update_python_pathredirect_stdiosig_redirect_stdio_requestedcreate_actionr   r   r   create_iconshow_path_managerpath_manager_actionr   s    r   setupzPythonpathContainer.setup3   s     	 	)4+E+E+GH $/d#F   1199$$	&  //77--	/ $(#5#5%%"#!!,/,,	 $6 $
 r   c                      y Nr   r6   s    r   update_actionsz"PythonpathContainer.update_actionsJ   s    r   c                 8    | j                  dt                      y )Nsystem_path)r*   r   r6   s    r   on_closezPythonpathContainer.on_closeM   s    m%:%<=r   c                 V   |t         j                  d       d}nt         j                  d|        |f}| j                         }|| _        || j                  _        | j                         }| j                  d| j                                | j                  j                  ||       y)zUpdate active project path.Nz,Update Pythonpath because project was closedr   z$Add to Pythonpath project's path -> r#   )	loggerdebug_get_spyder_pythonpath_dictr   r,   r*   r+   sig_pythonpath_changedemit)r   r   old_path_dict_pnew_path_dict_ps       r   update_active_project_pathz.PythonpathContainer.update_active_project_pathS   s    <LLGHDLL?vFG7D ::< !04  - ::< 	)4+E+E+GH##((/Jr   c                    | j                   j                         sS| j                   j                  | j                  | j                  t                      | j                   j                          | j                   j                          | j                   j                          | j                   j                          | j                   j                          y)zShow path manager dialog.N)r,   	isVisibleupdate_pathsr   r   r   r7   showactivateWindowraise_setFocusr6   s    r   r4   z%PythonpathContainer.show_path_managerj   s     ''113$$11		4//1F1H
 $$**, 	  %%'  //1  '')  ))+r   c                 |    | j                         }|j                         D cg c]
  \  }}|s	| }}}|S c c}}w )zW
        Return active Spyder PYTHONPATH plus project path as a list of paths.
        )rA   items)r   	path_dictkvr   s        r   r+   z)PythonpathContainer.get_spyder_pythonpath}   s<     446	'oo/5da155 6s   
88c                    t               }| j                  dd      }t        j                  | j                        rt        | j                  dd      5 }|j                         j                         }ddd       g }D ]2  }t        j                  |      s||v r||vr"|j                  |       4 t        |      | _        	 t        j                  | j                  | j                         | j%                  d|       |r| j                  |z   | _        t        j                  | j&                        rWt        | j&                  dd      5 }|j                         j                         }ddd       t        d D              | _        yy# 1 sw Y   xY w# t        $ r(}t        j!                  t#        |             Y d}~d}~ww xY w# 1 sw Y   cxY w)	zLoad Python paths.r<   r   )defaultrzutf-8r
   Nc              3   J   K   | ]  }t        j                  |      r|  y wr9   )ospisdir).0names     r   	<genexpr>z7PythonpathContainer._load_pythonpath.<locals>.<genexpr>   s"      )=$,/IIdO *. )=s   !#)r   get_confrW   isfile	PATH_FILEopenread
splitlinesrX   appendtupler   r   
writelinesOSErrorr?   errorstrr*   NOT_ACTIVE_PATH_FILEr   )	r   r<   previous_system_pathfprevious_pathspathsr   er   s	            r   r)   z$PythonpathContainer._load_pythonpath   s    ,-  $}}]B}G ::dnn%dnncG< 7!"!4!4!67 E& 
# yy //D4KT"
# eDI	!		4>>: 	m[1 		K/DI ::d//0d//wG 81"#&&("5"5"78#( )=/ )= $=D  1E7 7,  	!LLQ  	!8 8s0   F*F GF	G%GGGc                    t        d D              }t        fdD              }|| j                  k7  s|| j                  k7  rP	 t        j                  || j
                         t        j                  || j                         || _        || _        yy# t        $ r(}t        j                  t        |             Y d}~<d}~ww xY w)a  
        Save Spyder PYTHONPATH to configuration folder and update attributes.

        `new_path_dict` is an OrderedDict that has the new paths as keys and
        the state as values. The state is `True` for active and `False` for
        inactive.
        c              3       K   | ]  }|  y wr9   r   )rY   ps     r   r[   z8PythonpathContainer._save_python_path.<locals>.<genexpr>   s     .1Q.s   c              3   ,   K   | ]  }|   s|  y wr9   r   )rY   rp   new_path_dicts     r   r[   z8PythonpathContainer._save_python_path.<locals>.<genexpr>   s        9a'4Q'7 !"  9s   N)rc   r   r   r   rd   r^   rh   re   r?   rf   rg   )r   rr   r   r   rm   s    `   r   _save_python_pathz%PythonpathContainer._save_python_path   s     ...  9=  9 9 49943G3G G%##D$..9##O$($=$=?
 DI#2D  !H  %SV$$%s   A B 	C CCc                     t               }| j                  D ]  }d||<   	 | j                  D ]  }|| j                  v||<    |S )aJ  
        Return Spyder PYTHONPATH plus project path as dictionary of paths.

        The returned ordered dictionary has the paths as keys and the state
        as values. The state is `True` for active and `False` for inactive.

        Example:
            OrderedDict([('/some/path, True), ('/some/other/path, False)])
        T)r   r   r   r   )r   rP   r   s      r   rA   z/PythonpathContainer._get_spyder_pythonpath_dict   s\      M	 %% 	#D"IdO	# II 	?D"$*>*>>IdO	? r   c                 "   | j                         }|| j                  |       | j                         }||k7  rW| j                         }t        j	                  d|        | j                  d|       | j                  j                  ||       yy)z
        Update Python path on language server and kernels.

        The new_path_dict should not include the project path.
        NzUpdate Pythonpath to r#   )rA   rs   r+   r?   r@   r*   rB   rC   )r   rr   rD   rE   pypaths        r   r/   z'PythonpathContainer._update_python_path   s     ::< $""=1 ::< o-//1FLL09:MM-v6'',,_oN	 .r   r9   )r   r   r   r   r^   rh   r   objectrB   r   r7   r:   r=   rF   r4   r+   r)   rs   rA   r/   __classcell__)r!   s   @r   r   r   $   s`    f%I():;#FF3
.>K.,&0=d30,Or   r   )__doc__collectionsr   loggingos.pathr   rW   qtpy.QtCorer   spyder.api.translationsr   !spyder.api.widgets.main_containerr   spyder.config.baser   spyder.plugins.pythonpath.utilsr   -spyder.plugins.pythonpath.widgets.pathmanagerr	   spyder.utilsr   	getLoggerr   r?   r   r   r   r   r   <module>r      sW     #    % A , A E ! 
		8	$
 XO- XOr   