
    G@d$                         d Z ddlZddlmZmZ ddl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  ej        e          Zd	Z G d
 de          ZdS )zY
Widget that handle communications between the IPython Console and
the Variable Explorer
    N)PicklingErrorUnpicklingError)RichJupyterWidget)QMessageBox)	CommError)_)
IMPORT_EXT   c                   |     e Zd ZdZdZi ZdZd ZddZd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd Z fdZ fdZ xZS )NamepaceBrowserWidgetz
    Widget with the necessary attributes and methods to handle communications
    between the IPython Console and the Variable Explorer
    NTc                     || _         dS )zSet namespace browser widgetN)namespacebrowser)selfr   s     Vlib/python3.11/site-packages/spyder/plugins/ipythonconsole/widgets/namespacebrowser.pyset_namespacebrowserz*NamepaceBrowserWidget.set_namespacebrowser4   s     0    c                     | j         dS | j        r^|                     || j                                                   |                     || j                                                   dS dS )zRefresh namespace browserN)	interruptcallback)kernel_clientr   call_kernelset_namespace_viewget_namespace_viewset_var_propertiesget_var_properties)r   r   s     r   refresh_namespacebrowserz.NamepaceBrowserWidget.refresh_namespacebrowser8   s    %F  	##0    ! """#0    ! """""	# 	#r   c                 L    | j         | j                             |           dS dS )zSet the current namespace view.N)r   process_remote_view)r   views     r   r   z(NamepaceBrowserWidget.set_namespace_viewF   s0     ,!55d;;;;; -,r   c                 L    | j         | j                             |           dS dS )zSet var properties.N)r   r   )r   
propertiess     r   r   z(NamepaceBrowserWidget.set_var_propertiesK   s0     ,!44Z@@@@@ -,r   c                     | j         dS | j        rD| j                                        }|                     d                              |           dS dS )zSet the namespace view settingsNTr   )r   r   get_view_settingsr   set_namespace_view_settings)r   settingss     r   r%   z1NamepaceBrowserWidget.set_namespace_view_settingsP   sk    %F  	4,>>@@H   ))(33333		4 	4r   c                 ^   t          d          }t          d          }t          d          }t          d          }t          d          }t          d          }	 |                     ddt                                        |          S # t          $ r t          ||z            t          t          t          f$ r t          ||z            t          $ r t          ||z            t          $ r  t          $ r t          ||z            t          $ r t          ||z            w xY w)	zAsk kernel for a valuez'The variable is too big to be retrievedzThe variable is not picklablezThe kernel is deadz^An unkown error occurred. Check the console because its contents could have been printed therezThe comm channel is not workingzr<br><i>%s.</i><br><br><br><b>Note</b>: Please don't report this problem on Github, there's nothing to do about it.Tblockingdisplay_errortimeout)r   r   CALL_KERNEL_TIMEOUT	get_valueTimeoutError
ValueErrorr   r   	TypeErrorRuntimeErrorKeyErrorr   	Exception)r   name
reason_bigreason_not_picklablereason_deadreason_otherreason_commmsgs           r   r-   zNamepaceBrowserWidget.get_valueZ   s   @AA
 !@AA,-- F G G9:: 2 3 3	1##"+ $ - - .7Yt__=  	/ 	/ 	/S:-...	: 	9 	9 	9S#77888 	0 	0 	0S;./// 	 	 	 	0 	0 	0S;./// 	1 	1 	1S</000	1s   /B B D,c                 ^    |                      ddd                              ||           dS )zSet value for a variableTFr   r)   r*   N)r   	set_value)r   r4   values      r   r=   zNamepaceBrowserWidget.set_valuew   s=     	 	 	 ie$$$$$r   c                 \    |                      ddd                              |           dS )zRemove a variableTFr<   N)r   remove_value)r   r4   s     r   r@   z"NamepaceBrowserWidget.remove_value   s;     	 	 	 l4     r   c                 ^    |                      ddd                              ||           dS )zCopy a variableTFr<   N)r   
copy_value)r   	orig_namenew_names      r   rB   z NamepaceBrowserWidget.copy_value   s=     	 	 	 jH-----r   c                    d}| j         j        j        r\t          d          }t          j        t          j        z  }t	          j        | t          d          ||          }|t          j        k    }	 |                     ddt                    
                    |||          S # t          $ rW}t          |                              d          d         }t          d	                              |
          }|cY d}~S d}~wt          $ r t          d          }|cY S t           j        $ r@ t          d                              d                    t&                              }|cY S t(          t*          t,          f$ r Y dS w xY w)zLoad data from a file.FzWDo you want to overwrite old variables (if any) in the namespace when loading the data?zData loadingTr(   )	overwrite'   zSpyder is unable to open the file you're trying to load because <tt>{module}</tt> is not installed. Please install this package in your working environment.<br>)moduleNzData is too big to be loadedzThe file could not be opened successfully. Recall that the Variable Explorer supports the following file extensions to import data:<br><br><tt>{extensions}</tt>z, )
extensions)r   editorvar_propertiesr   r   YesNoquestionr   r,   	load_dataImportErrorstrsplitformatr.   tarfile	ReadErrorjoinr	   r   r1   r   )	r   filenameextrF   messagebuttonsresultr:   rI   s	            r   rP   zNamepaceBrowserWidget.load_data   s   	 '6 	2 1 2 2G "o6G )a'''; ;F+/1I	##"+ $ - - .7YcY .7 .8 .88
  	 	 	XX^^C((+F    #F&F11	 
 JJJJJJ 	 	 	233CJJJ  	 	 	 4 5 5 6<V#'99Z#8#8 6< 6: 6: 
 JJJy9 	 	 	44	s2   12B$ $
F.AD :F F AF-FFc                     	 |                      ddt                                        |          S # t          $ r t	          d          }|cY S t
          t          t          f$ r Y d S w xY w)NTr(   zData is too big to be saved)r   r,   save_namespacer.   r   r   r1   r   )r   rX   r:   s      r   r^   z$NamepaceBrowserWidget.save_namespace   s    		##"+ $ - - .<^H-E-EF  	 	 	122CJJJy9 	 	 	44	s   /2 A)A)(A)c                 j   |d         d         }| j         d                             |          }d| _        |d                             dd          }|dk    r\| j        rU| j        *|                                  |                     d
           d| _        t          j                    | j	        _
        |rB|j        dk    r7|                     |           | j         d                             |           d	S 	 t          t          |                               |           d	S # t"          $ r Y d	S w xY w)z^
        Reimplemented to handle communications between Spyder
        and the kernel
        parent_headermsg_idexecuteFcontentexecution_count r   Nr#   silent_exec_method)_request_infoget_reading_kernel_is_startingr   r%   r   time	monotonic	ipyclientt0kindhandle_exec_methodpopsuperr   _handle_execute_replyr1   )r   r:   ra   info
exec_count	__class__s        r   rs   z+NamepaceBrowserWidget._handle_execute_reply   sI   
 _%h/!),0088  ^''(92>>
??t7?$000222--->>>',D$ $ 0 0DN  	DI!555##C(((y)--f55555+T22HHMMMMM   s   :(D$ $
D21D2c                    |d                              dd          }|d                              dd          }|dk    r|t          j                    | j        _        | j        j        j                            | j        j                   | j        j        	                    d           | j
        s	d| _
        dS dS |d	k    rV|d
k    rP| j        *|                                  |                     d           t          j                    | j        _        dS t          t          |                               |           dS )z]
        Reimplemented to refresh the namespacebrowser after kernel
        restarts
        rc   execution_statere   r`   msg_typestartingi  Tidleshutdown_requestNFr#   )rh   rk   rl   rm   rn   timerr+   connect	show_timestartrj   r   r%   r   rr   r   _handle_status)r   r:   statery   rv   s       r   r   z$NamepaceBrowserWidget._handle_status   s:   
 I""#4b99'++J;;J !% 0 0DNN (001IJJJN &&t,,, + 0+/(((0 0f__-?!?!?$000222--->>> $ 0 0DN'..==cBBBBBr   )T)__name__
__module____qualname____doc__r   _kernel_methodsrj   r   r   r   r   r%   r-   r=   r@   rB   rP   r^   rs   r   __classcell__)rv   s   @r   r   r   #   s)          O 1 1 1# # # #< < <
A A A
4 4 41 1 1:% % %! ! !. . .% % %N
 
 
    >C C C C C C C C Cr   r   )r   loggingpickler   r   rU   rk   qtconsole.rich_jupyter_widgetr   qtpy.QtWidgetsr   spyder_kernels.comms.commbaser   spyder.config.baser   spyder.config.utilsr	   	getLoggerr   loggerr,   r    r   r   <module>r      s     1 1 1 1 1 1 1 1   < ; ; ; ; ; & & & & & & 3 3 3 3 3 3 !           * * * * * * 
	8	$	$  XC XC XC XC XC- XC XC XC XC XCr   