Mini Shell
3
<�Zc4( � @ s� d dl Z d dlmZ d dlmZ d dlmZ d dlmZm Z ddgZ
G dd� de�ZG d d
� d
e �Z
G dd� de�ZG dd
� d
e
�ZdS )� N)�App)�InputReadySignal)�
TextWidget)�InputThreadManager�InputRequest�InputHandler�PasswordInputHandlerc s� e Zd Zd� fdd� Zdd� Zdd� Zedd � �Zed
d� �Zedd
� �Z e j
dd
� �Z dd� Zdd� Zdd� Z
dd� Zdd� Zdd� Zdd� Zdd� Z� ZS ) r Nc s: t � j� d| _|| _d| _d| _d| _|| _| j� dS )a� Class to handle input from the terminal.
This class is designed to be instantiated on place where it should be used.
The main method is `get_input()` which is non-blocking asynchronous call. It can be used
as synchronous call be calling the `wait_on_input` method.
To get result from this class use the `value` property.
:param callback: You can specify callback which will be called when user give input.
:type callback: Callback function with one argument which will be user input.
:param source: Source of this input. It will be helpful in case of debugging an issue.
:type source: Class which will process an input from this InputHandler.
NF) �super�__init__�_input�_input_callback�_input_received�_input_successful�_skip_concurrency_check�_source�_register_input_ready_signal)�self�callback�source)� __class__� �#/usr/lib/python3.6/input_handler.pyr
s
zInputHandler.__init__c C s t j� jt| j� d S )N)r �get_event_loopZregister_signal_handlerr �_input_received_handler)r r r r r 9 s
z)InputHandler._register_input_ready_signalc C sR |j | krd S d| _|j| _| js&d S |j| _| jd k rN| j}d | _|| j� d S )NT)Zinput_handler_sourcer
�successr �datar r )r �signal�args�cbr r r r = s
z$InputHandler._input_received_handlerc C s | j S )zVReturn user input.
:returns: String or None if no is input received.
)r )r r r r �valueP s zInputHandler.valuec C s | j S )zQGet source of this input.
:returns: Anything probably UIScreen.
)r )r r r r r X s zInputHandler.sourcec C s | j S )zPIs this InputHandler skipping concurrency check?
:returns bool
)r )r r r r �skip_concurrency_check` s z#InputHandler.skip_concurrency_checkc C s
|| _ dS )z�Set if this InputHandler should skip concurrency check.
Note if you skip this check, you can have unexpected behavior. Use with caution.
:param value: True to skip the check, False if not.
N)r )r r r r r r h s c C s
|| _ dS )z�Set a callback to get user input asynchronously.
:param callback: Callback called when user write their input.
:type callback: Method with 1 argument which is user input: def cb(user_input)
N)r )r r r r r �set_callbackr s zInputHandler.set_callbackc C s | j S )zZWas user input already received?
:returns: True if yes, False otherwise.
)r
)r r r r �input_receivedz s zInputHandler.input_receivedc C s( | j r
dS x| j s"tj� jt� qW dS )z�Blocks execution till the user input is received.
Events will works as expected during this blocking.
Please check the `input_successful` method to test the input.
N)r
r r Zprocess_signalsr )r r r r �
wait_on_input� s zInputHandler.wait_on_inputc C s | j S )z6Was input successful?
:returns: bool
)r )r r r r �input_successful� s zInputHandler.input_successfulc C s | j � | j|� dS )a> Use prompt to ask for user input and wait (non-blocking) on user input.
This is an asynchronous call. If you want to wait for user input then use
the `wait_on_input` method. If you want to get results asynchronously then register
callback in constructor or by the `set_callback` method.
Check if user input was already received can be done by the `input_received` method call.
:param prompt: Ask user what you want to get.
:type prompt: String or Prompt instance.
:returns: User input.
:rtype: str
N)�_clear_input�_invoke_input_thread)r �promptr r r � get_input� s zInputHandler.get_inputc C s"