Ë ÉßXû~ ¤�ãóÆ—dZddlZddlZddlmZddlmZmZmZm Z m Z m Z ddl m Z mZdZdZ ej"e«Zdd„Zdd „Zdd „Zd „Zd „Zefd „ZGd„de«Zy)a; Interactive terminal prompts. The :mod:`~humanfriendly.prompts` module enables interaction with the user (operator) by asking for confirmation (:func:`prompt_for_confirmation()`) and asking to choose from a list of options (:func:`prompt_for_choice()`). It works by rendering interactive prompts on the terminal. éN)Úinteractive_prompt)ÚHIGHLIGHT_COLORÚ ansi_stripÚ ansi_wrapÚconnected_to_terminalÚterminal_supports_colorsÚwarning)ÚformatÚ concatenate) Ú MAX_ATTEMPTSÚTooManyInvalidRepliesÚloggerÚprepare_friendly_promptsÚprepare_prompt_textÚprompt_for_choiceÚprompt_for_confirmationÚprompt_for_inputÚ retry_limité cóZ—t|d¬«}|rdn|�dnd}|dt|t¬«zz }tjd t |«j ««t «D]Ã}t|d |d¬ «}|j«d vrtjd |«y|j«dvrtjd|«y|s |�tjd|rdnd«|cS|rd|znd}tjd|rdnd||t«td|rdnd |¬«ŒÅy)aø Prompt the user for confirmation. :param question: The text that explains what the user is confirming (a string). :param default: The default value (a boolean) or :data:`None`. :param padding: Refer to the documentation of :func:`prompt_for_input()`. :returns: - If the user enters 'yes' or 'y' then :data:`True` is returned. - If the user enters 'no' or 'n' then :data:`False` is returned. - If the user doesn't enter any text or standard input is not connected to a terminal (which makes it impossible to prompt the user) the value of the keyword argument ``default`` is returned (if that value is not :data:`None`). :raises: - Any exceptions raised by :func:`retry_limit()`. - Any exceptions raised by :func:`prompt_for_input()`. When `default` is :data:`False` and the user doesn't enter any text an error message is printed and the prompt is repeated: >>> prompt_for_confirmation("Are you sure?") Are you sure? [y/n] Error: Please enter 'yes' or 'no' (there's no default choice). Are you sure? [y/n] The same thing happens when the user enters text that isn't recognized: >>> prompt_for_confirmation("Are you sure?") Are you sure? [y/n] about what? Error: Please enter 'yes' or 'no' (the text 'about what?' is not recognized). Are you sure? [y/n] T©Úboldz[Y/n]Nz[y/N]z[y/n]z %s )Úcolorz5Requesting interactive confirmation from terminal: %rÚ©ÚpaddingÚstrip)ÚyÚyesz#Confirmation granted by reply (%r).)ÚnÚnoz"Confirmation denied by reply (%r).Fú,Default choice selected by empty reply (%r).ÚgrantedÚdeniedzthe text '%s' is not recognizedúthere's no default choiceú&Got %s reply (%s), retrying (%i/%i) ..ÚinvalidÚemptyz6{indent}Error: Please enter 'yes' or 'no' ({details}).Ú )ÚindentÚdetails) rrrÚdebugrÚrstriprrÚlowerr r )ÚquestionÚdefaultrÚ prompt_textÚhintÚattemptÚreplyr+s ú:/usr/lib/python3.12/site-packages/humanfriendly/prompts.pyrr6s €ôL& h°TÔ:€Ká‰7¨gÐ.A¡7Àw€DØ�6Ô/°¼OÔLÑLÑL€Kä ‡L�LÐHÌ*ÐU`ÓJa×JhÑJhÓJjÔkÜ–=ˆÜ  ¨b¸'ÈÔNˆØ �;‰;‹=˜LÑ (Ü �L‰LÐ>ÀÔ FÙØ �[‰[‹]˜kÑ )Ü �L‰LÐ=¸uÔ EÙÙ˜WÐ0Ü �L‰LÐGÙ&-™°8ô =àŠNñ ð9¸5Ò@Ø%@ð ä �L‰LÐAÙ&+™°¸'Ø ¤,ô 0ô ÐLÙ")™3¨r¸7ö Dñ%!óc ó>—|rdnd}t|«}t|«dk(rtjd|d«|dS|s t d«‚|rdndj dj t |d¬ «D��cgc]\}}d ||fz||k(rd ndz‘Œc}}«d g«}t|d ¬«}tjdttt|«««t«D�]²}t|d|d ¬«}|s|�tjd|«|cS|j«rFt|«dz } d| cxkrt|«kr$nn!tjd|| |«|| cSg} |D]h}|j«} |j«} | | k(rtjd|«|ccS| | vsŒIt| «dkDsŒX| j!|«Œjt| «dk(r!tjd| d|«| dcS| rt#d|t| ««} nG|j«rt#dt|««} n!|r|j%«s t#d|«} nd} tjd|rdnd| |t&«t)d|| «�Œµycc}}w)a Prompt the user to select a choice from a group of options. :param choices: A sequence of strings with available options. :param default: The default choice if the user simply presses Enter (expected to be a string, defaults to :data:`None`). :param padding: Refer to the documentation of :func:`~humanfriendly.prompts.prompt_for_input()`. :returns: The string corresponding to the user's choice. :raises: - :exc:`~exceptions.ValueError` if `choices` is an empty sequence. - Any exceptions raised by :func:`~humanfriendly.prompts.retry_limit()`. - Any exceptions raised by :func:`~humanfriendly.prompts.prompt_for_input()`. When no options are given an exception is raised: >>> prompt_for_choice([]) Traceback (most recent call last): File "humanfriendly/prompts.py", line 148, in prompt_for_choice raise ValueError("Can't prompt for choice without any options!") ValueError: Can't prompt for choice without any options! If a single option is given the user isn't prompted: >>> prompt_for_choice(['only one choice']) 'only one choice' Here's what the actual prompt looks like by default: >>> prompt_for_choice(['first option', 'second option']) 1. first option 2. second option Enter your choice as a number or unique substring (Control-C aborts): second 'second option' If you don't like the whitespace (empty lines and indentation): >>> prompt_for_choice(['first option', 'second option'], padding=False) 1. first option 2. second option Enter your choice as a number or unique substring (Control-C aborts): first 'first option' r)réz=Skipping interactive prompt because there's only option (%r).rz,Can't prompt for choice without any options!z Ú )Ústartz %i. %sz (default choice)zFEnter your choice as a number or unique substring (Control-C aborts): Trz=Requesting interactive choice on terminal (options are %s) ..rNr"z+Option (%r) selected by numeric reply (%s).z,Option (%r) selected by reply (exact match).z6Option (%r) selected by reply (substring match on %r).z*text '%s' matches more than one choice: %sznumber %i is not a valid choicez#text '%s' doesn't match any choicesr%r&r'r(z%sError: Invalid input (%s).)ÚlistÚlenrr,Ú ValueErrorÚjoinÚ enumeraterr ÚmapÚreprrrÚisdigitÚintr.Úappendr Úisspacer r )Úchoicesr0rr*ÚiÚchoicer1r3r4ÚindexÚmatchesÚ lower_replyÚ lower_choicer+s r5rrxs~€ñ`‰S €Fä�7‹m€GÜ ˆ7ƒ|�qÒä� ‰ ÐTÐV]Ð^_ÑV`ÔaØ�q‰zÐÙ äÐGÓHÐHá$‘6¨$×4Ñ4à � ‰ ä& w°aÕ8ô á8‘ ��6ð˜1˜f˜+Ñ %ÀÈ7ÒARÑ*=ÐXZÓ [Ø8ò ó ð Qð6ó€Kô& k¸Ô=€Kä ‡L�LÐPÜœS¤ wÓ/Ó0ô2ä—=ˆÜ  ¨b¸'ÈÔNˆÙ˜Ð,Ü �L‰LÐGÈÔ QØŠNØ �]‰]Œ_ܘ“J ‘NˆEØ�EÔ(œC ›LÕ(Ü— ‘ ÐJÈGÐTYÉNÐ\aÔbؘu‘~Ò%àˆÛˆFØŸ+™+›-ˆKØ!Ÿ<™<›>ˆLؘlÒ*ä— ‘ ÐKÈVÔTØ” Ø  Ò,´°[Ó1AÀAÓ1Eà—‘˜vÕ&ðô ˆw‹<˜1Ò ä �L‰LÐQÐSZÐ[\ÑS]Ð_dÔ eؘ1‘:Ò ñÜ Ð!MÈuÔVaÐbiÓVjÓk‘Ø—‘”Ü Ð!BÄCÈÃJÓO‘Ù˜uŸ}™}œÜ Ð!FÈÓN‘à5�Ü �L‰LÐAÙ&+™°¸'Ø ¤,ô 0ô Ð2°F¸GÖ DñO!ùó sÁ5JcóX—t«d} |rd|z}|jdd«} t|«}|€tjj d«|r tjj d« |�|s|S|j«S#|€tjj d«|r tjj d«wwxYw#t $rL}t|t«r|�tjd|«|cYd}~Stjdd¬«‚d}~wwxYw)aü Prompt the user for input (free form text). :param question: An explanation of what is expected from the user (a string). :param default: The return value if the user doesn't enter any text or standard input is not connected to a terminal (which makes it impossible to prompt the user). :param padding: Render empty lines before and after the prompt to make it stand out from the surrounding text? (a boolean, defaults to :data:`True`) :param strip: Strip leading/trailing whitespace from the user's reply? :returns: The text entered by the user (a string) or the value of the `default` argument. :raises: - :exc:`~exceptions.KeyboardInterrupt` when the program is interrupted_ while the prompt is active, for example because the user presses Control-C_. - :exc:`~exceptions.EOFError` when reading from `standard input`_ fails, for example because the user presses Control-D_ or because the standard input stream is redirected (only if `default` is :data:`None`). .. _Control-C: https://en.wikipedia.org/wiki/Control-C#In_command-line_environments .. _Control-D: https://en.wikipedia.org/wiki/End-of-transmission_character#Meaning_in_Unix .. _interrupted: https://en.wikipedia.org/wiki/Unix_signal#SIGINT .. _standard input: https://en.wikipedia.org/wiki/Standard_streams#Standard_input_.28stdin.29 Nr9z z6Got EOF from terminal, returning default value (%r) ..z0Interactive prompt was interrupted by exception!T)Úexc_info) rÚreplacerÚsysÚstderrÚwriteÚ BaseExceptionÚ isinstanceÚEOFErrorrr,r r)r/r0rrr4Úes r5rrês€ô6ÔØ €Eð'á Ø˜h‘ˆHØ×'Ñ'¨¨eÓ4ˆHð 'Ü& xÓ0ˆEàˆ}ô— ‘ × Ñ  Ô&Ùô — ‘ × Ñ  Õ&ðС5ðˆà�{‰{‹}ÐøðKˆ}ô— ‘ × Ñ  Ô&Ùô — ‘ × Ñ  Õ&ð ûô ò Ü �aœÔ " wÐ':ô �L‰LÐQÐSZÔ [Ø�Nô �N‰NÐMÐX\ˆNÔ ]Ø ûð ús;ŽC¨ B ³AC ACÃCà D)Ã)D$ÄD)Ä D$Ä$D)c ó‚—ttj«r%t|fdt tj «i|¤ŽS|S)aÿ Wrap a text to be rendered as an interactive prompt in ANSI escape sequences. :param prompt_text: The text to render on the prompt (a string). :param options: Any keyword arguments are passed on to :func:`.ansi_wrap()`. :returns: The resulting prompt text (a string). ANSI escape sequences are only used when the standard output stream is connected to a terminal. When the standard input stream is connected to a terminal any escape sequences are wrapped in "readline hints". Úreadline_hints)rrPÚstdoutrrÚstdin)r1Úoptionss r5rr8s<€ô(¬¯ © Ô3ô �kÑ ^Ô2GÌÏ É Ó2RÐ ^ÐV]Ñ ^ðàðr6có,— ddl}y#t$rYywxYw)u¤ Make interactive prompts more user friendly. The prompts presented by :func:`python2:raw_input()` (in Python 2) and :func:`python3:input()` (in Python 3) are not very user friendly by default, for example the cursor keys (:kbd:`â†�`, :kbd:`↑`, :kbd:`→` and :kbd:`↓`) and the :kbd:`Home` and :kbd:`End` keys enter characters instead of performing the action you would expect them to. By simply importing the :mod:`readline` module these prompts become much friendlier (as mentioned in the Python standard library documentation). This function is called by the other functions in this module to enable user friendly prompts. rN)ÚreadlineÚ ImportError)r]s r5rrIs€ð ÜøÜ ò á ð ús ‚‡ ’c#ó†K—t|«D] }|dz–—Œ d}||z}tj|«t|«‚­w)a  Allow the user to provide valid input up to `limit` times. :param limit: The maximum number of attempts (a number, defaults to :data:`MAX_ATTEMPTS`). :returns: A generator of numbers starting from one. :raises: :exc:`TooManyInvalidReplies` when an interactive prompt receives repeated invalid input (:data:`MAX_ATTEMPTS`). This function returns a generator for interactive prompts that want to repeat on invalid input without getting stuck in infinite loops. r8zTReceived too many invalid replies on interactive prompt, giving up! (tried %i times))Úrangerr r )ÚlimitrGÚmsgÚ formatted_msgs r5rr_sDèø€ô�5Ž\ˆØ�!‰e‹ ðà `€Cؘ%‘K€Mä ‡N�N�=Ô!ä   Ó .Ð.ùs‚?Acó—eZdZdZy)r zLRaised by interactive prompts when they've received too many invalid inputs.N)Ú__name__Ú __module__Ú __qualname__Ú__doc__©r6r5r r vs„âVr6r )NT)NTT)rhÚloggingrPÚhumanfriendly.compatrÚhumanfriendly.terminalrrrrrr Úhumanfriendly.textr r Ú__all__r Ú getLoggerrerrrrrrrÚ Exceptionr rir6r5ÚrqsƒðñóÛ õ4÷÷÷3ð €ð€ ØWð ˆ× Ñ ˜8Ó $€ó?DóDoEódKò\ò" ð,#ó/ô.W˜IõWr6