Mini Shell
3
<�Zce � @ s� d dl mZ d dlmZ d dlmZ d dlmZ d dlm Z d dl
mZmZ d dl
mZ e� ZG dd � d e�ZG d
d� de�ZdS )
� )�Enum)�App)�ExitMainLoop)�ExceptionSignal)�Prompt)�InputHandler�PasswordInputHandler)�get_simpleline_loggerc sx e Zd Z� fdd�Zedd� �Zedd� �Zedd� �Zejd d� �Zd
d� Z dd
d�Z
dd� Zdd� Zdd� Z
� ZS )�InputManagerc s, t � j� || _d| _d| _d| _d| _dS )z�Processor for user input.
This class is mainly helper class for ScreenScheduler.
:param ui_screen: Screen associated with this input manager.
:type ui_screen: The `simpleline.render.screen.UIScreen` based instance.
r � FN)�super�__init__�
_ui_screen�_input_error_counter�_input_error_threshold�_skip_concurrency_check�_input_args)�selfZ ui_screen)� __class__� �#/usr/lib/python3.6/input_manager.pyr
% s
zInputManager.__init__c C s | j S )z2Return how many times the user provided bad input.)r )r r r r �input_error_counter4 s z InputManager.input_error_counterc C s | j | j }|dkS )zXDid the error counter pass the threshold?
The screen should be redraw.
r )r r )r �errorsr r r �input_error_threshold_exceeded9 s z+InputManager.input_error_threshold_exceededc C s | j S )znShould the concurrency check be skipped?
:returns bool: True if the check should be skipped.
)r )r r r r �skip_concurrency_checkB s z#InputManager.skip_concurrency_checkc C s
|| _ dS )a= Set if the concurrency check should be skipped when asking for user input.
WARNING: Use this option with caution. When the concurrency check is disabled you
can easily get to unexpected behavior which is hard to debug.
:param bool value: True to skip the concurrency check.
N)r )r �valuer r r r J s c C sP |r&t | d�}| jjr0|j| jj� n
t| d�}| j|_|j|� |j� |j S )z�Get blocking input from the user.
:param message: Message prompt for the user.
:type message: str
:param hidden: Do not echo user input (password typing).
:type hidden: bool
)�source)
r r �
password_func�
set_pass_funcr r r � get_inputZ
wait_on_inputr )r �messageZhidden�handlerr r r �get_input_blockingU s
zInputManager.get_input_blockingNc C sz | j j|�}| j|�sdS || _| j js6t| j d�}n"t| j d�}| j jrX|j| j j� | j |_
|j| j� |j
|� dS )z�Get input from user.
:param args: Arguments passed in when UIScreen was scheduled.
:type args: Anything.
N)r )r �prompt�_is_input_expectedr Zhide_user_inputr r r r r r Zset_callback�
process_inputr )r �argsr# r! r r r r j s
zInputManager.get_inputc C s |dkrd| _ dS dS dS )a9 Check if user handled input processing some other way.
Do nothing if user did handled user input.
:returns: True if prompt is set and we can use it to get user input.
False if prompt is not available, which means that user handled input on their
own.
Nr FT)r )r r# r r r r$ � s
zInputManager._is_input_expectedc C s~ y| j |�}W n: tk
r$ � Y n& tk
rH tj� jt| �� dS X |j� rZd| _n| jd7 _tj � j
|| j� dS )z�Process input from the screens.
:param user_input: User input string.
:type user_input: String.
:raises: ExitMainLoop or any other kind of exception from screen processing.
Nr � )�_process_inputr � Exceptionr Zget_event_loopZenqueue_signalr �was_successfulr Z
get_schedulerZprocess_input_resultr )r Z
user_input�resultr r r r% � s
zInputManager.process_inputc C s� ddl m} yT| jj| j|�}||jkr.tjS ||jkr>tj S ||j
krNtjS ||jkr^tj
S W n tk
rv � Y nX |tjkr�tj S |tjkr�tjS |tjkr�tjS |dkr�tjd� tj
S )aV Method called internally to process unhandled input key presses.
:param key: The string entered by user.
:type key: String.
:return: Return state result object.
:rtype: `simpleline.render.in_out_manager.UserInputResult` class.
:raises: Anything the Screen can raise in the input processing.
r )�
InputStateNzXReturned key from screen is None. This could be missing return in a screen input method?)Zsimpleline.render.screenr, r �inputr Z PROCESSED�UserInputAction�NOOPZPROCESSED_AND_REDRAW�REDRAWZPROCESSED_AND_CLOSE�CLOSEZ DISCARDED�INPUT_ERRORr r ZREFRESHZCONTINUE�QUIT�logZwarning)r �keyr, r r r r( � s,
zInputManager._process_input)N)�__name__�
__module__�__qualname__r
�propertyr r r �setterr"