
    Je$                         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y)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                     || _         y)zSet namespace browser widgetN)namespacebrowser)selfr   s     X/usr/lib/python3/dist-packages/spyder/plugins/ipythonconsole/widgets/namespacebrowser.pyset_namespacebrowserz*NamepaceBrowserWidget.set_namespacebrowser4   s
     0    c                     | j                   y| j                  rW| j                  || j                        j	                          | j                  || j
                        j                          yy)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   ss    %  #00   ! "#00   ! " !r   c                 T    | j                   | j                   j                  |       yy)zSet the current namespace view.N)r   process_remote_view)r   views     r   r   z(NamepaceBrowserWidget.set_namespace_viewF   s'      ,!!55d; -r   c                 T    | j                   | j                   j                  |       yy)zSet var properties.N)r   r   )r   
propertiess     r   r   z(NamepaceBrowserWidget.set_var_propertiesK   s'      ,!!44Z@ -r   c                     | j                   y| j                  r<| j                  j                         }| j                  d      j	                  |       yy)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   sT    %  ,,>>@H  ))(3	 !r   c                    t        d      }t        d      }t        d      }t        d      }t        d      }t        d      }	 | j                  ddt              j                  |      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,- F G9: 2 3	1##"+ $ - .7Yt_=  	/S:-..	: 	9S#7788 	0S;.// 	 	0S;.// 	1S</00	1s   &A+ +BC/c                 L    | j                  ddd      j                  ||       y)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                 J    | j                  ddd      j                  |       y)zRemove a variableTFr<   N)r   remove_value)r   r4   s     r   r@   z"NamepaceBrowserWidget.remove_value   s*     	 	 l4 r   c                 L    | j                  ddd      j                  ||       y)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(  }	 | j                  ddt              j                  |||      S # t        $ rD}t        |      j                  d      d   }t        d	      j                  |
      }|cY d}~S d}~wt        $ r t        d      }|cY S t         j"                  $ r3 t        d      j                  dj%                  t&                    }|cY S t(        t*        t,        f$ r Y y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   sg   	  ''66 1 2G "oo6G ))a'';F+//1I	##"+ $ - .7YcY .7 .88
  	X^^C(+F   #F&F1	 
 J 	23CJ   	 4 5 6<V#'99Z#8 6< 6: 
 Jy9 		s1   )B. .	E+79C60E+6E+AE+E+*E+c                     	 | j                  ddt              j                  |      S # t        $ r t	        d      }|cY S t
        t        t        f$ r Y y 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   sh    		##"+ $ - .<^H-EF  	12CJy9 		s   &) AAAc                 $   |d   d   }| j                   d   j                  |      }d| _        |d   j                  dd      }|dk(  rd| j                  rX| j                  "| j                          | j                  d
       d| _        t        j                         | j                  _
        |r?|j                  dk(  r0| j                  |       | j                   d   j                  |       y		 t        t        | C  |       y	# t"        $ r Y y	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   s   
 _%h/!!),008  ^''(92>
?t77$$0002--->',D$ $ 0DNN DII!55##C(y)--f5+THM s   /D 	DDc                    |d   j                  dd      }|d   j                  dd      }|dk(  rt        j                         | j                  _        | j                  j
                  j                  j                  | j                  j                         | j                  j
                  j                  d       | j                  sd| _
        yy|d	k(  rW|d
k(  rR| j                  "| j                          | j                  d       t        j                         | j                  _        yt        t        | C  |       y)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'++J;J !% 0DNNNN  ((001I1IJNN  &&t, +++/( ,f_-?!?$$0002---> $ 0DNN'=c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   #   si      O 1#<
A
41:%!.%N
>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      sX     1   < & 3 ! * 
		8	$  XC- XCr   