o 7]h+,@sddlmZmZmZmZmZmZmZmZddl m Z ddl m Z ddl mZmZedZedZGdd d eZGd d d eZGd d d eeZGdddeeZGdddeeZGdddeeZGdddeeZedkrddlmZej dddr ej dddZ!e!dkre!dkrnedqede! ej d dd!Z"e#e"dkrned"qed#e"ej d$gd%d&Z$ed'e$d)Sed(d)Sd)S)*)AnyGenericListOptionalTextIOTypeVarUnionoverload) get_console)Console)TextTextType PromptType DefaultTypec@seZdZdZdS) PromptErrorz/Exception base class for prompt related errors.N)__name__ __module__ __qualname____doc__rr9/usr/lib/python3/dist-packages/pip/_vendor/rich/prompt.pyr src@s0eZdZdZdeddfddZdefddZdS) 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. messagereturnNcCs ||_dSNr)selfrrrr__init__s zInvalidResponse.__init__cCs|jSrrrrrr__rich__szInvalidResponse.__rich__)rrrrrrr rrrrrsrc@seZdZUdZeZeed<dZdZ dZ dZ e e eed< d3dd dd d d d ed e edede e edededdfddZee d3dd dd d ddd ed e edede e edededede edeeeffddZee d3dd dd d ddd ed e edede e ededede edefddZe d3dd dd d dddd ed e edede e edededede edefddZdedefddZdedefdd Ze d4d ed edede edef d!d"Zd#edefd$d%Zd#edefd&d'Zd#ed(e ddfd)d*Z!d5d+d,Z"edd-de edefd.d/Z#edd-dede edeeeffd0d/Z#ddd1dede edefd2d/Z#dS)6 PromptBaseaAsk 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. 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: NchoicesFTconsolepasswordr# show_default show_choicespromptr&r'r(r)rcCsN|pt|_t|trtj|ddn||_||_|dur||_||_ ||_ dS)Nr*style) r r& isinstancestrr from_markupr*r'r#r(r))rr*r&r'r#r(r)rrrr5s  zPromptBase.__init__)r&r'r#r(r)streamdefaultr0c CdSrr) clsr*r&r'r#r(r)r1r0rrraskKszPromptBase.askcCr2rr)r3r*r&r'r#r(r)r0rrrr4[s .)r&r'r#r(r)r1r0c Cs |||||||d} | ||dS)aShortcut 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. 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%r1r0r) r3r*r&r'r#r(r)r1r0_promptrrrr4js cCstd|ddS)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 )rr1rrrrender_defaults zPromptBase.render_defaultcCs|j}d|_|jr%|jr%d|j}d|d}|d||d|dkrC|jrCt|t |j frC|d| |}||||j |S)zMake prompt text. Args: default (DefaultType): Default value. Returns: Text: Text to display in prompt. r$/[] zprompt.choices.) r*copyendr)r#joinappendr(r-r.r"r: prompt_suffix)rr1r*_choicesr#_defaultrrr make_prompts"          zPromptBase.make_promptcCs|j|||dS)zGet input from user. Args: console (Console): Console instance. prompt (TextType): Prompt text. password (bool): Enable password entry. Returns: str: String from user. )r'r0)input)r3r&r*r'r0rrr get_inputszPromptBase.get_inputvaluecCs|jdusJ||jvS)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. N)r#striprrIrrr check_choices zPromptBase.check_choicecCsR|}z||}Wn tyt|jw|jdur'||s't|j|S)aProcess 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. N)rJr" ValueErrorrvalidate_error_messager#rLillegal_choice_message)rrI return_valuerrrprocess_responses    zPromptBase.process_responseerrorcCs|j|dS)zCalled to handle validation error. Args: value (str): String entered by user. error (InvalidResponse): Exception instance the initiated the error. N)r&print)rrIrRrrron_validate_errorszPromptBase.on_validate_errorcCsdS)z,Hook to display something before the prompt.Nrrrrr pre_promptszPromptBase.pre_promptr0cCr2rr)rr0rrr__call__szPromptBase.__call__cCr2rr)rr1r0rrrrWsr5c Cs |||}|j|j||j|d}|dkr|dkr|Sz||}W|Sty?}z |||WYd}~qd}~ww)zRun the prompt loop. Args: default (Any, optional): Optional default value. Returns: PromptType: Processed value. TrVr$.N)rUrFrHr&r'rQrrT)rr1r0r*rIrPrRrrrrWs     )r$r)rN)$rrrrr.r"type__annotations__rNrOrCr#rrrr boolr classmethodr rrrrr4rr r:rFrHrLrQrrTrUrWrrrrr!s$                $      &r!c@seZdZdZeZdS)PromptzbA prompt that returns a str. Example: >>> name = Prompt.ask("Enter your name") N)rrrrr.r"rrrrr\sr\c@eZdZdZeZdZdS) 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)rrrrintr"rNrrrrr^*r^c@r]) FloatPromptzyA prompt that returns a float. Example: >>> temperature = FloatPrompt.ask("Enter desired temperature") z%[prompt.invalid]Please enter a numberN)rrrrfloatr"rNrrrrra6r`rac@sReZdZUdZeZdZddgZee e d<de de fdd Z d e defd d Zd S)ConfirmzuA yes / no confirmation prompt. Example: >>> if Confirm.ask("Continue"): run_job() z#[prompt.invalid]Please enter Y or Nynr#r1rcCs4|j\}}t|rd|dddSd|dddS)z8Render the default as (y) or (n) rather than True/False.r7r8r9r+)r#r )rr1yesnorrrr:Os *zConfirm.render_defaultrIcCs.|}||jvrt|j||jdkS)zConvert choices to a bool.r)rJlowerr#rrNrKrrrrQTs   zConfirm.process_responseN)rrrrrZr"rNr#rr.rYrr r:rQrrrrrcBs rc__main__)rSzRun [i]prompt[/i] tests?T)r1z6:rocket: Enter a number between [b]1[/b] and [b]10[/b] z=:pile_of_poo: [prompt.invalid]Number must be between 1 and 10znumber=z=Please enter a password [cyan](must be at least 5 characters))r'z"[prompt.invalid]password too shortz password=z Enter a fruit)appleorangepear)r#zfruit=z[b]OK :loudly_crying_face:N)%typingrrrrrrrr r$r r&r textr rrr Exceptionrrr!r.r\r_r^rarZrcrpip._vendor.richrSr4resultr'lenfruitrrrrsR(