
    ړi'*                       d 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 dd	lmZ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"m#Z# ddl$m%Z%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l0m1Z1 ddl2m3Z3 ddl4m5Z5 ddl6m7Z7 dgZ8 G d de-      Z9 G d de7      Z:d Z;d  Z<d! Z= G d" d#e      Z> G d$ d%e      Z? G d& d'e1      Z@ G d( d)e      Zd* ZAd+ ZBd, ZCd- ZDy.)/z

Adaptor for using the input system of `prompt_toolkit` with the IPython
backend.

This gives a powerful interactive shell that has a nice user interface, but
also the power of for instance all the %-magic functions that IPython has to
offer.

    )annotations)Iterable)warn)utils)TransformerManager)InteractiveShellEmbed)load_default_config)CompleteEvent	Completer
CompletionPathCompleterWordCompleter)SystemCompletercompile)GrammarCompleter)GrammarLexer)Document)AnyFormattedTextPygmentsTokens)PygmentsLexerSimpleLexer)Style)	BashLexerPythonLexer)PromptStyle   )PythonCompleter)PythonInput)PyCF_ALLOW_TOP_LEVEL_AWAIT)default_ui_style)PythonValidatorembedc                  .    e Zd ZdZd ZddZddZddZy)	IPythonPromptzI
    Style for IPython >5.0, use the prompt_toolkit tokens directly.
    c                    || _         y N)prompts)selfr(   s     7/tmp/pip-target-20744g9h/lib/python/ptpython/ipython.py__init__zIPythonPrompt.__init__6   s	        c                H    t        | j                  j                               S r'   )r   r(   in_prompt_tokensr)   s    r*   	in_promptzIPythonPrompt.in_prompt9   s    dll;;=>>r,   c                H    t        | j                  j                               S r'   )r   r(   continuation_prompt_tokens)r)   widths     r*   
in2_promptzIPythonPrompt.in2_prompt<   s    dllEEGHHr,   c                    g S r'    r/   s    r*   
out_promptzIPythonPrompt.out_prompt?   s    	r,   N)returnr   )r3   intr8   r   )__name__
__module____qualname____doc__r+   r0   r4   r7   r6   r,   r*   r%   r%   1   s    ?Ir,   r%   c                  *     e Zd Z fdZd fdZ xZS )IPythonValidatorc                B    t        |   |i | t               | _        y r'   )superr+   r   isp)r)   argskwargs	__class__s      r*   r+   zIPythonValidator.__init__D   s    $)&)%'r,   c                    t        | j                  j                  |j                              }t        |   |       y )N)text)r   rB   transform_cellrG   rA   validate)r)   documentrE   s     r*   rI   zIPythonValidator.validateH   s-    !8!8!GH"r,   )rJ   r   r8   None)r:   r;   r<   r+   rI   __classcell__rE   s   @r*   r?   r?   C   s    (# #r,   r?   c                     t        d      S )z*
    Return compiled IPython grammar.
    a  
        \s*
        (
            (?P<percent>%)(
                (?P<magic>pycat|run|loadpy|load)  \s+ (?P<py_filename>[^\s]+)  |
                (?P<magic>cat)                    \s+ (?P<filename>[^\s]+)     |
                (?P<magic>pushd|cd|ls)            \s+ (?P<directory>[^\s]+)    |
                (?P<magic>pdb)                    \s+ (?P<pdb_arg>[^\s]+)      |
                (?P<magic>autocall)               \s+ (?P<autocall_arg>[^\s]+) |
                (?P<magic>time|timeit|prun)       \s+ (?P<python>.+)           |
                (?P<magic>psource|pfile|pinfo|pinfo2) \s+ (?P<python>.+)       |
                (?P<magic>system)                 \s+ (?P<system>.+)           |
                (?P<magic>unalias)                \s+ (?P<alias_name>.+)       |
                (?P<magic>[^\s]+)   .* |
            ) .*            |
            !(?P<system>.+) |
            (?![%!]) (?P<python>.+)
        )
        \s*
    r   r6   r,   r*   create_ipython_grammarrO   M   s     	 r,   c                    t               }t        |t        | ||      t        |      t	        |      t        ddgd      t        g dd      t        dd       t        d	      t        d	      t               d
	      S )NonoffT)ignore_case)012Fc                $    | j                  d      S )Nz.py)endswith)names    r*   <lambda>z"create_completer.<locals>.<lambda>}   s    uAU r,   )only_directoriesfile_filter)r[   )	pythonmagic
alias_namepdb_argautocall_argpy_filenamefilename	directorysystem)rO   r   r   MagicsCompleterAliasCompleterr   r   r   )get_globals
get_localsmagics_manageralias_manager get_enable_dictionary_completiongs         r*   create_completerrn   i   s     	 A	%Z)I %^4(7$dE]E)/tL(!&4U &u=&=%'	
 r,   c            
         t               } t        | t        d      t        d      t        d      t        t              t        t
              d      S )Nzclass:pygments.operatorzclass:pygments.keywordzclass:pygments.name)percentr^   rc   r]   re   )lexers)rO   r   r   r   r   r   )rm   s    r*   create_lexerrr      sJ     A	"#<= !9:#$9:#K0#I.
	 	r,   c                  &    e Zd Zd Z	 	 	 	 	 	 ddZy)rf   c                    || _         y r'   )rj   )r)   rj   s     r*   r+   zMagicsCompleter.__init__   s
    ,r,   c              #     K   |j                   j                         }t        | j                  j                  d         D ]-  }|j                  |      st        | t        |              / y w)Nline)text_before_cursorlstripsortedrj   magics
startswithr   len)r)   rJ   complete_eventrG   ms        r*   get_completionszMagicsCompleter.get_completions   sb      **113++226:; 	5A||D! A3#d)44	5s   AA0A0NrJ   r   r}   r
   r8   zIterable[Completion]r:   r;   r<   r+   r   r6   r,   r*   rf   rf      s$    -5 52?5	5r,   rf   c                  &    e Zd Zd Z	 	 	 	 	 	 ddZy)rg   c                    || _         y r'   )rk   )r)   rk   s     r*   r+   zAliasCompleter.__init__   s
    *r,   c              #     K   |j                   j                         }| j                  j                  }t	        |d       D ]2  \  }}|j                  |      st        | t        |       |       4 y w)Nc                    | d   S )Nr   r6   )as    r*   rZ   z0AliasCompleter.get_completions.<locals>.<lambda>   s
    AaD r,   )key)display_meta)rw   rx   rk   aliasesry   r{   r   r|   )r)   rJ   r}   rG   r   r   cmds          r*   r   zAliasCompleter.get_completions   sl      **113$$,,W.9 	GFAs||D! A3#d)#FF	Gs   AA7A7Nr   r   r6   r,   r*   rg   rg      s(    +	G 	G2?	G		Gr,   rg   c                  ,     e Zd ZdZ fdZ fdZ xZS )IPythonInputzR
    Override our `PythonCommandLineInterface` to add IPython specific stuff.
    c                    t        |d   |d   |j                  |j                   fd      |d<   t               |d<   t	         j
                        |d<   t           |i | | _        t        |j                         j                  d<   d _        i }|j                  t               |j                  dd	d
dd       dt        j                   |      i _         j%                  d       y )Nrh   c                      j                   S r'   )enable_dictionary_completionr/   s   r*   rZ   z'IPythonInput.__init__.<locals>.<lambda>   s    D55 r,   
_completer_lexer)get_compiler_flags
_validatoripythonz#009900z#00ff00 boldz#990000z#ff0000 bold)zpygments.promptzpygments.prompt-numzpygments.out-promptzpygments.out-prompt-numdefault)rn   rj   rk   rr   r?   r   rA   r+   ipython_shellr%   r(   all_prompt_stylesprompt_styleupdater!   r   	from_dict	ui_stylesuse_ui_colorscheme)r)   r   r   kw
style_dictrE   s   `    r*   r+   zIPythonInput.__init__   s    +}}((''5
< $~8+t?V?VW<!"r"*,9-:O:O,Py)% 
*+#,'5'0+9		
 $U__Z%@A	*r,   c                b    t         |          }| j                  j                  r	|t        z  }|S r'   )rA   r   r   	autoawaitr    )r)   flagsrE   s     r*   r   zIPythonInput.get_compiler_flags   s/    *,''//Er,   )r:   r;   r<   r=   r+   r   rL   rM   s   @r*   r   r      s    +@ r,   r   c                  *     e Zd ZdZ fdZddZ xZS )r   z
    Override the `InteractiveShellEmbed` from IPython, to replace the front-end
    with our input shell.

    :param configure: Callable for configuring the repl.
    c                .    |j                  dd      }|j                  dd       }|j                  dd       }|j                  dd       }d _        t        	   |i |  fd}t	         |||      }|r||_        |r ||       d|_        | _        y )	Nvi_modeFhistory_filename	configuretitlec                      j                   S r'   )user_nsr/   s   r*   rh   z3InteractiveShellEmbed.__init__.<locals>.get_globals   s    <<r,   )rh   r   r   r   )popconfirm_exitrA   r+   r   terminal_titler   python_input)
r)   r   r   r   r   r   r   rh   r   rE   s
   `        r*   r+   zInteractiveShellEmbed.__init__   s    &&E*66"4d;FF;-	w% "!"r"	  $#-	
 */L'l#(1L%(r,   c                    	 | j                   j                  j                         S # t        $ r& t	               | j                   j
                  _        Y yw xY w)N )r   apprunKeyboardInterruptr   default_bufferrJ   r/   s    r*   prompt_for_codez%InteractiveShellEmbed.prompt_for_code  sJ    	$$((,,..  	8@
D,,5	s   #& ,AA)r8   str)r:   r;   r<   r=   r+   r   rL   rM   s   @r*   r   r      s    )<r,   r   c                   	 t        |       |D ]  }	 | j                  j                  |         y#  t        d| dt        j
                  j                          z          | j                          Y fxY w# t        $ r Y yw xY w)zM
    Partial copy of `InteractiveShellApp.init_extensions` from IPython.
    zError in loading extension: z
Check your config files in N)	iterextension_managerload_extensionr   	ipy_utilspathget_ipython_dirshowtraceback	TypeError)shell
extensionsexts      r*   initialize_extensionsr     s    &Z  	&C&''66s;	&&23%85inn6T6T6V5WXY ##%  s   A8 1AA58	BBc                    	 t        |       	 |D ]  }	 | j                  |d        y#  | j                          Y .xY w#  | j                          Y yxY w# t        $ r Y yw xY w)zK
    Partial copy of  run_exec_lines code from IPython.core.shellapp .
    F)store_historyN)r   run_cellr   r   )r   
exec_linesrv   s      r*   run_exec_linesr   !  sp    "Z	"" **NN4uN=**'')	"!  s.   A A *A >A A	A$#A$c                 P   | j                  d      }| j                  dd      }| j                  dd      }| t               }|j                  |_        || d<   t	        j
                  di | }t        ||d   d          t        ||d   d          t        |        ||d	|
       y)za
    Copied from `IPython/terminal/embed.py`, but using our `InteractiveShellEmbed` instead.
    configheaderr   compile_flagsNInteractiveShellAppr   r      )r   stack_depthr   r6   )	getr   r	   TerminalInteractiveShellr   instancer   r   run_startup_scripts)rD   r   r   r   r   s        r*   r#   r#   4  s     ZZ!FZZ"%FJJ5M~$&'-'F'F$!x!**4V4E%(=!>|!LM5&!67EF	QmDr,   c                Z   ddl }ddl}| j                  j                  }g }||j                  |j                  j                  |d            z  }||j                  |j                  j                  |d            z  }|D ]*  }| j                  t        |      j                                , y)zk
    Contributed by linyuxu:
    https://github.com/prompt-toolkit/ptpython/issues/126#issue-161242480
    r   Nz*.pyz*.ipy)	globosprofile_dirstartup_dirr   joinr   openread)r   r   r   r   startup_filesfiles         r*   r   r   F  s    
 ##//KMTYYrww||K@AAMTYYrww||KABBM *tDz()*r,   N)Er=   
__future__r   typingr   warningsr   IPythonr   r   IPython.core.inputtransformer2r   IPython.terminal.embedr   _InteractiveShellEmbedIPython.terminal.ipappr	   prompt_toolkit.completionr
   r   r   r   r   !prompt_toolkit.contrib.completersr   1prompt_toolkit.contrib.regular_languages.compilerr   3prompt_toolkit.contrib.regular_languages.completionr   .prompt_toolkit.contrib.regular_languages.lexerr   prompt_toolkit.documentr   prompt_toolkit.formatted_textr   r   prompt_toolkit.lexersr   r   prompt_toolkit.stylesr   pygments.lexersr   r   ptpython.prompt_styler   	completerr   r   r   replr    styler!   	validatorr"   __all__r%   r?   rO   rn   rr   rf   rg   r   r   r   r#   r   r6   r,   r*   <module>r      s   	 #   & = R 6  > E P G , J < ' 2 - & % , # &)K $# #8:5i 5GY G ); )X+2 +\&("&E$*r,   