
    JeR#                         d 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 dd	lmZ dd
lmZ ddlmZ  G d de      Zy)z
Working Directory Plugin.
    N)Signal)SpyderPluginV2Plugins)on_plugin_availableon_plugin_teardown_)get_conf_path)WorkingDirectoryConfigPage)WorkingDirectoryContainer)ApplicationToolbars)encodingc                      e Zd ZdZdZej                  ej                  ej                  gZ	ej                  ej                  ej                  ej                  ej                  gZeZeZeZdZdZ ee      Z ee      Z	 ed        Zd Zd Zd Z  e!ej                        d	        Z" e!ej                        d
        Z# e!ej                        d        Z$ e!ej                        d        Z% e!ej                        d        Z& e!ej                        d        Z' e(ej                        d        Z) e(ej                        d        Z* e(ej                        d        Z+ e(ej                        d        Z, e(ej                        d        Z- e(ej                        d        Z.d dZ/d dZ0d Z1d Z2d Z3d Z4d Z5d Z6d Z7d Z8y)!WorkingDirectoryz+
    Working directory changer plugin.
    
workingdirFc                      t        d      S )NzWorking directoryr        H/usr/lib/python3/dist-packages/spyder/plugins/workingdirectory/plugin.pyget_namezWorkingDirectory.get_name<   s    $%%r   c                     t        d      S )Nz6Set the current working directory for various plugins.r   selfs    r   get_descriptionz WorkingDirectory.get_description@   s    IJJr   c                 $    | j                  d      S )NDirOpenIcon)create_iconr   s    r   get_iconzWorkingDirectory.get_iconC   s    ..r   c                 "     j                         }|j                  j                   j                          j                  j                  d fd	        j                         }|j	                   j                         |j                         y )Nc                 (    j                  | |      S N)chdir)pathpluginr   s     r   <lambda>z0WorkingDirectory.on_initialize.<locals>.<lambda>L   s    djjv&> r   r!   )get_containersig_current_directory_changedconnectget_command_line_optionsset_historyload_historyworking_directory)r   	containercli_optionss   `  r   on_initializezWorkingDirectory.on_initializeF   sy    &&(	//77..	0**22>	@ 335))	
r   )r$   c                     | j                         }| j                  t        j                        }|j	                  |j
                         y r!   )r&   
get_pluginr   Toolbaradd_application_toolbartoolbar)r   r-   r4   s      r   on_toolbar_availablez%WorkingDirectory.on_toolbar_availableT   s6    &&(	//'//2''	(9(9:r   c                 d    | j                  t        j                        }|j                  |        y r!   )r1   r   Preferencesregister_plugin_preferencesr   preferencess     r   on_preferences_availablez)WorkingDirectory.on_preferences_availableZ   s$    oog&9&9://5r   c                     | j                  t        j                        }|j                  j	                  | j
                         y r!   )r1   r   Editorsig_dir_openedr(   _editor_change_dirr   editors     r   on_editor_availablez$WorkingDirectory.on_editor_available_   s.    0%%d&=&=>r   c                     | j                  t        j                        }| j                  j	                  | j
                         |j                  j	                  | j                         y r!   )r1   r   Explorerr'   r(   _explorer_change_dirr>   _explorer_dir_openedr   explorers     r   on_explorer_availablez&WorkingDirectory.on_explorer_availabled   sJ    ??7#3#34**2243L3LM''(A(ABr   c                     | j                  t        j                        }| j                  j	                  |j
                         |j                  j	                  | j                         y r!   )r1   r   IPythonConsoler'   r(   $set_current_client_working_directory_ipyconsole_change_dirr   
ipyconsoles     r   on_ipyconsole_availablez(WorkingDirectory.on_ipyconsole_availablej   sP    __W%;%;<
**22;;	=0088''	)r   c                     | j                  t        j                        }|j                  j	                  | j
                         |j                  t           j	                  | j                         y r!   )	r1   r   Projectssig_project_loadedr(   _project_loadedsig_project_closedobject_project_closedr   projectss     r   on_projects_availablez&WorkingDirectory.on_projects_availables   sO    ??7#3#34##++D,@,@A##F+33D4H4HIr   c                     | j                  t        j                        }|j                  t        j
                         y r!   )r1   r   r2   remove_application_toolbarr   r   )r   r4   s     r   on_toolbar_teardownz$WorkingDirectory.on_toolbar_teardowny   s+    //'//2**00	2r   c                 d    | j                  t        j                        }|j                  |        y r!   )r1   r   r7   deregister_plugin_preferencesr9   s     r   on_preferences_teardownz(WorkingDirectory.on_preferences_teardown   s$    oog&9&9:11$7r   c                     | j                  t        j                        }|j                  j	                  | j
                         y r!   )r1   r   r=   r>   
disconnectr?   r@   s     r   on_editor_teardownz#WorkingDirectory.on_editor_teardown   s.    0(()@)@Ar   c                     | j                  t        j                        }| j                  j	                  | j
                         |j                  j	                  | j                         y r!   )r1   r   rD   r'   rb   rE   r>   rF   rG   s     r   on_explorer_teardownz%WorkingDirectory.on_explorer_teardown   sJ    ??7#3#34**55d6O6OP**4+D+DEr   c                     | j                  t        j                        }| j                  j	                  |j
                         |j                  j	                  | j                         y r!   )r1   r   rK   r'   rb   rL   rM   rN   s     r   on_ipyconsole_teardownz'WorkingDirectory.on_ipyconsole_teardown   sP    __W%;%;<
**55;;	=00;;''	)r   c                     | j                  t        j                        }|j                  j	                  | j
                         |j                  t           j	                  | j                         y r!   )	r1   r   rR   rS   rb   rT   rU   rV   rW   rX   s     r   on_projects_teardownz%WorkingDirectory.on_projects_teardown   sO    ??7#3#34##..t/C/CD##F+66t7K7KLr   Nc                    | j                  t        j                        }| j                  t        j                        }| j                  t        j                        }|r+||k7  r&|j                  |d       |j                  |d       |r||k7  r|j                  |       |r|j                          |6| j                         }|j                  |d       |r|j                  |       | j                          y)a  
        Change current working directory.

        Parameters
        ----------
        directory: str
            The new working directory to set.
        sender_plugin: spyder.api.plugins.SpyderPluginsV2
            The plugin that requested this change: Default is None.
        FemitT)force_currentN)r1   r   rD   rK   Findr"   refreshrL   refresh_search_directoryr&   save_working_directorysave_history)r   	directorysender_pluginrH   rO   findr-   s          r   r"   zWorkingDirectory.chdir   s     ??7#3#34__W%;%;<
w||,1NN95N1Yd;-:5;;IF))+$**,IOOIEO211)<r   c                 &   t        j                  | j                        rIt        j                  | j                        \  }}|D cg c]  }t        j
                  |      s| }}|S || j                         j                         }|g}|S c c}w )a  
        Load history from a text file located in Spyder configuration folder
        or use `workdir` if there are no directories saved yet.

        Parameters
        ----------
        workdir: str
            The working directory to return. Default is None.
        )ospisfileLOG_PATHr   	readlinesisdirr&   _get_init_workdir)r   workdirhistoryr	   names        r   r+   zWorkingDirectory.load_history   s     ::dmm$!++DMM:JGQ(/C399T?tCGC  ,,.@@BiG Ds   B!Bc                     | j                         j                         }	 t        j                  || j                         y# t
        $ r Y yw xY w)zU
        Save history to a text file located in Spyder configuration folder.
        N)r&   get_historyr   
writelinesry   EnvironmentError)r   r~   s     r   rr   zWorkingDirectory.save_history   sF     $$&224	7 		s    A 	AAc                 >    | j                         j                         S )z
        Get current working directory.

        Returns
        -------
        str
            Current working directory.
        )r&   get_workdirr   s    r   r   zWorkingDirectory.get_workdir   s     !!#//11r   c                 f    | j                  t        j                        }| j                  ||       y r!   )r1   r   r=   r"   )r   r#   rA   s      r   r?   z#WorkingDirectory._editor_change_dir   s"    0

4 r   c                 h    | j                  t        j                        }|j                  |d       y )NFrk   r1   r   rD   r"   r   r#   rH   s      r   rE   z%WorkingDirectory._explorer_change_dir   s&    ??7#3#34t%(r   c                 f    | j                  t        j                        }| j                  ||       y r!   r   r   s      r   rF   z%WorkingDirectory._explorer_dir_opened   s$    ??7#3#34

4"r   c                 f    | j                  t        j                        }| j                  ||       y r!   )r1   r   rK   r"   )r   r#   rO   s      r   rM   z'WorkingDirectory._ipyconsole_change_dir   s$    __W%;%;<


4$r   c                 h    | j                  t        j                        }| j                  ||       y N)rs   rt   )r1   r   rR   r"   r   r#   rY   s      r   rT   z WorkingDirectory._project_loaded   s&    ??7#3#34

T
:r   c                     | j                  t        j                        }| j                  |j	                         |       y r   )r1   r   rR   r"   get_last_working_dirr   s      r   rW   z WorkingDirectory._project_closed   s6    ??7#3#34

335" 	 	
r   r!   )9__name__
__module____qualname____doc__NAMEr   r7   Consoler2   REQUIRESr=   rD   rK   rn   rR   OPTIONALr   CONTAINER_CLASSCONF_SECTIONr   CONF_WIDGET_CLASSCAN_BE_DISABLED	CONF_FILEr
   ry   r   strr'   staticmethodr   r   r   r/   r   r5   r;   rB   rI   rP   rZ   r   r]   r`   rc   re   rg   ri   r"   r+   rr   r   r?   rE   rF   rM   rT   rW   r   r   r   r   r      sW    D##W__gooFH 0 0'2H2Hg..0H/OL2OI\*H %+3K! & &K/
 0; 1;
  3 346 56 /? 0?  0 01C 2C
  6 67) 8)  0 01J 2J
 w/2 02
 w2238 48 w~~.B /B w//0F 1F
 w556) 7) w//0M 1MB*	2!)#%;
r   r   )r   os.pathr#   rw   qtpy.QtCorer   spyder.api.pluginsr   r   )spyder.api.plugin_registration.decoratorsr   r   spyder.api.translationsr	   spyder.config.baser
   (spyder.plugins.workingdirectory.confpager   )spyder.plugins.workingdirectory.containerr   spyder.plugins.toolbar.apir   spyder.utilsr   r   r   r   r   <module>r      s@   
   7- % , O : !g
~ g
r   