U
    pufÜ  ã                   @   sÜ   d Z ddlZddlZddlZddlmZ ddlZdd„ Zedd„ ƒZ	G dd	„ d	ej
ƒZG d
d„ deƒZedd„ ƒZedd„ ƒZedd„ ƒZdZzeejd ƒZW n ek
r´   Y nX edkrÄeZnejdkrÔeZneZdS )z;Enable wxPython to be used interactively in prompt_toolkit
é    N)Údefault_timerc                    s   ‡ fdd„}|S )aC  Decorator which causes KeyboardInterrupt exceptions to be ignored during
    execution of the decorated function.

    This is used by the inputhook functions to handle the event where the user
    presses CTRL+C while IPython is idle, and the inputhook loop is running. In
    this case, we want to ignore interrupts.
    c                     s(   zˆ | |Ž W n t k
r"   Y nX d S ©N)ÚKeyboardInterrupt)ÚargsÚkwargs©Úfunc© úE/tmp/pip-unpacked-wheel-bxwx4lqe/IPython/terminal/pt_inputhooks/wx.pyÚwrapper   s    z*ignore_keyboardinterrupts.<locals>.wrapperr	   )r   r   r	   r   r
   Úignore_keyboardinterrupts   s    r   c                 C   sN   t  ¡ }|dk	rJt  ¡ st‚t  ¡ }t  |¡}| ¡ r@| ¡  q.| ¡  ~dS )z¼Run the wx event loop by processing pending events only.

    This approach seems to work, but its performance is not great as it
    relies on having PyOS_InputHook called regularly.
    Nr   )	ÚwxÚGetAppÚThread_IsMainÚAssertionErrorÚ	EventLoopÚEventLoopActivatorÚPendingÚDispatchÚProcessIdle)ÚcontextÚappÚevtloopÚear	   r	   r
   Úinputhook_wx1   s    

r   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚEventLoopTimerc                 C   s   || _ tj | ¡ d S r   )r   r   ÚTimerÚ__init__)Úselfr   r	   r	   r
   r   4   s    zEventLoopTimer.__init__c                 C   s   |   ¡  d S r   r   ©r   r	   r	   r
   ÚNotify8   s    zEventLoopTimer.NotifyN)Ú__name__Ú
__module__Ú__qualname__r   r    r	   r	   r	   r
   r   2   s   r   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚEventLoopRunnerc                 C   s6   || _ t ¡ | _t| jƒ| _| j |¡ | j ¡  d S r   )	Úinput_is_readyr   r   r   r   Úcheck_stdinÚtimerÚStartÚRun)r   Útimer%   r	   r	   r
   r)   >   s
    
zEventLoopRunner.Runc                 C   s    |   ¡ r| j ¡  | j ¡  d S r   )r%   r'   ÚStopr   ZExitr   r	   r	   r
   r&   E   s    
zEventLoopRunner.check_stdinN)r!   r"   r#   r)   r&   r	   r	   r	   r
   r$   <   s   r$   c                 C   s6   t  ¡ }|dk	r2t  ¡ st‚tƒ }|jd| jd dS )aˆ  Run the wx event loop, polling for stdin.

    This version runs the wx eventloop for an undetermined amount of time,
    during which it periodically checks to see if anything is ready on
    stdin.  If anything is ready on stdin, the event loop exits.

    The argument to elr.Run controls how often the event loop looks at stdin.
    This determines the responsiveness at the keyboard.  A setting of 1000
    enables a user to type at most 1 char per second.  I have found that a
    setting of 10 gives good keyboard response.  We can shorten it further,
    but eventually performance would suffer from calling select/kbhit too
    often.
    Né
   )r*   r%   r   )r   r   r   r   r$   r)   r%   )r   r   Zelrr	   r	   r
   Úinputhook_wx2K   s    ÿr-   c                 C   sÀ   t  ¡ }|dk	r¼t  ¡ st‚tt tj¡ƒs<t tjtj¡ t  	¡ }t  
|¡}tƒ }|  ¡ sº| ¡ rttƒ }| ¡  q\| ¡  tƒ | }|dkršt d¡ qT|dkr®t d¡ qTt d¡ qT~dS )a^  Run the wx event loop by processing pending events only.

    This is like inputhook_wx1, but it keeps processing pending events
    until stdin is ready.  After processing all pending events, a call to
    time.sleep is inserted.  This is needed, otherwise, CPU usage is at 100%.
    This sleep time should be tuned though for best performance.
    Ng      $@g      ð?gš™™™™™¹?gš™™™™™©?gü©ñÒMbP?r   )r   r   r   r   ÚcallableÚsignalÚ	getsignalÚSIGINTÚdefault_int_handlerr   r   Úclockr%   r   r   r   r*   Úsleep)r   r   r   r   ÚtZ	used_timer	   r	   r
   Úinputhook_wx3e   s*    	


r6   c                    s–   t  ¡ ‰ ˆ dkrdS ˆ ¡ r dS t  ¡ s,t‚d}t  ¡ ‰‡ ‡‡fdd„}ˆ |¡ ˆ t j|¡ t	t
 t
j¡ƒs€t
 
t
jt
j¡ ˆ  d¡ ˆ  ¡  dS )a|  Run the wx event loop until the user provides more input.

    This input hook is suitable for use with wxPython >= 4 (a.k.a. Phoenix).

    It uses the same approach to that used in
    ipykernel.eventloops.loop_wx. The wx.MainLoop is executed, and a wx.Timer
    is used to periodically poll the context for input. As soon as input is
    ready, the wx.MainLoop is stopped.
    Néd   c                    s   ˆ  ¡ rˆ ¡  ˆ  ¡  d S r   )r%   r+   ZExitMainLoop)Zev©r   r   r'   r	   r
   Úpoll¶   s    z!inputhook_wxphoenix.<locals>.pollF)r   r   r%   ZIsMainThreadr   r   r(   ZBindZ	EVT_TIMERr.   r/   r0   r1   r2   ZSetExitOnFrameDeleteZMainLoop)r   Úpoll_intervalr9   r	   r8   r
   Úinputhook_wxphoenix™   s    

r;   é   é   Údarwin)Ú__doc__Úsysr/   r*   Ztimeitr   r3   r   r   r   r   r   Úobjectr$   r-   r6   r;   Úmajor_versionÚintÚ__version__Ú	ExceptionZ	inputhookÚplatformr	   r	   r	   r
   Ú<module>   s4   



3
1
