U
    NSh0                     @   s  d dl 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 ddlmZmZ edZedZG dd	 d	eZG d
d deZG dd dee ZG dd dee ZG dd dee ZG dd dee ZG dd dee Zedkrd dlm Z  ej!dddrej!dddZ"e"dkr0e"dkr0q<e d q
e de"  ej!d dd!Z#e$e#dkrjqve d" qJe d#e# ej!d$d%d&d'gd(Z%e d)e% ej!d*d+d,d-gd.d/Z&e d0e& ne d1 d2S )3    )AnyGenericListOptionalTextIOTypeVarUnionoverload   )get_console)Console)TextTextType
PromptTypeDefaultTypec                   @   s   e Zd ZdZdS )PromptErrorz/Exception base class for prompt related errors.N)__name__
__module____qualname____doc__ r   r   J/root/rtd-docs/venv/lib/python3.8/site-packages/pip/_vendor/rich/prompt.pyr      s   r   c                   @   s.   e Zd ZdZeddddZedddZdS )	InvalidResponsezException to indicate a response was invalid. Raise this within process_response() to indicate an error
    and provide an error message.

    Args:
        message (Union[str, Text]): Error message.
    N)messagereturnc                 C   s
   || _ d S Nr   )selfr   r   r   r   __init__   s    zInvalidResponse.__init__r   c                 C   s   | j S r   r   r   r   r   r   __rich__   s    zInvalidResponse.__rich__)r   r   r   r   r   r   r!   r   r   r   r   r      s   r   c                   @   sD  e Zd ZU dZeZeed< dZdZ	dZ
dZeee  ed< d3dd	dd
d
d
deee eeee  eeeddddZeed4dd	dd
d
d
ddeee eeee  eeeeee eeef d
ddZeed5dd	dd
d
d
ddeee eeee  eeeee ed	ddZed6dd	dd
d
d
dddeee eeee  eeeeee ed
ddZeedddZeedddZed7eeeee edddZeed d!d"Zeed d#d$Zee dd%d&d'Z!dd(d)d*Z"edd+ee ed,d-d.Z#edd+eee eeef d/d0d.Z#ddd1eee ed/d2d.Z#dS )8
PromptBasea  Ask the user for input until a valid response is received. This is the base class, see one of
    the concrete classes for examples.

    Args:
        prompt (TextType, optional): Prompt text. Defaults to "".
        console (Console, optional): A Console instance or None to use global console. Defaults to None.
        password (bool, optional): Enable password input. Defaults to False.
        choices (List[str], optional): A list of valid choices. Defaults to None.
        case_sensitive (bool, optional): Matching of choices should be case-sensitive. Defaults to True.
        show_default (bool, optional): Show default in prompt. Defaults to True.
        show_choices (bool, optional): Show choices in prompt. Defaults to True.
    response_typez*[prompt.invalid]Please enter a valid valuezA[prompt.invalid.choice]Please select one of the available optionsz: Nchoices FTconsolepasswordr$   case_sensitiveshow_defaultshow_choices)promptr'   r(   r$   r)   r*   r+   r   c                C   sT   |pt  | _t|tr$tj|ddn|| _|| _|d k	r>|| _|| _	|| _
|| _d S )Nr,   style)r   r'   
isinstancestrr   from_markupr,   r(   r$   r)   r*   r+   )r   r,   r'   r(   r$   r)   r*   r+   r   r   r   r   6   s    zPromptBase.__init__)r'   r(   r$   r)   r*   r+   stream)
r,   r'   r(   r$   r)   r*   r+   defaultr2   r   c          
      C   s   d S r   r   )
clsr,   r'   r(   r$   r)   r*   r+   r3   r2   r   r   r   askN   s    zPromptBase.ask)	r,   r'   r(   r$   r)   r*   r+   r2   r   c          	      C   s   d S r   r   )	r4   r,   r'   r(   r$   r)   r*   r+   r2   r   r   r   r5   _   s    .)r'   r(   r$   r)   r*   r+   r3   r2   c             	   C   s"   | |||||||d}
|
||	dS )a  Shortcut to construct and run a prompt loop and return the result.

        Example:
            >>> filename = Prompt.ask("Enter a filename")

        Args:
            prompt (TextType, optional): Prompt text. Defaults to "".
            console (Console, optional): A Console instance or None to use global console. Defaults to None.
            password (bool, optional): Enable password input. Defaults to False.
            choices (List[str], optional): A list of valid choices. Defaults to None.
            case_sensitive (bool, optional): Matching of choices should be case-sensitive. Defaults to True.
            show_default (bool, optional): Show default in prompt. Defaults to True.
            show_choices (bool, optional): Show choices in prompt. Defaults to True.
            stream (TextIO, optional): Optional text file open for reading to get input. Defaults to None.
        r&   r3   r2   r   )r4   r,   r'   r(   r$   r)   r*   r+   r3   r2   Z_promptr   r   r   r5   o   s    	r3   r   c                 C   s   t d| ddS )zTurn the supplied default in to a Text instance.

        Args:
            default (DefaultType): Default value.

        Returns:
            Text: Text containing rendering of default value.
        ()prompt.default)r   )r   r3   r   r   r   render_default   s    	zPromptBase.render_defaultc                 C   s   | j  }d|_| jrJ| jrJd| j}d| d}|d ||d |dkr| jrt|t	| j
fr|d | |}|| || j |S )zMake prompt text.

        Args:
            default (DefaultType): Default value.

        Returns:
            Text: Text to display in prompt.
        r%   /[] zprompt.choices.)r,   copyendr+   r$   joinappendr*   r/   r0   r#   r;   prompt_suffix)r   r3   r,   Z_choicesr$   _defaultr   r   r   make_prompt   s$    	




zPromptBase.make_prompt)r'   r,   r(   r2   r   c                 C   s   |j |||dS )zGet input from user.

        Args:
            console (Console): Console instance.
            prompt (TextType): Prompt text.
            password (bool): Enable password entry.

        Returns:
            str: String from user.
        )r(   r2   )input)r4   r'   r,   r(   r2   r   r   r   	get_input   s    zPromptBase.get_inputvaluer   c                 C   s>   | j dk	st| jr"| | j kS |  dd | j D kS )zCheck value is in the list of valid choices.

        Args:
            value (str): Value entered by user.

        Returns:
            bool: True if choice was valid, otherwise False.
        Nc                 S   s   g | ]}|  qS r   lower.0choicer   r   r   
<listcomp>   s     z+PromptBase.check_choice.<locals>.<listcomp>)r$   AssertionErrorr)   striprL   r   rJ   r   r   r   check_choice   s    	zPromptBase.check_choicec                 C   s   |  }z| |}W n tk
r4   t| jY nX | jdk	r| |sTt| j| js| | jdd | jD 	|
  }|S )a  Process response from user, convert to prompt type.

        Args:
            value (str): String typed by user.

        Raises:
            InvalidResponse: If ``value`` is invalid.

        Returns:
            PromptType: The value to be returned from ask method.
        Nc                 S   s   g | ]}|  qS r   rK   rM   r   r   r   rP      s     z/PromptBase.process_response.<locals>.<listcomp>)rR   r#   
ValueErrorr   validate_error_messager$   rT   illegal_choice_messager)   indexrL   )r   rJ   return_valuer   r   r   process_response   s    


zPromptBase.process_response)rJ   errorr   c                 C   s   | j | dS )zCalled to handle validation error.

        Args:
            value (str): String entered by user.
            error (InvalidResponse): Exception instance the initiated the error.
        N)r'   print)r   rJ   r[   r   r   r   on_validate_error  s    zPromptBase.on_validate_errorr   c                 C   s   dS )z,Hook to display something before the prompt.Nr   r    r   r   r   
pre_prompt  s    zPromptBase.pre_promptr2   )r2   r   c                C   s   d S r   r   )r   r2   r   r   r   __call__  s    zPromptBase.__call__)r3   r2   r   c                C   s   d S r   r   )r   r3   r2   r   r   r   r`     s    r6   c             
   C   s   |    | |}| j| j|| j|d}|dkr<|dkr<|S z| |}W n6 tk
r } z| || W Y q W 5 d}~X Y q X |S q dS )zRun the prompt loop.

        Args:
            default (Any, optional): Optional default value.

        Returns:
            PromptType: Processed value.
        r_   r%   .N)r^   rF   rH   r'   r(   rZ   r   r]   )r   r3   r2   r,   rJ   rY   r[   r   r   r   r`     s    

)r%   )r%   )r%   )r%   )N)$r   r   r   r   r0   r#   type__annotations__rV   rW   rD   r$   r   r   r   r   boolr   classmethodr	   r   r   r   r   r5   r   r   r;   rF   rH   rT   rZ   r   r]   r^   r`   r   r   r   r   r"      s   
 
 

 
 
' 	 
r"   c                   @   s   e Zd ZdZeZdS )PromptzbA prompt that returns a str.

    Example:
        >>> name = Prompt.ask("Enter your name")


    N)r   r   r   r   r0   r#   r   r   r   r   re   0  s   re   c                   @   s   e Zd ZdZeZdZdS )	IntPromptzA prompt that returns an integer.

    Example:
        >>> burrito_count = IntPrompt.ask("How many burritos do you want to order")

    z3[prompt.invalid]Please enter a valid integer numberN)r   r   r   r   intr#   rV   r   r   r   r   rf   <  s   rf   c                   @   s   e Zd ZdZeZdZdS )FloatPromptzyA prompt that returns a float.

    Example:
        >>> temperature = FloatPrompt.ask("Enter desired temperature")

    z%[prompt.invalid]Please enter a numberN)r   r   r   r   floatr#   rV   r   r   r   r   rh   H  s   rh   c                   @   sN   e Zd ZU dZeZdZddgZee	 e
d< eedddZe	ed	d
dZdS )ConfirmzuA yes / no confirmation prompt.

    Example:
        >>> if Confirm.ask("Continue"):
                run_job()

    z#[prompt.invalid]Please enter Y or Nynr$   r7   c                 C   s.   | j \}}t|rd| dn
d| dddS )z8Render the default as (y) or (n) rather than True/False.r8   r9   r:   r-   )r$   r   )r   r3   yesnor   r   r   r;   a  s    
zConfirm.render_defaultrI   c                 C   s.   |   }|| jkr t| j|| jd kS )zConvert choices to a bool.r   )rR   rL   r$   r   rV   rS   r   r   r   rZ   f  s    

zConfirm.process_responseN)r   r   r   r   rc   r#   rV   r$   r   r0   rb   r   r   r;   rZ   r   r   r   r   rj   T  s   
rj   __main__)r\   zRun [i]prompt[/i] tests?T)r3   z6:rocket: Enter a number between [b]1[/b] and [b]10[/b]   
   z=:pile_of_poo: [prompt.invalid]Number must be between 1 and 10znumber=z=Please enter a password [cyan](must be at least 5 characters))r(   z"[prompt.invalid]password too shortz	password=zEnter a fruitappleZorangepear)r$   zfruit=z'What's the best Dog? (Case INSENSITIVE)zBorder TerrierZCollieZLabradoodleF)r$   r)   zdoggie=z[b]OK :loudly_crying_face:N)'typingr   r   r   r   r   r   r   r	   r%   r   r'   r   textr   r   r   r   	Exceptionr   r   r"   r0   re   rg   rf   ri   rh   rc   rj   r   pip._vendor.richr\   r5   resultr(   lenZfruitZdoggier   r   r   r   <module>   sT   (  
 