a
    lcn                  	   @   s   d Z ddlZddlZddl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mZmZ ddlmZ dd	lmZmZmZmZmZmZmZmZmZmZmZmZm Z  dd
l!m"Z" ddl#m$Z$m%Z% ddl&m'Z'm(Z(m)Z) ddl*m+Z+ ddl,m-Z- ddl.m/Z/m0Z0 ddl1m2Z2 ddl3m4Z4 ddl5m6Z6m7Z7m8Z8 ddl9m:Z:m;Z; ddl<m=Z=m>Z> ddl?m@ZA ddlBmCZC ddlDmEZE ddlFmGZG ddlHmIZImJZJ ddlKmLZL ddlMmNZN ddlOmPZPmQZQmRZR ddlSmTZTmUZU ddlVmWZW eAXd ZYG d!d" d"eEZZeGj[d#eGj\d$eGj]d%eGj^d&iZ_eGj[d'eGj\d(eGj]d%eGj^d&iZ`d)d* Zad+D ]^ZbeceebZdz,edrbeeedd,rbedf snd-ZgW  qW n  ehy   d-ZgY  qY n0 q6d.Zgd/ejiv peg Zjd0d1 Zkd2d3 ZlG d4d5 d5e-ZmG d6d7 d7e	Zne
oen epd8krenq r  dS )9z/IPython terminal interface using prompt_toolkit    N)warn)get_asyncio_loop)InteractiveShellInteractiveShellABC)input)toggle_set_term_titleset_term_titlerestore_term_title)
abbrev_cwd)BoolUnicodeDictIntegerobserveInstanceTypedefaultEnumUnionAnyvalidateFloat)AutoSuggestFromHistory)DEFAULT_BUFFEREditingMode)HasFocus	ConditionIsDone)PygmentsTokens)History)ConditionalProcessor!HighlightMatchingBracketProcessor)
ColorDepth)patch_stdout)PromptSessionCompleteStyleprint_formatted_text)DynamicStylemerge_styles)style_from_pygments_clsstyle_from_pygments_dict)__version__)get_style_by_name)Style)Token   )TerminalPdbPdb)TerminalMagics)get_inputhook_name_and_func)PromptsClassicPromptsRichPromptDisplayHook)IPythonPTCompleterIPythonPTLexer)create_ipython_shortcutsz3.c                   @   s   e Zd ZdS )_NoStyleN)__name__
__module____qualname__ r>   r>   ]/var/www/html/django/DPS/env/lib/python3.9/site-packages/IPython/terminal/interactiveshell.pyr:   8       r:   z#ansibrightbluez#ansiblue boldz#ansibrightred#ansired boldz#ansibrightgreen#ansigreen boldc                   C   sN   zt jd W S  ty   Y n ty6   td Y n0 t jdkrFdS dS d S )NZEDITORzN$EDITOR environment variable is not pure ASCII. Using platform default editor.posixvinotepad)osenvironKeyErrorUnicodeErrorr   namer>   r>   r>   r?   get_default_editorJ   s    
rK   )stdinstdoutstderrisattyFTZIPY_TEST_SIMPLE_PROMPTc                 C   s>   ddl }|j| | d}| ds:|dr:|dd }|S )z
    We do not need to protect against error,
    this is taken care at a higher level where any reformat error is ignored.
    Indeed we may call reformatting on incomplete code.
    r   N)mode
)black
format_strZFileModeendswith)text_before_cursorrS   formatted_textr>   r>   r?   black_reformat_handlerl   s
    rX   c                 C   sh   ddl m} ddl m} |t }|j| |d\}}|r`| ds\|dr\|d d }|S | S d S )Nr   )file_resources)yapf_api)style_configrQ   rR   )Zyapf.yapflibrY   rZ   ZGetDefaultStyleForDirrF   getcwdZ
FormatCoderU   )rV   rY   rZ   r[   rW   Zwas_formattedr>   r>   r?   yapf_reformat_handlerz   s    
r]   c                       sH   e Zd ZdZ fddZdd Zdd Zdd	 Zed
dddZ	  Z
S )PtkHistoryAdapterzs
    Prompt toolkit has it's own way of handling history, Where it assumes it can
    Push/pull from history.

    c                    s   t    || _|   d S N)super__init__shell_refresh)selfrb   	__class__r>   r?   ra      s    
zPtkHistoryAdapter.__init__c                 C   s   d| _ |   d S NF)_loadedrc   rd   stringr>   r>   r?   append_string   s    zPtkHistoryAdapter.append_stringc                 C   s   | j st|  | _d S r_   )rh   listload_history_stringsZ_loaded_stringsrd   r>   r>   r?   rc      s    zPtkHistoryAdapter._refreshc                 c   sd   d}g }| j jj| j jddD ],\}}}| }|r||kr|| |}q|d d d E d H  d S )N T)include_latestrR   )rb   history_managerget_tailhistory_load_lengthrstripappend)rd   Z	last_cellres_____cellr>   r>   r?   rm      s    
z&PtkHistoryAdapter.load_history_stringsN)rj   returnc                 C   s   d S r_   r>   ri   r>   r>   r?   store_string   s    zPtkHistoryAdapter.store_string)r;   r<   r=   __doc__ra   rk   rc   rm   strr{   __classcell__r>   r>   re   r?   r^      s   r^   c                       s  e Zd Ze jddZedddjddZdZdZ	e
dddjddZeed	djddZed
d ZedddjddZe
dddjddZedddjddZedddjddZedddjddZedddjddZe
ddddjddZedddjddZedddjddZee
deedgddjddZeddd Z e!dd d! Z"d"d# Z#e!d$d%d& Z$e!d'e!d(d)d* Z%d+d, Z&ed-djddZ'edd.djddZ(e
e) d/djddZ*ee+d0djddZ,e-e+Z.e/d1d2d3 Z0e/d4d5d6 Z1edd7djddZ2e
d8d9djddZ3e4d:d;d<d=jddZ5edd>djddZ6edd?djddZ7e8dd@djddZ9eddAdjddZ:e
dBdCddjddZ;dDdE Z<e!dFdGdH Z=eddIdjddZ>e!dJdudKdLZ?dMdN Z@ fdOdPZAdQdR ZBdSdT ZCedUdV ZDedWdX ZEdYdZ ZFd[d\ ZGd]d^ ZHd_d` ZI fdadbZJ fdcddZK fdedfZLdgdh ZMdZNdidj ZOdkdl ZPdZQdmdn ZRdZSdvdodpZTeUjVZWdqdr ZXdZYdsdt ZZ  Z[S )wTerminalInteractiveShellT)config   zNumber of line at the bottom of the screen to reserve for the tab completion menu, search history, ...etc, the height of these menus will at most this value. Increase it is you prefer long and skinny menus, decrease for short and wide.)helpNz~/.pdbhistoryz'File in which to store and read historya  Use `raw_input` for the REPL, without completion and prompt colors.

            Useful when controlling IPython as a subprocess, and piping STDIN/OUT/ERR. Known usage are:
            IPython own testing machinery, and emacs inferior-shell integration through elpy.

            This mode default to `True` if the `IPY_TEST_SIMPLE_PROMPT`
            environment variable is set, or the current terminal is not a tty.c                 C   s   | j r
tS tS r_   )simple_promptr1   r0   rn   r>   r>   r?   debugger_cls   s    z%TerminalInteractiveShell.debugger_clsz
        Set to confirm when you try to exit IPython with an EOF (Control-D
        in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit',
        you can force a direct exit without any confirmation.emacsz5Shortcut style to use at the prompt. 'vi' or 'emacs'.z;Add shortcuts from 'emacs' insert mode to 'vi' insert mode.z
       Cursor shape changes depending on vi mode: beam in vi insert mode,
       block in nav mode, underscore in replace mode.g{Gz?zJThe time in milliseconds that is waited for a key code
       to complete.g      ?zUThe time in milliseconds that is waited for a mapped key
       sequence to complete.zMAutoformatter to reformat Terminal code. Can be `'black'`, `'yapf'` or `None`)r   
allow_noneFz
        Automatically add/delete closing bracket or quote when opening bracket or quote is entered/deleted.
        Brackets: (), [], {}
        Quotes: '', ""
        zQEnable mouse support in the prompt
(Note: prevents selecting text with the mouse)legacy)klasszThe name or class of a Pygments style to use for syntax
        highlighting. To see available styles, run `pygmentize -L styles`.editing_modec                 C   sV   |d   dkrd|d< n|d   dkr2d|d< tt|d  rP|d   S | jS )NvaluevimrD   r   r   )lowerhasattrr   upperr   )rd   proposalr>   r>   r?   _validate_editing_mode  s    
z/TerminalInteractiveShell._validate_editing_modec                 C   s   | j rtt|j | j _d S r_   )pt_appgetattrr   newr   r   rd   changer>   r>   r?   _editing_mode  s    z&TerminalInteractiveShell._editing_modec                 C   s<   |d u rdd | _ n$|dkr$t| _ n|dkr4t| _ ntd S )Nc                 S   s   | S r_   r>   )xr>   r>   r?   <lambda>   r@   z9TerminalInteractiveShell._set_formatter.<locals>.<lambda>rS   Zyapf)Zreformat_handlerrX   r]   
ValueError)rd   	formatterr>   r>   r?   _set_formatter  s    z'TerminalInteractiveShell._set_formatterautoformatterc                 C   s   |j }| | d S r_   )r   r   )rd   r   r   r>   r>   r?   _autoformatter_changed(  s    z/TerminalInteractiveShell._autoformatter_changedhighlighting_stylecolorsc                 C   s   |    d S r_   )refresh_styler   r>   r>   r?   _highlighting_style_changed-  s    z4TerminalInteractiveShell._highlighting_style_changedc                 C   s   |  | j| _d S r_   )_make_style_from_name_or_clsr   _stylern   r>   r>   r?   r   2  s    z&TerminalInteractiveShell.refresh_stylez0Override highlighting format for specific tokenszUse 24bit colors instead of 256 colors in prompt highlighting.
        If your terminal supports true color, the following command should
        print ``TRUECOLOR`` in orange::

            printf "\x1b[38;2;255;100;0mTRUECOLOR\x1b[0m\n"
        z?Set the editor used by IPython (default to $EDITOR/vi/notepad).z6Class used to generate Prompt token for prompt_toolkitpromptsc                 C   s
   |  | S r_   )prompts_classrn   r>   r>   r?   _prompts_defaultK  s    z)TerminalInteractiveShell._prompts_defaultdisplayhook_classc                 C   s   t S r_   )r6   rn   r>   r>   r?   _displayhook_class_defaultS  s    z3TerminalInteractiveShell._displayhook_class_defaultz$Automatically set the terminal titlezIPython: {cwd}ziCustomize the terminal title format.  This is a python format string. Available substitutions are: {cwd}.)columnmulticolumnreadlinelikezOptions for displaying tab completions, 'column', 'multicolumn', and 'readlinelike'. These options are for `prompt_toolkit`, see `prompt_toolkit` documentation for more information.r   )r   default_valuezHighlight matching brackets.zEnable vi (v) or Emacs (C-X C-E) shortcuts to open an external editor. This is in addition to the F2 binding, which is always enabled.zProvide an alternative handler to be called when the user presses Return. This is an advanced option intended for debugging, which may be changed or removed in later releases.z:Allows to enable/disable the prompt toolkit history searchr   zSpecifies from which source automatic suggestions are provided. Can be set to `'AutoSuggestFromHistory`' or `None` to disableautomatic suggestions. Default is `'AutoSuggestFromHistory`'.c                 C   s>   |d u rd | _ n|dkr"t | _ ntd| jr:| j | j_ d S )Nr   zNo valid provider.)auto_suggestr   r   r   )rd   providerr>   r>   r?   _set_autosuggestions  s    
z-TerminalInteractiveShell._set_autosuggestionsautosuggestions_providerc                 C   s   |j }| | d S r_   )r   r   )rd   r   r   r>   r>   r?   !_autosuggestions_provider_changed  s    z:TerminalInteractiveShell._autosuggestions_provider_changedz9Display the current vi mode (when using vi editing mode).
term_titlec                 C   s4   | j r(tr(td t| jjt d ntd d S )NT)cwdF)r   _is_ttyr   r   term_title_formatformatr
   r   r>   r>   r?   init_term_title  s    
z(TerminalInteractiveShell.init_term_titlec                 C   s   | j rtrt  d S r_   )r   r   r	   rn   r>   r>   r?   r	     s    
z+TerminalInteractiveShell.restore_term_titlec                    s   t t|   dg| j_d S )Nz
text/plain)r`   r   init_display_formatterdisplay_formatteractive_typesrn   re   r>   r?   r     s    z/TerminalInteractiveShell.init_display_formatterc                    s    j r fdd}| _d S t }t }  j _t fdd _t	t
 j }t  _tf  j|||t d j jd j j jdd   _d S )	Nc                     sp   d dd  j D } t| g}d dd  j D } d |d dkrf|t| q>d |S )Nro   c                 s   s   | ]}|d  V  qdS r/   Nr>   .0r   r>   r>   r?   	<genexpr>  r@   zSTerminalInteractiveShell.init_prompt_toolkit_cli.<locals>.prompt.<locals>.<genexpr>c                 s   s   | ]}|d  V  qdS r   r>   r   r>   r>   r?   r     r@   rQ   r   
incomplete)joinr   in_prompt_tokensr   continuation_prompt_tokenscheck_completeru   )prompt_textlinesprompt_continuationrn   r>   r?   prompt  s    
z@TerminalInteractiveShell.init_prompt_toolkit_cli.<locals>.promptc                      s    j S r_   )r   r>   rn   r>   r?   r     r@   zBTerminalInteractiveShell.init_prompt_toolkit_cli.<locals>.<lambda>)rb   Fz.py)r   r   key_bindingshistory	completerenable_history_searchstyleZinclude_default_pygments_stylemouse_supportZenable_open_in_editorcolor_depthZtempfile_suffix)r   prompt_for_coder9   r^   r   r   r   r'   r   r   r   r   r   asyncionew_event_looppt_loopr$   r   r7   r   r   extra_open_editor_shortcutsr   _extra_prompt_optionsr   )rd   r   r   r   r   r>   rn   r?   init_prompt_toolkit_cli  s4    
z0TerminalInteractiveShell.init_prompt_toolkit_clic                 C   sN  i }|dkr| j  }|dkr,td}t}n|dkrBt}td}n|dkrtd}|tjdtjd	tj	d
tj
jdtj
jdtj
jdtj
jjdtjdtjdtjdtjdi tjdkr|tjdtjdtjdtjdi n|dkrt}i }n
td|n6t|tr
t|}n|}tjdtjdtjdtjdi}|| j tt|t|g}|S )z
        Small wrapper that make an IPython compatible style from a style name

        We need that to add style for prompt ... etc.
        r   linuxmonokaiZlightbgpastieneutralr   z
#ansigreen	noinheritz#ansiyellowz	#ansibluezbold #ansibluez#ansibrightgreen boldz#ansiredz#ansibrightred boldntz#ansidarkgreenrB   z#ansidarkredrA   nocolorzGot unknown colors: )r   r   r,   _style_overrides_linux_style_overrides_light_bgupdater.   NumberOperatorStringNameFunctionClass	NamespaceVariableMagicPrompt	PromptNum	OutPromptOutPromptNumrF   rJ   r:   r   
isinstancer}   highlighting_style_overridesr(   r)   r*   )rd   Zname_or_clsZstyle_overridesr   Z	style_clsr   r>   r>   r?   r     sb    




z5TerminalInteractiveShell._make_style_from_name_or_clsc                 C   s   t jt jt jd| j S )N)r   r   r   )r%   ZMULTI_COLUMNCOLUMNZREADLINE_LIKEdisplay_completionsrn   r>   r>   r?   pt_complete_style  s    z*TerminalInteractiveShell.pt_complete_stylec                 C   s   | j rtjS d S r_   )
true_colorr"   Z
TRUE_COLORrn   r>   r>   r?   r     s    z$TerminalInteractiveShell.color_depthc                    s~    fdd} j dkr| }dt  j| fddd jttdd	ttt  @ t	 fd
d@ dgd}t
sz j|d< |S )z\
        Return the current layout option for the current Terminal InteractiveShell
        c                      s   t  j S r_   )r   r   r   r>   rn   r>   r?   get_message%  s    zCTerminalInteractiveShell._extra_prompt_options.<locals>.get_messager   Fc                    s   t  j| S r_   )r   r   r   )widthlinenoZis_soft_wraprn   r>   r?   r   8  s    z@TerminalInteractiveShell._extra_prompt_options.<locals>.<lambda>Tz[](){})charsc                      s    j S r_   )highlight_matching_bracketsr>   rn   r>   r?   r   B  r@   )	processorfilter)Zcomplete_in_threadlexerZreserve_space_for_menumessager   	multilineZcomplete_styleZinput_processors	inputhook)r   r8   space_for_menur   r    r!   r   r   r   r   PTK3r   )rd   r   optionsr>   rn   r?   r   !  s(    


z.TerminalInteractiveShell._extra_prompt_optionsc              	   C   s   | j r| j }d | _ nd}t }t }|| jur<|| j zdtdd* | jjf d|i| 	 }W d    n1 sx0    Y  W |d ur|| jur|| n|d ur|| jur|| 0 |S )Nro   T)rawr   )
rl_next_inputr   get_event_loop_policyr   r   set_event_loopr#   r   r   r   )rd   r   policyZold_looptextr>   r>   r?   r   I  s(    

&z(TerminalInteractiveShell.prompt_for_codec                 C   s   t dtdd d S )Nzu`enable_win_unicode_console` is deprecated since IPython 7.10, does not do anything and will be removed in the future   )
stacklevel)r   DeprecationWarningrn   r>   r>   r?   enable_win_unicode_consolek  s    z3TerminalInteractiveShell.enable_win_unicode_consolec                 C   s"   t jdvrd S dd l}|  d S )N>   win32clir   )sysplatformcoloramainit)rd   r  r>   r>   r?   init_ior  s    
z TerminalInteractiveShell.init_ioc                    s   t t|   | t d S r_   )r`   r   init_magicsregister_magicsr2   rn   re   r>   r?   r  y  s    z$TerminalInteractiveShell.init_magicsc                    s4   t t|   tjdkr0dD ]}| j|| qd S )NrC   )clearmorelessman)r`   r   
init_aliasrF   rJ   alias_managersoft_define_alias)rd   cmdre   r>   r?   r  }  s    
z#TerminalInteractiveShell.init_aliasc                    sH   t t| j|i | | | j |   |   d| _| | j	 d S )NT)
r`   r   ra   r   r   r   r   keep_runningr   r   )rd   argskwargsre   r>   r?   ra     s    z!TerminalInteractiveShell.__init__c                 C   s
   d| _ d S rg   )r  rn   r>   r>   r?   ask_exit  s    z!TerminalInteractiveShell.ask_exitc                 C   sn   d| _ | j rjt| jdd z|  }W n. tyT   | jrH| dddrP|   Y q0 |r| j|dd qd S )NTro   )endz#Do you really want to exit ([y]/n)?yn)store_history)	r  printseparate_inr   EOFErrorconfirm_exit
ask_yes_nor  run_cell)rd   coder>   r>   r?   interact  s    z!TerminalInteractiveShell.interactc              
   C   s   zz.|    W W t| dr$| j  |   qW n6 tyf } ztdt|j  W Y d }~n
d }~0 0 W t| dr~| j  |   q t| dr| j  |   0 q | 	  d S )N
_eventloopz
%s escaped interact()
)
r+  r   r,  stopr	   KeyboardInterruptr$  typer;   _atexit_once)rd   er>   r>   r?   mainloop  s     

*




z!TerminalInteractiveShell.mainloopc                 C   s   | j d ur|  | d S r_   )
_inputhook)rd   contextr>   r>   r?   r     s    
z"TerminalInteractiveShell.inputhookc                 C   sv   |r|dvrt |\| _| _nd  | _| _trrdd l}ddlm} |dkrTt | _n| jrh|| j| _n
|	 | _d S )N>   Zwebagginliner   )new_eventloop_with_inputhookr   )
r3   active_eventloopr3  r   r   Zprompt_toolkit.eventloopr6  r   r   r   )rd   guir   r6  r>   r>   r?   
enable_gui  s    
z#TerminalInteractiveShell.enable_guic                 C   sb   | j s
dS | j }| jr<tt|d| jjjd t| n"d	dd |D }t||dd dS )z>Overridden from the parent class to use fancy rewriting promptNro   )r   r   c                 s   s   | ]\}}|V  qd S r_   r>   )r   tsr>   r>   r?   r     r@   z>TerminalInteractiveShell.auto_rewrite_input.<locals>.<genexpr>)sep)
show_rewritten_inputr   Zrewrite_prompt_tokensr   r&   r   appr   r$  r   )rd   r  tokensr   r>   r>   r?   auto_rewrite_input  s    


z+TerminalInteractiveShell.auto_rewrite_inputc                 C   s0   |r| j | _t| | _ n| jr,| j| _ d| _dS )z+Switch prompts to classic for %doctest_modeN)r   _prompts_beforer5   )rd   rP   r>   r>   r?   switch_doctest_mode  s    z,TerminalInteractiveShell.switch_doctest_mode)N)N)\r;   r<   r=   r   tagmime_renderersr   r   r   Zdebugger_historyr   Zdebugger_history_filer   _use_simple_promptr   propertyr   r'  r   Z emacs_bindings_in_vi_insert_modeZmodal_cursorr   ZttimeoutlenZ
timeoutlenr   Z
auto_matchr   r   r   r-   r   r   r   r   r   r   r   r   r   r   r   rK   editorr4   r   r   r   r   r   r   r   r   r   r   r   r   r   Zhandle_returnr   r   r   r   Zprompt_includes_vi_moder   r	   r   r   r   r   r   r   r   r
  r  r  r  ra   r  r  r+  r2  r3  r   r7  r9  r   
system_rawsystemr@  rA  rB  r~   r>   r>   re   r?   r      sv  

	



	



+C

("	
r   __main__)sr|   r   rF   r  warningsr   ZIPython.core.async_helpersr   IPython.core.interactiveshellr   r   ZIPython.utils.py3compatr   IPython.utils.terminalr   r   r	   IPython.utils.processr
   	traitletsr   r   r   r   r   r   r   r   r   r   r   r   r   Zprompt_toolkit.auto_suggestr   Zprompt_toolkit.enumsr   r   Zprompt_toolkit.filtersr   r   r   Zprompt_toolkit.formatted_textr   Zprompt_toolkit.historyr   Z prompt_toolkit.layout.processorsr    r!   Zprompt_toolkit.outputr"   Zprompt_toolkit.patch_stdoutr#   Zprompt_toolkit.shortcutsr$   r%   r&   Zprompt_toolkit.stylesr'   r(   Zprompt_toolkit.styles.pygmentsr)   r*   Zprompt_toolkitr+   Zptk_versionpygments.stylesr,   pygments.styler-   pygments.tokenr.   debuggerr0   r1   magicsr2   Zpt_inputhooksr3   r   r4   r5   r6   Zptutilsr7   r8   	shortcutsr9   
startswithr   r:   r   r   r   r   r   r   rK   _namer   _streamr   rO   r   r   rG   rE  rX   r]   r^   r   registerr;   instancer+  r>   r>   r>   r?   <module>   s   <

%    W

