
    G@dR#                         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dS )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 Z8dS )!WorkingDirectoryz+
    Working directory changer plugin.
    
workingdirFc                       t          d          S )NzWorking directoryr        Flib/python3.11/site-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JJr   c                 ,    |                      d          S )NDirOpenIcon)create_iconr   s    r   get_iconzWorkingDirectory.get_iconC   s    ...r   c                 ,                                      }|j                             j                    j                            d fd	                                            }|                                                     |j                   d S )Nc                 0                         | |          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   s    &&((	/77.	0 	0 	0*22>>>>>	@ 	@ 	@ 3355)	
 	
 	
 	
 	
r   )r$   c                     |                                  }|                     t          j                  }|                    |j                   d S r!   )r&   
get_pluginr   Toolbaradd_application_toolbartoolbar)r   r-   r4   s      r   on_toolbar_availablez%WorkingDirectory.on_toolbar_availableT   sC    &&((	//'/22''	(9:::::r   c                 n    |                      t          j                  }|                    |            d S r!   )r1   r   Preferencesregister_plugin_preferencesr   preferencess     r   on_preferences_availablez)WorkingDirectory.on_preferences_availableZ   s0    oog&9:://55555r   c                     |                      t          j                  }|j                            | j                   d S r!   )r1   r   Editorsig_dir_openedr(   _editor_change_dirr   editors     r   on_editor_availablez$WorkingDirectory.on_editor_available_   s5    00%%d&=>>>>>r   c                     |                      t          j                  }| j                            | j                   |j                            | j                   d S r!   )r1   r   Explorerr'   r(   _explorer_change_dirr>   _explorer_dir_openedr   explorers     r   on_explorer_availablez&WorkingDirectory.on_explorer_availabled   sP    ??7#344*2243LMMM''(ABBBBBr   c                     |                      t          j                  }| j                            |j                   |j                            | j                   d S r!   )r1   r   IPythonConsoler'   r(   $set_current_client_working_directory_ipyconsole_change_dirr   
ipyconsoles     r   on_ipyconsole_availablez(WorkingDirectory.on_ipyconsole_availablej   sb    __W%;<<
*22;	= 	= 	=088'	) 	) 	) 	) 	)r   c                     |                      t          j                  }|j                            | j                   |j        t                                       | j                   d S 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   sU    ??7#344#++D,@AAA#F+33D4HIIIIIr   c                     |                      t          j                  }|                    t          j                   d S r!   )r1   r   r2   remove_application_toolbarr   r   )r   r4   s     r   on_toolbar_teardownz$WorkingDirectory.on_toolbar_teardowny   s=    //'/22**0	2 	2 	2 	2 	2r   c                 n    |                      t          j                  }|                    |            d S r!   )r1   r   r7   deregister_plugin_preferencesr9   s     r   on_preferences_teardownz(WorkingDirectory.on_preferences_teardown   s0    oog&9::11$77777r   c                     |                      t          j                  }|j                            | j                   d S r!   )r1   r   r=   r>   
disconnectr?   r@   s     r   on_editor_teardownz#WorkingDirectory.on_editor_teardown   s5    00(()@AAAAAr   c                     |                      t          j                  }| j                            | j                   |j                            | j                   d S r!   )r1   r   rD   r'   rb   rE   r>   rF   rG   s     r   on_explorer_teardownz%WorkingDirectory.on_explorer_teardown   sP    ??7#344*55d6OPPP**4+DEEEEEr   c                     |                      t          j                  }| j                            |j                   |j                            | j                   d S r!   )r1   r   rK   r'   rb   rL   rM   rN   s     r   on_ipyconsole_teardownz'WorkingDirectory.on_ipyconsole_teardown   sb    __W%;<<
*55;	= 	= 	=0;;'	) 	) 	) 	) 	)r   c                     |                      t          j                  }|j                            | j                   |j        t                                       | j                   d S r!   )	r1   r   rR   rS   rb   rT   rU   rV   rW   rX   s     r   on_projects_teardownz%WorkingDirectory.on_projects_teardown   sU    ??7#344#..t/CDDD#F+66t7KLLLLLr   Nc                 B   |                      t          j                  }|                      t          j                  }|                      t          j                  }|r4||k    r.|                    |d           |                    |d           |r||k    r|                    |           |r|                                 |B| 	                                }|                    |d           |r|
                    |           |                                  dS )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44__W%;<<
w|,, 	<11NN95N111Yd;;; 	G-:55;;IFFF 	,))+++$**,,IOOIEO222 =11)<<<r   c                     t          j        | j                  r)t          j        | j                  \  }}d |D             }n+|&|                                                                 }|g}|S )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.
        c                 :    g | ]}t          j        |          |S r   )ospisdir).0names     r   
<listcomp>z1WorkingDirectory.load_history.<locals>.<listcomp>   s%    CCC39T??CtCCCr   )rx   isfileLOG_PATHr   	readlinesr&   _get_init_workdir)r   workdirhistoryr	   s       r   r+   zWorkingDirectory.load_history   sq     :dm$$ 	 !+DM::JGQCCCCCGG,,..@@BBiGr   c                     |                                                                  }	 t          j        || j                   dS # t
          $ r Y dS w xY w)zU
        Save history to a text file located in Spyder configuration folder.
        N)r&   get_historyr   
writelinesr~   EnvironmentError)r   r   s     r   rr   zWorkingDirectory.save_history   sc     $$&&2244	77777 	 	 	DD	s   A 
AAc                 N    |                                                                  S )z
        Get current working directory.

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

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

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

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

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

335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
   r~   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      s         D#W_goFH 0'2Hg.0H/OL2OI}\**H %+F3KK! & & \&K K K/ / /
 
 
 000; ; 10;
  34446 6 546 ///? ? 0/?  0111C C 21C
  6777) ) 87)  0111J J 21J
 w///2 2 0/2
 w23338 8 438 w~...B B /.B w/000F F 10F
 w5666) ) 76) w/000M M 10M   B   *  	2 	2 	2! ! !) ) )# # #% % %; ; ;
 
 
 
 
r   r   )r   os.pathr#   rx   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 6 6 6 6 6 6 6- - - - - - - - % % % % % % , , , , , , O O O O O O      : : : : : : ! ! ! ! ! !g
 g
 g
 g
 g
~ g
 g
 g
 g
 g
r   