o
    eX                     @   sP  d dl mZmZmZ 	 d dl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 d dlm  mZ d dlm  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mZ dd
lm Z  ddl!m"Z" ddl#m$Z$m%Z% erd dl&Z&dZ'ndZ'G dd de(Z)G dd de*Z+G dd de*Z,G dd de,Z-dS )    )absolute_importprint_functionunicode_literalsN)glob)make_KeyPress_from_keydescr)is_ironpython)
ensure_strensure_unicode   )release)GetSetErrorReadlineErrorlog)editingmodes)execfileis_callablez>>>  c                   @   s   e Zd ZdS )MockConsoleErrorN)__name__
__module____qualname__ r   r   BD:\Projects\ConvertPro\env\Lib\site-packages\pyreadline3/rlmain.pyr   )   s    r   c                   @   s"   e Zd ZdZdd ZdddZdS )	MockConsolezVobject used during refactoring. Should raise errors when someone tries to use it.
    c                 C   s   t d)Nz1Should not try to get attributes from MockConsole)r   )selfxr   r   r   __setattr__1   s   zMockConsole.__setattr__2   c                 C      d S Nr   )r   sizer   r   r   cursor5      zMockConsole.cursorN)r   )r   r   r   __doc__r   r"   r   r   r   r   r   -   s    r   c                   @   s>  e Zd Zdd Zdd Zdd Zdd ZeeeZd	d
 Z	dd Z
dFddZdd Zdd Zdd Zdd Zdd Zdd ZdFddZdFddZdFd d!Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ ZdFd,d-ZdFd.d/Zd0d1 ZdGd3d4Zd5d6 ZdGd7d8Zd9d: Z d;d< Z!d=d> Z"d?d@ Z#e$j%&e'dAfdBdCZ(dDdE Z)dS )HBaseReadlinec                    s   d _ d _d _d _d _t  _d _ fddtD  _ jD ]}|	d  q# jd  _
   td j
  d  _d S )	NFg333333?nonec                    s   g | ]}| qS r   r   ).0moder   r   r   
<listcomp>D   s    z)BaseReadline.__init__.<locals>.<listcomp>r   
)allow_ctrl_cctrl_c_tap_time_intervaldebug
bell_stylemarkr   consoledisable_readliner   Zinit_editing_moder)   read_inputrcr   joinZrl_settings_to_stringcallback)r   r)   r   r*   r   __init__:   s   

zBaseReadline.__init__c           	      C   sX  zt d|  |drW dS |drTtd|}|rK|d}|d}zt| j|dd	| W W dS  t	yJ   t d
||f  Y W dS w t d|  W dS td|}|r|d}|d}|dd	}zt
| j|}W n t	y   t d||f  | jrtd|  Y W dS w | j|| W dS W dS  ty   t d  w )z6Parse and execute single line of a readline init file.zparse_and_bind("%s")#Nsetz set\s+([-a-zA-Z0-9]+)\s+(.+)\s*$r
      -_zunknown var="%s" val="%s"zbad set "%s"z\s*(\S+)\s*:\s*([-a-zA-Z]+)\s*$zunknown func key="%s" func="%s"z@pyreadline3 parse_and_bind error, unknown function to bind: "%s"error)r   
startswithrecompilematchgroupsetattrr)   replaceAttributeErrorgetattrr/   print	_bind_keyBaseException)	r   stringmvar_namevalkey	func_nameZpy_namefuncr   r   r   parse_and_bindN   sZ   





zBaseReadline.parse_and_bindc                 C   s   || j _d S r    r)   promptr   rS   r   r   r   _set_promptu      zBaseReadline._set_promptc                 C      | j jS r    rR   r*   r   r   r   _get_promptx   s   zBaseReadline._get_promptc                 C      | j j S )z/Return the current contents of the line buffer.)r)   l_bufferZget_line_textr*   r   r   r   get_line_buffer}      zBaseReadline.get_line_bufferc                 C   s   | j | dS )z"Insert text into the command line.N)r)   insert_textr   rJ   r   r   r   r]         zBaseReadline.insert_textNc                 C   s   t d|  dS )zUParse a readline initialization file. The default filename is the last filename used.zread_init_file("%s")Nr   r   filenamer   r   r   read_init_file   r_   zBaseReadline.read_init_filec                 C      | j j| dS )zFAppend a line to the history buffer, as if it was the last line typed.N)r)   _historyadd_historyr   liner   r   r   re         zBaseReadline.add_historyc                 C   rY   )zReturn the number of lines currently in the history.
        (This is different from get_history_length(), which returns
        the maximum number of lines that will be written to a history file.))r)   rd   get_current_history_lengthr*   r   r   r   ri         z'BaseReadline.get_current_history_lengthc                 C   rY   )zjReturn the desired length of the history file.

        Negative values imply unlimited history file size.)r)   rd   get_history_lengthr*   r   r   r   rk      rj   zBaseReadline.get_history_lengthc                 C   rc   )zSet the number of lines to save in the history file.

        write_history_file() uses this value to truncate the history file
        when saving. Negative values imply unlimited history file size.
        N)r)   rd   set_history_length)r   lengthr   r   r   rl      s   zBaseReadline.set_history_lengthc                 C   s   | j j|S )z5Return the current contents of history item at index.)r)   rd   get_history_item)r   indexr   r   r   rn      s   zBaseReadline.get_history_itemc                 C   s   | j j  dS )zClear readline historyN)r)   rd   clear_historyr*   r   r   r   rp      r_   zBaseReadline.clear_historyc                 C   s4   |du r	| j jj}tdt|  | j j| dS )zALoad a readline history file. The default filename is ~/.history.Nzread_history_file from %s)r)   rd   history_filenamer   r	   read_history_filer`   r   r   r   rr      s   
zBaseReadline.read_history_filec                 C   rc   )zASave a readline history file. The default filename is ~/.history.N)r)   rd   write_history_filer`   r   r   r   rs      rh   zBaseReadline.write_history_filec                 C   s   t d || j_dS )a  Set or remove the completer function.

        If function is specified, it will be used as the new completer
        function; if omitted or None, any completer function already
        installed is removed. The completer function is called as
        function(text, state), for state in 0, 1, 2, ..., until it returns a
        non-string value. It should return the next possible completion
        starting with text.
        set_completerNr   r)   Z	completerr   functionr   r   r   rt      s   
zBaseReadline.set_completerc                 C   s   t d | jjS )z$Get the completer function.
        get_completerru   r*   r   r   r   rx      s   zBaseReadline.get_completerc                 C   rW   )z=Get the beginning index of the readline tab-completion scope.)r)   Zbegidxr*   r   r   r   
get_begidx      zBaseReadline.get_begidxc                 C   rW   )z:Get the ending index of the readline tab-completion scope.)r)   Zendidxr*   r   r   r   
get_endidx   rz   zBaseReadline.get_endidxc                 C      || j _dS )z4Set the readline word delimiters for tab-completion.Nr)   completer_delimsr^   r   r   r   set_completer_delims   r\   z!BaseReadline.set_completer_delimsc                 C   rW   )z4Get the readline word delimiters for tab-completion.r}   r*   r   r   r   get_completer_delims   rz   z!BaseReadline.get_completer_delimsc                 C   r|   )aG  Set or remove the startup_hook function.

        If function is specified, it will be used as the new startup_hook
        function; if omitted or None, any hook function already installed is
        removed. The startup_hook function is called with no arguments just
        before readline prints the first prompt.

        N)r)   Zstartup_hookrv   r   r   r   set_startup_hook   s   	zBaseReadline.set_startup_hookc                 C   r|   )a  Set or remove the pre_input_hook function.

        If function is specified, it will be used as the new pre_input_hook
        function; if omitted or None, any hook function already installed is
        removed. The pre_input_hook function is called with no arguments
        after the first prompt has been printed and just before readline
        starts reading input characters.

        N)r)   Zpre_input_hookrv   r   r   r   set_pre_input_hook   s   
zBaseReadline.set_pre_input_hookc                 C   r   r    r   r*   r   r   r   _bell   r#   zBaseReadline._bellr   c                 C   s   t r    )NotImplementedErrorrT   r   r   r   readline   r#   zBaseReadline.readlinec                 C      | j |S r    )r)   process_keyevent)r   keyinfor   r   r   r     rV   zBaseReadline.process_keyeventc                 C   r   r    )r)   readline_setuprT   r   r   r   r     rV   zBaseReadline.readline_setupc                 C   s
   | j  S r    )r)   _readline_from_keyboard_pollr*   r   r   r   keyboard_poll     
zBaseReadline.keyboard_pollc                 C   s   || _ | | dS )zbool readline_callback_handler_install ( string prompt, callback callback)
        Initializes the readline callback interface and terminal, prints the prompt and returns immediately
        N)r6   r   )r   rS   r6   r   r   r   callback_handler_install  s   z%BaseReadline.callback_handler_installc                 C   s
   d| _ dS )zNRemoves a previously installed callback handler and restores terminal settingsN)r6   r*   r   r   r   callback_handler_remove  s   
z$BaseReadline.callback_handler_removec                 C   s4   |   r|  d }| | jj | | dS dS )UReads a character and informs the readline callback interface when a line is receivedr,   N)r   r[   re   r)   rZ   r6   rf   r   r   r   callback_read_char  s
   zBaseReadline.callback_read_charz~/pyreadlineconfig.inic                    s  t dd jD jd j fdd} fdd} fdd	} fd
d} fdd}dd }fdd}fdd}	fdd}
fdd}fdd}fdd}	fdd}fdd}fdd }fd!d"}fd#d$}fd%d&}dRfd(d)	}dd*d+d,d-d.d/d0d1d2d3d4d5d6d7d8}fd9d:}fd;d<}i d=tjd>tjd? d@dA|dB|dC|
dD|dE|dF|dG|	dH|dI|dJ|dK|dL|dM||||||||dN}tj|rz	t	||| W d S  t
y } z d }~ww d S )SNc                 S   s   g | ]}|j |fqS r   r)   )r(   r   r   r   r   r+   #  s    z-BaseReadline.read_inputrc.<locals>.<listcomp>r   c                    s    |  _ d S r    r   )name)modesr   r   r   setmode&     z*BaseReadline.read_inputrc.<locals>.setmodec                    sp   dd l }t|r  | ||   d S t  |r.  | t  | d S td|| f  d S )Nr   z/Trying to bind unknown command '%s' to key '%s')typesr   rH   
MethodTypehasattrrF   rG   )rN   r   r   r)   r   r   r   bind_key)  s    z+BaseReadline.read_inputrc.<locals>.bind_keyc                    .   t |  }|  jv r  j|= d S d S r    )r   tupleZkey_dispatchrN   r   r   r   r   un_bind_key4     z.BaseReadline.read_inputrc.<locals>.un_bind_keyc                    s      |  d S r    )Z_bind_exit_key)rN   r   r   r   bind_exit_key9     z0BaseReadline.read_inputrc.<locals>.bind_exit_keyc                    r   r    )r   r   Zexit_dispatchr   r   r   r   un_bind_exit_key<  r   z3BaseReadline.read_inputrc.<locals>.un_bind_exit_keyc                 S   s   dd l }| |jj_d S Nr   )pyreadline3.lineeditor.lineobj
lineeditorlineobjkill_ring_to_clipboard)ZkillringZpyreadline3r   r   r   setkill_ring_to_clipboardA  s   z<BaseReadline.read_inputrc.<locals>.setkill_ring_to_clipboardc                    s   t jt|  jj_d S r    )ospath
expanduserr   r)   rd   rq   )ra   r*   r   r   sethistoryfilenameE  s   z5BaseReadline.read_inputrc.<locals>.sethistoryfilenamec                    
   |  _ d S r    r0   r   r*   r   r   setbellstyleI  r   z/BaseReadline.read_inputrc.<locals>.setbellstylec                    r   r    )r3   r   r*   r   r   r3   L  r   z3BaseReadline.read_inputrc.<locals>.disable_readlinec                    s   t |  jj_d S r    )intr)   rd   history_length)rm   r*   r   r   sethistorylengthO  r   z3BaseReadline.read_inputrc.<locals>.sethistorylengthc                    s   t d j| f  |  _d S )Nzallow_ctrl_c:%s:%s)r   r-   r   r*   r   r   r-   R  s   
z/BaseReadline.read_inputrc.<locals>.allow_ctrl_cc                    r   r    r   r   r*   r   r   r   V  r   c                       |  j _d S r    )r)   show_all_if_ambiguousr   r*   r   r   r   Y  rV   z8BaseReadline.read_inputrc.<locals>.show_all_if_ambiguousc                    r   r    )r.   r   r*   r   r   r.   \  r   z;BaseReadline.read_inputrc.<locals>.ctrl_c_tap_time_intervalc                    r   r    )r)   mark_directoriesr   r*   r   r   r   _  rV   z3BaseReadline.read_inputrc.<locals>.mark_directoriesc                    r   r    r}   delimsr*   r   r   r~   b  rV   z3BaseReadline.read_inputrc.<locals>.completer_delimsc                    s   |    j_d S r    )lowerr)   complete_filesystemr   r*   r   r   r   e  s   z6BaseReadline.read_inputrc.<locals>.complete_filesystemc                    r   r    )r)   enable_ipython_paste_for_paths)booleanr*   r   r   r   h  rV   zABaseReadline.read_inputrc.<locals>.enable_ipython_paste_for_pathspyreadline_debug_log.txtc                    sr   | dv rd _ | dkrt| t  td d S | dkr*t  td d S td t  t  d S )N)onon_nologfileTr   zSTARTING LOGr   zSTOPING LOG)r/   loggerZstart_file_logZstart_socket_logr   Zstop_file_logZstop_socket_log)r   ra   r*   r   r   debug_outputk  s   

z/BaseReadline.read_inputrc.<locals>.debug_output   r:      r
               
      	            )blackZdarkredZ	darkgreenZ
darkyellowZdarkblueZdarkmagentaZdarkcyangrayredgreenyellowbluemagentacyanwhitec                        j |  d _d S Nr   )_color_trtablegetr   prompt_colorcolorr*   r   r   set_prompt_color     z3BaseReadline.read_inputrc.<locals>.set_prompt_colorc                    r   r   )r   r   r   command_colorr   r*   r   r   set_input_color  r   z2BaseReadline.read_inputrc.<locals>.set_input_colorbranchversionr)   r   set_moder   r3   r   r   r   r0   r   r   r~   r   r   rq   )r   r   r   r-   r.   r   r   zError reading .pyinputrc)filezLine: %s in file %s)r   )dictr   r)   r   r   r   r   r   isfiler   	Exception	tracebackrG   sysstderr
extract_tbexc_infor   )r   Zinputrcpathr   r   r   r   r   r   r   r   r3   r   r-   r   r.   r   r~   r   r   r   r   r   r   locr   r   filepathlinenor   )r)   r   r   r   r4   !  s   
	
zBaseReadline.read_inputrcc                 C   r   r    r   r*   r   r   r   	redisplay  r#   zBaseReadline.redisplayr    r   )*r   r   r   r7   rQ   rU   rX   propertyrS   r[   r]   rb   re   ri   rk   rl   rn   rp   rr   rs   rt   rx   ry   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r4   r   r   r   r   r   r%   9   sJ    '









 r%   c                   @   s   e Zd Z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dd Zdd Zdd Zd"ddZd"ddZdd Zdd  Zd!S )#Readlinez.Baseclass for readline based on a console
    c                 C   s>   t |  t | _| jjd> | _d | _d | _| j | _d S )Nr   )	r%   r7   r2   ConsoleZsaveattrselection_colorr   r   r!   r*   r   r   r   r7     s   

zReadline.__init__c                 C   sF   | j dkrdS | j dkrtd| j dkr| j  dS td| j  )zring the bell if requested.r&   visiblez)Bellstyle visible is not implemented yet.ZaudiblezBellstyle %s unknown.N)r0   r   r2   bellr   r*   r   r   r   r     s   


zReadline._bellc                 C   s\   | j }| \}}| \}}||||d |d f |d|d |t|d |f d S )Nr
   r   r   )r2   posr!   Z	rectanglemin)r   cr   ywhr   r   r   _clear_after  s
   $zReadline._clear_afterc                 C   s\   | j }| j\}}| \}}|| jj 7 }||kr&||8 }|d7 }||ks||| d S )Nr
   )r2   prompt_end_posr!   r)   rZ   Zvisible_line_widthr   )r   r   ZxcZycr   r   r   r   r   _set_cursor  s   
zReadline._set_cursorc                 C   sH   | j }| \}}|| j| j}||| f| _| | _| | _d S r    )r2   r   write_scrollingrS   r   prompt_begin_posr   r!   )r   r   r   r   nr   r   r   _print_prompt  s   
zReadline._print_promptc                 C   s@   |dkr| j \}}| j\}}||| f| _ ||| f| _d S d S r   )r  r   )r   r  bxZbyexZeyr   r   r   _update_prompt_pos  s   

zReadline._update_prompt_posc                 C   sl  | j }| jj}|d |j| j  |   | }|jrc|j	dkrct
|d |j	  }t
|d |j  }||krA||}}||d | | j}|||| | j}|||d  | j}n||| j}| \}}| \}	}
||
d ks|dkr|d |dd|	|
fdd |d7 }| | t|dr|  n|   |jd| jjd |   d S )Nr   r
   r'   clear_to_end_of_window)r!   )r2   r)   rZ   r"   r   r  r  Zquoted_textZenable_selectionZselection_marklenpointr  r   r   r!   Zscroll_windowscrollr  r   r  r   Zcursor_sizer   )r   r   rZ   ltextstartstopr  r   r   r   r   r   r   r   _update_line  s:   




zReadline._update_linec                 C   s@   |   r|  d }| jd | | jj | | dS dS )r   r,   
N)r   r[   r2   writere   r)   rZ   r6   rf   r   r   r   r     s   zReadline.callback_read_charc                 C   s   | j  pt| jdkS r   )r2   peekr	  paste_line_bufferr*   r   r   r   event_available$  r   zReadline.event_availablec                 C   s   	 |   rd S qr    )r   r*   r   r   r   _readline_from_keyboard'  s   z Readline._readline_from_keyboardc                 C   s   | j j}t|dkr t|d | _|   |dd  | j _dS | j}dd }z| }W n t	y:   | 
 }Y nw z	| j |j}W n tyP   t   w |   |S )Nr   r
   Tc                 S   r   r    r   )er   r   r   nop7  r#   z2Readline._readline_from_keyboard_poll.<locals>.nop)r)   r  r	  r   ZReadLineTextBufferrZ   r  r2   ZgetkeypressKeyboardInterrupthandle_ctrl_cr   r   EOFErrorr   Zstop_logging)r   Zpastebufferr   r  eventresultr   r   r   r   ,  s*   z%Readline._readline_from_keyboard_pollr   c                 C   s    t | | |   |   d S r    )r%   r   r  r  rT   r   r   r   r   E  s   zReadline.readline_setupc                 C   sD   |  | t | _|   | jd td|    |  d S )Nr  zreturning(%s)r,   )r   timectrl_c_timeoutr  r2   r  r   r[   rT   r   r   r   r   J  s   

zReadline.readlinec                 C   s|   ddl m} ddlm} td |dd}d|_|ddddd d|_| jr<t }|| j	 | j
k r7td	 t|| _	|S t)
Nr   )Event)KeyPressZKBDIRQr   FT)shiftcontrolmetaZkeynamezRaise KeyboardInterrupt)Zpyreadline3.console.eventr  pyreadline3.keysyms.commonr   r   charr   r-   r  r  r.   r  )r   r  r   r  nowr   r   r   r  R  s    
zReadline.handle_ctrl_cc                 C   s   t |  d S r    )r%   r   r*   r   r   r   r   e  r   zReadline.redisplayNr   )r   r   r   r$   r7   r   r   r   r  r  r  r   r  r  r   r   r   r  r   r   r   r   r   r     s"    
	$

r   ).
__future__r   r   r   r   r?   r   r  r   Zpyreadline3.clipboard	clipboardZpyreadline3.consoler2   Zpyreadline3.lineeditor.historyr   historyr   r   Zpyreadline3.loggerr   r$  r   Zpyreadline3.py3k_compatr   Zpyreadline3.unicode_helperr   r	   r   r   r=   r   r   r   r   r   Zpy3k_compatr   r   SystemZdefault_promptr   r   objectr   r%   r   r   r   r   r   <module>   s<   	  |