B
    )²ô`8  ã               @   sf   d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlmZ ddlm	Z	 G dd	„ d	ej
ƒZdS )
zEReadline-Based Command-Line Interface of TensorFlow Debugger (tfdbg).é    )Úabsolute_import)Údivision)Úprint_functionN)Úbase_ui)Údebugger_cli_commonc               @   sT   e Zd ZdZddd„Zdd„ Zdd„ Zdd
d„Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )Ú
ReadlineUIzReadline-based Command-line UI.Nc             C   s   t jj| ||d |  ¡  d S )N)Ú
on_ui_exitÚconfig)r   ÚBaseUIÚ__init__Ú_init_input)Úselfr   r	   © r   úV/home/dcms/DCMS/lib/python3.7/site-packages/tensorflow/python/debug/cli/readline_ui.pyr      s    zReadlineUI.__init__c             C   s8   t  d¡ t  d¡ t  | j¡ t  d¡ tjj| _d S )Nzset editing-mode emacsÚ
ztab: complete)	ÚreadlineZparse_and_bindZset_completer_delimsZset_completerÚ_readline_completeÚsixÚmovesÚinputÚ_input)r   r   r   r   r   #   s
    


zReadlineUI._init_inputc                s<   |   |¡\}}‰ | j ||¡\}}‡ fdd„|D ƒ}|| S )Nc                s   g | ]}ˆ | ‘qS r   r   )Ú.0Ú	candidate)Úexcept_last_wordr   r   ú
<listcomp>2   s    z1ReadlineUI._readline_complete.<locals>.<listcomp>)Z_analyze_tab_complete_inputZ_tab_completion_registryZget_completions)r   ÚtextÚstateÚcontextÚprefixÚ
candidatesÚ_r   )r   r   r   .   s
    
zReadlineUI._readline_completeTc             C   s4   t |ƒ |dk	r|  |¡ |  ¡ }| jr0|  ¡  |S )zCRun the CLI: See the doc of base_ui.BaseUI.run_ui for more details.N)ÚprintÚ_dispatch_commandÚ_ui_loopZ_on_ui_exit)r   Zinit_commandÚtitleZtitle_colorZenable_mouse_on_startÚ
exit_tokenr   r   r   Úrun_ui5   s    
zReadlineUI.run_uic             C   s(   x"|   ¡ }|  |¡}|d k	r|S qW d S )N)Ú_get_user_commandr"   )r   Úcommandr%   r   r   r   r#   H   s
    
zReadlineUI._ui_loopc             C   s   t dƒ |  | j¡ ¡ S )NÚ )r!   r   Z
CLI_PROMPTÚstrip)r   r   r   r   r'   P   s    zReadlineUI._get_user_commandc          
   C   s  || j krtjS y|  |¡\}}}W n. tk
rR } ztt|ƒƒ dS d}~X Y nX | j |¡r y| jj	||dd}W q¶ tj
k
rœ } z|jS d}~X Y q¶X nt | jd|  g¡}|  |¡ |ry| |¡ td| ƒ W n" tk
r   td| ƒ Y nX dS )zãDispatch user command.

    Args:
      command: (str) Command to dispatch.

    Returns:
      An exit token object. None value means that the UI loop should not exit.
      A non-None value means the UI loop should exit.
    N)Zscreen_infozInvalid command prefix "%s"zWrote output to %szFailed to write output to %s)ZCLI_EXIT_COMMANDSr   ZEXPLICIT_USER_EXITZ_parse_commandÚSyntaxErrorr!   ÚstrZ_command_handler_registryZis_registeredZdispatch_commandZCommandLineExitr%   ZRichTextLinesZERROR_MESSAGE_PREFIXÚ_display_outputZwrite_to_fileÚ	Exception)r   r(   r   ÚargsZoutput_file_pathÚeÚscreen_outputr   r   r   r"   T   s,    


zReadlineUI._dispatch_commandc             C   s   x|j D ]}t|ƒ qW d S )N)Úlinesr!   )r   r1   Úliner   r   r   r-   }   s    zReadlineUI._display_output)NN)NNNT)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r&   r#   r'   r"   r-   r   r   r   r   r      s   
   
)r   )r7   Ú
__future__r   r   r   r   r   Ztensorflow.python.debug.clir   r   r
   r   r   r   r   r   Ú<module>   s   