"Te,ddlmZmZmZmZmZmZmZmZddl m Z ddl m Z ddl mZmZedZedZGdd eZGd d eZGd d eeZGddeeZGddeeZGddeeZGddeeZedk(rddlm Z ejCddr ejCddZ"e"dk\re"dkrn e d'e de" ejCd d!Z#e$e#dk\rn e d"(e d#e#ejCd$gd%&Z%e d'e%y)e d(y)y))*)AnyGenericListOptionalTextIOTypeVarUnionoverload) get_console)Console)TextTextType PromptType DefaultTypeceZdZdZy) PromptErrorz/Exception base class for prompt related errors.N)__name__ __module__ __qualname____doc__-/usr/lib/python3/dist-packages/rich/prompt.pyrr s9rrc,eZdZdZdeddfdZdefdZy)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. messagereturnNc||_yNr)selfrs r__init__zInvalidResponse.__init__s  rc|jSr r!r"s r__rich__zInvalidResponse.__rich__s ||r)rrrrrr#r&rrrrrs&T(rrceZdZUdZeZeed<dZdZ dZ dZ e e eed< d(dddd d d d ed e ed ede e edededdfdZee d(dddd d ddd ed e ed ede e edededede edeeeffdZee d(dddd d ddd ed e ed ede e ededede edefdZe d(dddd d dddd ed e ed ede e edededede edefdZdedefdZdedefdZe d)d ed ed ede edef dZdedefdZdedefdZded e ddfd!Z!d*d"Z"edd#de edefd$Z#edd#dede edeeeffd%Z#ddd&dede edefd'Z#y)+ 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/rc|xs t|_t|trt j |dn||_||_|||_||_ ||_ y)Nr0style) r r, isinstancestrr from_markupr0r-r*r.r/)r"r0r,r-r*r.r/s rr#zPromptBase.__init__5s`/+- &#&   V8 4 !  "DL((r)r,r-r*r.r/streamdefaultr7cyr r) clsr0r,r-r*r.r/r8r7s raskzPromptBase.askKs rcyr r)r:r0r,r-r*r.r/r7s rr;zPromptBase.ask[s r.)r,r-r*r.r/r8r7c2|||||||} | ||S)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+r8r7r) r:r0r,r-r*r.r/r8r7_prompts rr;zPromptBase.askjs/6 %%  wv66rc"td|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)r"r8s rrender_defaultzPromptBase.render_defaultsayN$455rc|jj}d|_|jrP|jrDdj |j}d|d}|j d|j |d|dk7r[|jrOt|t|jfr3|j d|j|}|j ||j |j|S)zMake prompt text. Args: default (DefaultType): Default value. Returns: Text: Text to display in prompt. /[] zprompt.choices.) r0copyendr/r*joinappendr.r4r5r)rD prompt_suffix)r"r8r0_choicesr*_defaults r make_promptzPromptBase.make_prompts!!#   xx -H(1oG MM#  MM'#3 4 sN!!7S$*<*<$=> MM# **73H MM( # d(() rc*|j|||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-r7)input)r:r,r0r-r7s r get_inputzPromptBase.get_inputs$}}Vhv}FFrvaluecV|jJ|j|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. )r*stripr"rVs r check_choicezPromptBase.check_choices)||'''{{} ,,rc|j} |j|}|j &|j |st|j|S#t$rt|jwxYw)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. )rXr) ValueErrorrvalidate_error_messager*rZillegal_choice_message)r"rV return_values rprocess_responsezPromptBase.process_responsesv  ?'+'9'9%'@L << #D,=,=e,D!$"="=> >  ?!$"="=> > ?s AA6errorc:|jj|y)zCalled to handle validation error. Args: value (str): String entered by user. error (InvalidResponse): Exception instance the initiated the error. N)r,print)r"rVras ron_validate_errorzPromptBase.on_validate_errors 5!rcy)z,Hook to display something before the prompt.Nrr%s r pre_promptzPromptBase.pre_promptsrr7cyr r)r"r7s r__call__zPromptBase.__call__s rcyr r)r"r8r7s rrizPromptBase.__call__s rr>c( |j|j|}|j|j||j|}|dk(r|dk7r|S |j |}|S#t $r}|j||Yd}~d}~wwxYw)zRun the prompt loop. Args: default (Any, optional): Optional default value. Returns: PromptType: Processed value. rgrF.N)rfrRrUr,r-r`rrd)r"r8r7r0rVr_ras rrizPromptBase.__call__s OO %%g.FNN4<<vNVE{w#~ $#44U; $# # &&ue4 sA,, B5B  B)rFr )rN)$rrrrr5r)type__annotations__r]r^rOr*rrrr boolr# classmethodr rrr rr;rrrDrRrUrZr`rrdrfrirrrr(r(s M4IKM#'GXd3i ')&*'+!!))'" )  ) $s)$ ))) ),   &*'+!!#'    '"    $s)$            {J& '       &*'+!!#'    '"    $s)$              "7&*'+!!#'"7"7'" "7  "7 $s)$ "7"7"7"7 "7 "7"7H 6k 6d 6;4> $( GGG G  G GG& -# -$ -cj."s"?"t";59 (6"2 j  BF % /7/? z;& '  *-$3$hv6F$RU$rr(ceZdZdZeZy)PromptzbA prompt that returns a str. Example: >>> name = Prompt.ask("Enter your name") N)rrrrr5r)rrrrqrqsMrrqceZdZdZeZdZy) 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)r]rrrrsrs*sMRrrsceZdZdZeZdZy) FloatPromptzyA prompt that returns a float. Example: >>> temperature = FloatPrompt.ask("Enter desired temperature") z%[prompt.invalid]Please enter a numberN)rrrrfloatr)r]rrrrvrv6sMDrrvcReZdZUdZeZdZddgZee e d<de de fdZ d e defd Zy ) ConfirmzuA yes / no confirmation prompt. Example: >>> if Confirm.ask("Continue"): run_job() z#[prompt.invalid]Please enter Y or Nynr*r8rc^|j\}}t|r d|ddSd|ddS)z8Render the default as (y) or (n) rather than True/False.rArBrCr2)r*r)r"r8yesnos rrDzConfirm.render_defaultOs:,,R'auAJ@PQQ2$ay@PQQrrVc|jj}||jvrt|j||jdk(S)zConvert choices to a bool.r)rXlowerr*rr]rYs rr`zConfirm.process_responseTsF ##%  $!$"="=> > Q''rN)rrrrrnr)r]r*rr5rmrrrDr`rrrryryBsOMBsGT#Y#RkRdR (c(d(rry__main__)rczRun [i]prompt[/i] tests?T)r8z6: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)&typingrrrrrrr r rFr r,r textrrrr Exceptionrrr(r5rqrtrsrwrvrnryrrichrcr;resultr-lenfruitrrrrsQQQ \ " m$ :): k }$$}$@ Z_  S 3 S E*U# E(j(4 z{{-t{<]]HRS#F{v| Q R  x !zzO"H8}! 6 7  (&' ?4O P ui ! *+7r