
    MhY6                        U d Z ddlZddlZddlZddlZddlZddlZddlZddl	m
Z
mZ ddlmZmZmZmZmZmZ ddlmZ ddlmZmZ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$ ddl%m&Z& ej'        Z'i Z(e)e*d<   e(+                    e           ddddiidfiZ,e)e*d<   e,+                     edddd                     e(+                    e,           i Z-e)e*d<   e-+                    e
           ddddddd d!d"d#d$d%d&Z.e)e*d'<   e-+                    e.           ee"e$gZ/ej        ej        ej0                          e*d(<    G d) de'          Z1 G d* d+e1          Z2dS ),a)   A minimal application base mixin for all ZMQ based IPython frontends.

This is not a complete console app, as subprocess will not be able to receive
input, there is no real readline support, among other limitations. This is a
refactoring of what used to be the IPython/qt/console/qtconsoleapp.py
    N)base_aliases
base_flags)CBoolCUnicodeDictListTypeUnicode)boolean_flag   )KernelManagerconnectfind_connection_filetunnel_to_kernel)BlockingKernelClient)KernelConnectionInfo)NoSuchKernel)	localhost)KernelRestarter)Session)	_filefindflagsexistingJupyterConsoleAppzkernel*.jsonzJConnect to an existing kernel. If no argument specified, guess most recent	app_flagszconfirm-exitzJupyterConsoleApp.confirm_exitzSet to display confirmation dialog on exit. You can always use 'exit' or
       'quit', to force a direct exit without any confirmation. This can also
       be set in the config file by setting
       `c.JupyterConsoleApp.confirm_exit`.
    zDon't prompt the user when exiting. This will terminate the kernel
       if it is owned by the frontend, and leave it alive if it is external.
       This can also be set in the config file by setting
       `c.JupyterConsoleApp.confirm_exit`.
    aliaseszJupyterConsoleApp.ipzJupyterConsoleApp.transportzJupyterConsoleApp.hb_portzJupyterConsoleApp.shell_portzJupyterConsoleApp.iopub_portzJupyterConsoleApp.stdin_portzJupyterConsoleApp.control_portzJupyterConsoleApp.existingz!JupyterConsoleApp.connection_filezJupyterConsoleApp.kernel_namezJupyterConsoleApp.sshserverzJupyterConsoleApp.sshkey)ip	transporthbshelliopubstdincontrolr   fkernelsshsshkeyapp_aliasesclassesc                      e Zd ZU dZdZej        eef         e	d<   dZ
ej        eef         e	d<   eZ ee          Z ee          Z eedd          ZeZ e e                      Z ed	dd
          Z ed	dd          ZdefdZ ed	dd          Z eddd          Z eddd          ZddeddfdZddZ ddZ!defdZ"ddZ#ddZ$ddeddfdZ%dS )r   z%The base Jupyter console application.zjupyter-console-mixinnamea  
        The Jupyter Console Mixin.

        This class contains the common portions of console client (QtConsole,
        ZMQ-based terminal console, etc).  It is not a full console, in that
        launched terminal subprocesses will not be able to accept input.

        The Console using this mixing supports various extra features beyond
        the single-process Terminal IPython shell, such as connecting to
        existing kernel, via:

            jupyter console <appname> --existing

        as well as tunnel via SSH

    descriptionTz The kernel manager class to use.)default_valueconfighelp z/The SSH server to use to connect to the kernel.)r.   r/   z<Path to the ssh key to use for logging in to the ssh server.returnc                 .    dt          j                    z  S )Nzkernel-%i.json)osgetpidselfs    Y/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/jupyter_client/consoleapp.py_connection_file_defaultz*JupyterConsoleApp._connection_file_default   s    ")++--    z$Connect to an already running kernelpythonz(The name of the default kernel to start.z
        Set to display confirmation dialog on exit. You can always use 'exit' or 'quit',
        to force a direct exit without any confirmation.Nargvc                     | j         | _        dS )zbuild argv to be passed to kernel subprocess

        Override in subclasses if any args should be passed to the kernel
        N)
extra_argskernel_argvr6   r;   s     r7   build_kernel_argvz#JupyterConsoleApp.build_kernel_argv   s    
  ?r9   c                    | j         }| j        r	 t          | j        d|g          }nE# t          $ r8 | j                            d| j                   |                     d           Y nw xY w| j                            d|           || _        n	 t          | j        |g          }nq# t          $ rd | j        t          j
                            | j                  k    r&t          j
                            || j                  }n| j        }|| _        Y nw xY w	 t          | j        d|g          | _        n1# t          $ r$ | j                            d| j                   Y dS w xY w	 |                                  dS # t          $ r; | j                            d| j        d	           |                     d           Y dS w xY w)
a  find the connection file, and load the info if found.

        The current working directory and the current profile's security
        directory will be searched for the file if it is not given by
        absolute path.

        When attempting to connect to an existing kernel and the `--existing`
        argument does not match an existing file, it will be interpreted as a
        fileglob, and the matching file in the current profile's security dir
        with the latest access time will be used.

        After this method is called, self.connection_file contains the *full path*
        to the connection file, never just its name.
        .z1Could not find existing kernel connection file %sr   z!Connecting to existing kernel: %szConnection File not found: %sNz"Failed to load connection file: %rTexc_info)runtime_dirr   r   	Exceptionlogcriticalexitdebugconnection_filer3   pathbasenamejoinr   OSErrorload_connection_fileerror)r6   rE   cfs      r7   init_connection_filez&JupyterConsoleApp.init_connection_file   s    &= 	*)$-#{9KLL   !!G   		!	
 HNN>CCC#%D  	*)$*>NN * * *'27+;+;D<P+Q+QQQk43GHHBB-B')$$$*	#,T-ACCU#V#VD   	 	 	HNN:D<PQQQFF		%%''''' 	 	 	HNN4$    
 IIaLLLLLL	sG   ( ?A*)A*B( (A+DDD7 7*E%$E%)E? ?AGGc                    | j         r| j        s	| j        sdS |                                  | j        }| j        }|dk    r/| j                            d|           t          j	        d           | j        r| j        s|| _        t                      }|| j        | j        | j        | j        | j        d}| j                            d|| j                   t                      | _        	 t#          || j        | j                  }n8#  | j                            dd	           | 	                    d
           Y nxY w|\  | _        | _        | _        | _        | _        | j        }t&          j                            |          \  }}|dz   |z   | _        |                                  | j                            d           | j                            dt&          j                            | j                             dS )zset up ssh tunnels, if needed.Ntcpz1Can only use ssh tunnels with TCP sockets, not %s)r   
shell_port
iopub_port
stdin_porthb_portcontrol_portz#Forwarding connections to %s via %szCould not setup tunnelsTrC   r   z-sshz/To connect another client via this tunnel, use:z--existing %s)r   	sshserverr'   rP   r   r   rG   rQ   sysrI   r   rW   rX   rY   rZ   r[   infor   rK   r3   rL   splitextwrite_connection_filerM   )r6   r   r   r^   newportsrR   rootexts           r7   init_sshzJupyterConsoleApp.init_ssh   s   } 	T^ 	DK 	F!!###N	WHNNNPYZZZHRLLL; 	t~ 	DNB ///| -&
 &
 	;RPPP ++	'dndkJJHH	HNN4tNDDDIIaLLLLL 	
OOOL !G$$R((	c#f}s2""$$$GHHHorw'7'78L'M'MNNNNNs   )D 3D:c                    d}|st          t          j                                                  d          d         }| j        }t
          j                            |d|z            }t
          j                            |          s|nd}||S )Nr0   -rV   zkernel-%s.json)	struuiduuid4splitrE   r3   rL   rN   exists)r6   rR   identrE   s       r7   _new_connection_filez&JupyterConsoleApp._new_connection_file  s     		6 
%%++C004E*Kk+;e+CDDB 7>>"--52B  		6 	r9   c                    | j         r	d| _        dS t          j        t          j        t          j                   	 |                     | j        | j        | j        | j	        | j
        | j        | j        | j        | j        | j        | | j                  | _        nE# t"          $ r8 | j                            d| j                   |                     d           Y nw xY wt+          j        t.          | j                  | _        | j        | j        _        i }| j        |d<    | j        j        di | t9          j        | j        j                   | j        r| j                                          | j        }|j	        | _	        |j
        | _
        |j        | _        |j        | _        |j        | _        |j        | _        t9          j        | j        j!                   dS )zInitialize the kernel manager.N)r   sessionr   rW   rX   rY   rZ   r[   rK   kernel_nameparentdata_dirzCould not find kernel %sr   extra_arguments )"r   kernel_managersignalSIGINTSIG_DFLkernel_manager_classr   ro   r   rW   rX   rY   rZ   r[   rK   rp   rr   r   rG   rH   rI   tcastr   kernel_client_classclient_factoryr>   start_kernelatexitregistercleanup_ipc_filesr\   r`   cleanup_connection_file)r6   kwargskms      r7   init_kernel_managerz%JupyterConsoleApp.init_kernel_manager  s    = 	"&DFfmV^444	"&";";7.???!. $ 4 , #< # #D  	 	 	H8$:JKKKIIaLLLLL	  f]D4GHH-1-E*$($4 !((226222+=>>>> 	855777  ---zO!1+CDDDDDs   AB ?CCc                 &   | j         | j                                         | _        nQ|                     | j        | j        | j        | j        | j        | j	        | j
        | j        | j        | 
  
        | _        | j                                         dS )zInitialize the kernel client.N)
ro   r   r   rW   rX   rY   rZ   r[   rK   rq   )ru   clientkernel_clientr|   ro   r   r   rW   rX   rY   rZ   r[   rK   start_channelsr5   s    r7   init_kernel_clientz$JupyterConsoleApp.init_kernel_clientO  s    *!%!4!;!;!=!=D!%!9!97.???!. $ 4 ": " "D 	))+++++r9   c                     t          | dd          rdS |                                  |                                  |                                  |                                  dS )zu
        Classes which mix this class in should call:
               JupyterConsoleApp.initialize(self,argv)
        _dispatchingFN)getattrrS   rd   r   r   r?   s     r7   
initializezJupyterConsoleApp.initializec  sf    
 4// 	F!!###  """!!!!!r9   )N)r1   N)&__name__
__module____qualname____doc__r+   rz   Unionrg   r
   __annotations__r,   r)   r   r   r   r	   r   ry   r   r|   r   r>   r\   r'   r8   r   r   rp   r   confirm_exitobjectr@   rS   rd   rm   r   r   r   rt   r9   r7   r   r   \   s'        //"9D!'#w,
999*Kg&   " GDKKEd7mmG4#/  
 /$wwyy//K 4.cdddIW
O  F.# . . . . x4.XYYYH'$R  K 5<  L+ +f + + + + +8 8 8 8t4O 4O 4O 4Olc    0E 0E 0E 0Ed, , , ,(
" 
"v 
" 
" 
" 
" 
" 
" 
"r9   c                   D     e Zd ZdZdej        dej        ddf fdZ xZS )IPythonConsoleAppz$An app to manage an ipython console.argsr   r1   Nc                 f    t          j        dd            t                      j        |i | dS )zInitialize the app.z6IPythonConsoleApp is deprecated. Use JupyterConsoleApp   )
stacklevelN)warningswarnsuper__init__)r6   r   r   	__class__s      r7   r   zIPythonConsoleApp.__init__s  s<    N[\]]]]$)&)))))r9   )r   r   r   r   rz   Anyr   __classcell__)r   s   @r7   r   r   p  s\        ..*ae *qu * * * * * * * * * * *r9   r   )3r   r   r3   rv   r]   typingrz   rh   r   jupyter_core.applicationr   r   	traitletsr   r   r   r   r	   r
   traitlets.config.applicationr   r0   r   r   r   r   blockingr   r   
kernelspecr   localinterfacesr   	restarterr   ro   r   utilsr   ConnectionFileMixinr   dictr   updater   r   r(   r)   r   r   r   rt   r9   r7   <module>r      s
      				  



       = = = = = = = = @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ 5 5 5 5 5 5 L L L L L L L L L L L L * * * * * * ) ) ) ) ) ) $ $ $ $ $ $ & & & & & & & & & & & &            1  t    Z   
 	z>:;T	4    
  L(	
	     Y       |    !.
%+++/,	,-(( T    {    #0'!Jqu	 J J JQ" Q" Q" Q" Q"+ Q" Q" Q"h* * * * *) * * * * *r9   