B
    #["<                 @   s  d Z ddlZddlZddlZddlmZ ejdkrejZdd Z	e	dd Z
e
e_ydd	lmZ ejd
 W n ek
r   Y nX 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mZ ddlmZ ddlm Z  ddl!m"Z" ddl#m$Z$m%Z%m&Z&m'Z' ddl(m)Z)m*Z*m+Z+ ddl,m-Z-m.Z.m/Z/ ddl0m1Z1 dZ2e3e*Z4ddddiidfiZ5e56edddd  e56e/ e46e5 e3e+Z7e3d!d"d#d$d%Z8e86e. e86d&d'i e76e8 e9e8: Z8e9e5: Z5G d(d de)e-Z;G d)d* d*e;Z<d+d, Z=e>d-kre=  dS ).z A minimal application using the Qt console-style Jupyter frontend.

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.
    N)warnntc             C   s   | S )N )funcr   r   5lib/python3.7/site-packages/qtconsole/qtconsoleapp.py<lambda>$   s    r   c          
   C   sX   zDdd l }dd l}d}d}d|| ||}|jjd||| W d t| || X d S )Nr      zError starting QtConsole )ctypes	tracebackjoinformat_exceptionwindllZuser32ZMessageBoxWold_excepthook)exctypevaluetbr
   r   ZMB_ICONERRORtitlemsgr   r   r   gui_excepthook&   s    r   )r   zJupyter.Qtconsole)QtCoreQtGui)boolean_flag)catch_config_error)JupyterWidget)RichJupyterWidget)styles__version__)
MainWindow)QtKernelClient)QtKernelManager)DictUnicodeCBoolAny)
JupyterApp
base_flagsbase_aliases)JupyterConsoleAppapp_aliases	app_flags)is_local_ipz>
jupyter qtconsole                      # start the qtconsole
plainJupyterQtConsoleAppTzDisable rich text support.Zbannerz"JupyterQtConsoleApp.display_bannerz-Display a banner upon starting the QtConsole.z3Don't display a banner upon starting the QtConsole.zJupyterWidget.syntax_stylezJupyterQtConsoleApp.stylesheetzJupyterWidget.editorzConsoleWidget.paging)style
stylesheetZeditorZpagingzgui-completionzConsoleWidget.gui_completionc                   s  e Zd ZdZeZdZeZe	ge
j ZeeZeeZeeZeeZeZeZeddddZeddddZeddd	dZeddd
dZeddddZdd Zee Z!d% fdd	Z"dd Z#dd Z$dd Z%dd Z&dd Z'dd Z(dd Z)dd  Z*e+d& fd!d"	Z, fd#d$Z-  Z.S )'r-   zjupyter-qtconsolez
        The Jupyter QtConsole.
        
        This launches a Console-style application using Qt.  It is not a full
        console, in that launched terminal subprocesses will not be able to accept
        input.
        
    r	   Tzpath to a custom CSS stylesheet)confighelpFz2Start the console window with the menu bar hidden.z#Start the console window maximized.zEUse a plaintext widget instead of rich text (plain can't print/save).z8Whether to display a banner upon starting the QtConsole.c             C   s,   |rdnd}|| j j_|r"t| _nt| _d S )Nr,   Zrich)r0   ZConsoleWidgetkindr   widget_factoryr   )selfnameoldnewr2   r   r   r   _plain_changed   s
    
z"JupyterQtConsoleApp._plain_changedNc                s    t t| | | | j d S )N)superr-   parse_command_lineZbuild_kernel_argvZ
extra_args)r4   argv)	__class__r   r   r:      s    z&JupyterQtConsoleApp.parse_command_linec             C   s   | j |  | dd}i }| jjr*| j|d< |jf | | j|_| }|j	ddd | j
| jdd}| | ||_||_d|_d|_| j|_| j|_|S )zW Create and return new frontend attached to new kernel, launched on localhost.
        T)connection_fileparentZautorestartZextra_arguments)shellZiopub)r0   local_kernelF)kernel_manager_classZ_new_connection_filekernel_managerZ	ipykernelZkernel_argvZstart_kernelkernel_client_classZclient_factoryZclientstart_channelsr3   r0   init_colorskernel_client	_existing
_may_closeconfirm_exit_confirm_exitdisplay_banner_display_banner)r4   rB   kwargsrF   widgetr   r   r   new_frontend_master   s*    

z'JupyterQtConsoleApp.new_frontend_masterc             C   sd   | j || jd}|  |  | j| jdd}| | d|_d|_d|_| j	|_
||_d|_|S )zCreate and return a new frontend attached to an existing kernel.

        Parameters
        ----------
        connection_file : str
            The connection_file path this frontend is to connect to
        )r=   r0   F)r0   r@   TN)rC   r0   load_connection_filerD   r3   rE   rG   rH   rJ   rK   rL   rF   rB   )r4   r=   rF   rN   r   r   r   new_frontend_connection   s    

z+JupyterQtConsoleApp.new_frontend_connectionc             C   sj   | j |jj| jd}|  |  | j| jdd}| | d|_d|_	d|_
| j|_||_|j|_|S )zCreate and return a new frontend attached to an existing kernel.
        
        Parameters
        ----------
        current_widget : JupyterWidget
            The JupyterWidget whose kernel this frontend is to share
        )r=   r0   F)r0   r@   T)rC   rF   r=   r0   rP   rD   r3   rE   rG   rH   rJ   rK   rL   rB   )r4   Zcurrent_widgetrF   rN   r   r   r   new_frontend_slave   s    

z&JupyterQtConsoleApp.new_frontend_slavec             C   s   t dg| _| jd d S )Nzjupyter-qtconsole)r   QApplicationappZsetApplicationName)r4   r   r   r   init_qt_app  s    zJupyterQtConsoleApp.init_qt_appc             C   s*  t jt jt}t j|ddd}t|| j_	tj
| jj	 | j}| j pXt|}| j| j|d| _| | j | j| j_| j | j_| j| j_| j| j_| j| j_| j| j_t| j| j| j| j| jd| _| j | j_ | j!| j | j"  t#j$dkr| j%r| j& 'd | j(d d S )	NZ	resourcesiconzJupyterConsole.svg)r0   r@   )rI   Znew_frontend_factoryZslave_frontend_factoryZconnection_frontend_factorydarwinFzJupyter QtConsole))ospathabspathdirname__file__r   r   ZQIconrT   rV   rS   ZsetWindowIconipexistingr+   r3   r0   rN   rE   rG   rH   rI   rJ   rK   rL   rB   rF   r   rO   rR   rQ   windowlogZadd_tab_with_frontendZinit_menu_barsysplatformhide_menubarZmenuBarZ
setVisibleZsetWindowTitle)r4   	base_pathZ	icon_pathr]   r@   r   r   r   init_qt_elements  s4    







z$JupyterQtConsoleApp.init_qt_elementsc          	   C   s4  | j }d|kr|jjnd}d|kr*|jjnd}d|kr>|jjnd}|rp| }|dkr\d}q|dkrjd}qd	}n,|r|d
krd	}qt|rd}qd}nd}|rt	|||_||_|
  |  n|r|j|d | jrtj| jrt| j}| }W dQ R X ntd| j |r0||_|  dS )z$Configure the coloring of the widgetzZMQInteractiveShell.colorsNzJupyterWidget.syntax_stylezJupyterWidget.style_sheet)lightbgZlightrf   )Zdarklinuxrg   ZnocolorZbw)colorszStylesheet %r not found.)r0   ZZMQInteractiveShellrh   r   Zsyntax_styleZstyle_sheetlowerr   Z
dark_styleZsheet_from_templateZ_syntax_style_changedZ_style_sheet_changedZset_default_styler/   rX   rY   isfileopenreadIOError)r4   rN   cfgrh   r.   Zsheetfr   r   r   rE   ,  sB    

zJupyterQtConsoleApp.init_colorsc                sB   t  t j fdd t }|jdd  |d | _dS )zallow clean shutdown on sigintc                s
     dS )N)exit)Zsigframe)r4   r   r   r   b  s    z1JupyterQtConsoleApp.init_signal.<locals>.<lambda>c               S   s   d S )Nr   r   r   r   r   r   h  s       N)signalSIGINTr   ZQTimerZtimeoutZconnectstartZ_sigint_timer)r4   Ztimerr   )r4   r   init_signal`  s
    
zJupyterQtConsoleApp.init_signalc          
      sZ    |krV| j d| d fdd| j   D  | }|| |   |S dS )zWarn about deprecated config.z1Use %s in config, not %s. Outdated config:
    %sz
    c             3   s"   | ]\}}d j || dV  qdS )z{name}.{key} = {value!r})keyr   r5   N)format).0rx   r   )old_namer   r   	<genexpr>t  s   z8JupyterQtConsoleApp._deprecate_config.<locals>.<genexpr>N)r`   Zwarningr   r0   itemscopymerge)r4   rn   r{   new_namer   )r{   r   _deprecate_configm  s    
z%JupyterQtConsoleApp._deprecate_configc                sr   |    tt| | | jr"d S x.dD ]&\}}| | j||}|r(| | q(W t| | | 	  | 
  d S )N))IPythonQtConsoleAppZJupyterQtConsole)ZIPythonWidgetr   )ZRichIPythonWidgetr   )rU   r9   r-   
initializeZ_dispatchingr   r0   Zupdate_configr(   re   rw   )r4   r;   r{   r   rn   )r<   r   r   r   }  s    zJupyterQtConsoleApp.initializec                sB   t t|   | jr | j  n
| j  | j  | j	  d S )N)
r9   r-   rv   maximizer_   ZshowMaximizedZshowZraise_rT   Zexec_)r4   )r<   r   r   rv     s    

zJupyterQtConsoleApp.start)N)N)/__name__
__module____qualname__r5   r   versionZdescription	_examplesZexamplesr   r(   classesr!   flagsaliasesr$   qt_flagsZfrontend_flags
qt_aliasesZfrontend_aliasesr   rC   r    rA   r"   r/   r#   rc   r   r,   rK   r8   r   r3   r:   rO   rQ   rR   rU   re   rE   rw   r   r   r   rv   __classcell__r   r   )r<   r   r-      sF   	$4c                   s   e Zd Z fddZ  ZS )r   c                s    t dt tt| j|| d S )Nz:IPythonQtConsoleApp is deprecated; use JupyterQtConsoleApp)r   DeprecationWarningr9   r   __init__)r4   akw)r<   r   r   r     s    zIPythonQtConsoleApp.__init__)r   r   r   r   r   r   r   )r<   r   r     s   r   c               C   s   t   d S )N)r-   Zlaunch_instancer   r   r   r   main  s    r   __main__)?__doc__rX   rt   ra   warningsr   r5   
excepthookr   Zundocr   r
   r   Zshell32Z'SetCurrentProcessExplicitAppUserModelIDAttributeErrorZqtconsole.qtr   r   Ztraitlets.config.applicationr   r   Zqtconsole.jupyter_widgetr   Zqtconsole.rich_jupyter_widgetr   Z	qtconsoler   r   Zqtconsole.mainwindowr   Zqtconsole.clientr   Zqtconsole.managerr    Z	traitletsr!   r"   r#   r$   Zjupyter_core.applicationr%   r&   r'   Zjupyter_client.consoleappr(   r)   r*   Zjupyter_client.localinterfacesr+   r   dictr   r   updater   r   setkeysr-   r   r   r   r   r   r   r   <module>   sn   




   
