
    E^e2              	       ,   d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZ d!d
Z G d de	          Zd!dZd Zd!dZdZd"dZ G d de
e                   Zd!dZd ZdedefdZd Z	 d!dZd#dZ d#dede!de"de!fd Z#dS )$z2Functions for prompting the user for project info.    N)OrderedDict)Path)PromptConfirm
PromptBaseInvalidResponse)UndefinedError)StrictEnvironment)UndefinedVariableInTemplate c                     |r&| |                                 v r||          r||          n| }	 t          j        | | |          }|n|S )zPrompt user for variable and return the entered value or given default.

    :param str var_name: Variable of the context to query the user
    :param default_value: Value that will be returned if no input happens
    Tdefault)keysr   ask)var_namedefault_valuepromptsprefixquestionvariables         3lib/python3.11/site-packages/cookiecutter/prompt.pyread_user_variabler      sw     	7<<>>11gh6G1 	 :333]KKK
 O    c                   2    e Zd ZdZg dZg dZdedefdZdS )YesNoPromptz5A prompt that returns a boolean for yes/no questions.)1truetyesyon)0falsefnonoffvaluereturnc                     |                                                                 }|| j        v rdS || j        v rdS t	          | j                  )zConvert choices to a bool.TF)striploweryes_choices
no_choicesr   validate_error_messageselfr)   s     r   process_responsezYesNoPrompt.process_response(   sQ    ##%%D$$$4do%%5!$"=>>>r   N)	__name__
__module____qualname____doc__r.   r/   strboolr3    r   r   r   r   "   sR        ??666K666J?c ?d ? ? ? ? ? ?r   r   c                     |r&| |                                 v r||          r||          n| }t                              | | |          S )a  Prompt the user to reply with 'yes' or 'no' (or equivalent values).

    - These input values will be converted to ``True``:
      "1", "true", "t", "yes", "y", "on"
    - These input values will be converted to ``False``:
      "0", "false", "f", "no", "n", "off"

    Actual parsing done by :func:`prompt`; Check this function codebase change in
    case of unexpected behaviour.

    :param str question: Question to the user
    :param default_value: Value that will be returned if no input happens
    r   )r   r   r   )r   r   r   r   r   s        r   read_user_yes_nor<   3   sa      	7<<>>11gh6G1 	 
 ??f0h00-?HHHr   c                 .    t          j        | d          S )zXPrompt the user to enter a password.

    :param str question: Question to the user
    T)password)r   r   )r   s    r   read_repo_passwordr?   I   s    
 :h....r   c                     t          |t                    st          |st          t	          d t          |d          D                       }|                                }d  }d |                                D             }rs                                 v r]t                    t                    r	          }n9d          v r          d         } fd|                                D             }d	                    | | d	                    |          df          }t          j        |t          |          t          |          d	         
          }	||	         S )aa  Prompt the user to choose from several options for the given variable.

    The first item will be returned if no input happens.

    :param str var_name: Variable as specified in the context
    :param list options: Sequence of options that are available to select from
    :return: Exactly one item of ``options`` that has been chosen by the user
    c              3   &   K   | ]\  }}| |fV  d S Nr:   ).0ir)   s      r   	<genexpr>z#read_user_choice.<locals>.<genexpr>`   s,      SSEfe_SSSSSSr      zSelect c                 "    g | ]} d j         | S )z%    [bold magenta]{}[/] - [bold]{}[/])format)rC   cs     r   
<listcomp>z$read_user_choice.<locals>.<listcomp>d   s1       ?@6/6:  r   
__prompt__c                 f    g | ]-\  }}|         v rd | d         |          dnd | d| d.S )z    [bold magenta]z[/] - [bold]z[/]r:   )rC   rD   pr   r   s      r   rJ   z$read_user_choice.<locals>.<listcomp>o   sx        Aq ))) NQMMGH4Ea4HMMMM?!?????  r   
z    Choose fromr   )choicesr   )
isinstancelist	TypeError
ValueErrorr   	enumerater   itemsr8   joinr   r   )
r   optionsr   r   
choice_maprO   r   choice_linespromptuser_choices
   ` `       r   read_user_choicer\   Q   s    gt$$  SSYwPQ=R=RSSSSSJooG###H DNDTDTDVDV  L
  8w||~~--gh'-- 
	x(HHwx000"8,\:     ',,..	  L YY!x!!IIl##	
 F *VT']]DMMRSDTUUUKk""r   r   c                     	 t          j        | t                    }n"# t          $ r}t	          d          |d}~ww xY wt          |t                    st	          d          |S )ztLoad user-supplied value as a JSON dict.

    :param str user_value: User-supplied value to load as a JSON dict
    )object_pairs_hookzUnable to decode to JSON.NzRequires JSON dict.)jsonloadsr   	Exceptionr   rP   dict)
user_valuer   	user_dicterrors       r   process_jsonrf      s{    
FJz[III		 F F F9::EF i&& 53444s    
=8=c                   .    e Zd ZdZdZeZdZdedefdZ	dS )
JsonPromptz.A prompt that returns a dict from JSON string.Nz2[prompt.invalid]  Please enter a valid JSON stringr)   r*   c                 ,    t          || j                  S )zConvert choices to a dict.)rf   r   r1   s     r   r3   zJsonPrompt.process_response   s    E4<000r   )
r4   r5   r6   r7   r   rb   response_typer0   r8   r3   r:   r   r   rh   rh      sL        88GMQ1c 1d 1 1 1 1 1 1r   rh   c                     t          |t                    st          |r&| |                                v r||          r||          n| }t                              | | dt           d|d          }|S )zPrompt the user to provide a dictionary of data.

    :param str var_name: Variable as specified in the context
    :param default_value: Value that will be returned if no input is provided
    :return: A Python dictionary to use in the context.
    z [cyan bold](z)[/]F)r   show_default)rP   rb   rR   r   rh   r   DEFAULT_DISPLAY)r   r   r   r   r   rc   s         r   read_user_dictrn      s     mT**  	7<<>>11gh6G1 	 
 ?8??/???    J
 r   c                     |t          |t                    r|S t          |t                    r! fd|                                D             S t          |t                    r fd|D             S t          |t
                    st          |          }                     |          }|                              S )a  Render the next variable to be displayed in the user prompt.

    Inside the prompting taken from the cookiecutter.json file, this renders
    the next variable. For example, if a project_name is "Peanut Butter
    Cookie", the repo_name could be be rendered with:

        `{{ cookiecutter.project_name.replace(" ", "_") }}`.

    This is then presented to the user as the default.

    :param Environment env: A Jinja2 Environment object.
    :param raw: The next value to be prompted for by the user.
    :param dict cookiecutter_dict: The current context as it's gradually
        being populated with variables.
    :return: The rendered value for the default variable.
    Nc                 X    i | ]&\  }}t          |          t          |          'S r:   render_variable)rC   kvcookiecutter_dictenvs      r   
<dictcomp>z#render_variable.<locals>.<dictcomp>   sP     
 
 
 1 C$566Q)9 9
 
 
r   c                 2    g | ]}t          |          S r:   rq   )rC   rt   ru   rv   s     r   rJ   z#render_variable.<locals>.<listcomp>   s&    HHHqQ(9::HHHr   )cookiecutter)rP   r9   rb   rU   rQ   r8   from_stringrender)rv   rawru   templates   ` ` r   rr   rr      s    " {jd++{
	C		 

 
 
 
 
 			
 
 
 	
 
C		 HHHHHCHHHHS!! #hhs##H??(9?:::r   rW   r*   c                     ddi}|                                  D ]S\  }}t          |                    d|                    }|                    d|          }||k    r|n| d| d}|||<   T|S )z@Process template options and return friendly prompt information.rK   zSelect a templatetitledescriptionz ())rU   r8   get)rW   r   
option_keyoption_valuer   r   labels          r   _prompts_from_optionsr      s    01G$+MMOO $ $ 
LL$$Wj99::"&&}jAA+--e3M3M{3M3M3M#
Nr   c                     t          |                                          }dt          |          i}|r|d         nt          | ||d          S )Prompt user with a set of options to choose from.

    :param no_input: Do not prompt for user input and return the first available option.
    	templatesr   r   )rQ   r   r   r\   )keyrW   no_inputoptsr   s        r   prompt_choice_for_templater      sN    
 D1'::;GL477$4S$$L$LLr   c                 \      fd|D             }|r|d         S t          ||||          S )r   c                 2    g | ]}t          |          S r:   rq   )rC   r|   ru   rv   s     r   rJ   z,prompt_choice_for_config.<locals>.<listcomp>   s&    XXXS2CDDXXXr   r   )r\   )ru   rv   r   rW   r   r   r   rendered_optionss   ``      r   prompt_choice_for_configr      sM     YXXXXPWXXX #""C!17FCCCr   Fc           
      `   t          g           }t          |           }| d                             di           }d}| d                                         }d |D             }t	          |          }|D ]_\  }	}
|	                    d          r|	                    d          s|
||	<   6|	                    d          rt          ||
|          ||	<   `t          |
t                    s|dz  }d	| d
| d}	 t          |
t                    rt          |||	|
|||          }|||	<   nt          |
t                    r-|rt          ||
|          ||	<   nUt          |	|
||          ||	<   n?t          |
t                    s*t          ||
|          }|st          |	|||          }|||	<   7# t          $ r}d|	 d}t          |||           |d}~ww xY w| d                                         D ]\  }	}
|	                    d          r|	                    d          s0	 t          |
t                    rM|dz  }d	| d
| d}t          ||
|          }|s'|	                    d          st!          |	|||          }|||	<   # t          $ r}d|	 d}t          |||           |d}~ww xY w|S )zPrompt user to enter a new config.

    :param dict context: Source for field names and sample values.
    :param no_input: Do not prompt for user input and use only values from context.
    contextry   __prompts__r   c                 B    g | ]\  }}|                     d           |S )_)
startswith)rC   rs   r   s      r   rJ   z%prompt_for_config.<locals>.<listcomp>  s-    JJJTQS8I8IJqJJJr   r   __rF   z  [dim][/z][/] zUnable to render variable ''N)r   r
   poprU   lenr   rr   rP   rb   rQ   r   r9   r<   r   r	   r   rn   )r   r   ru   rv   r   countall_promptsvisible_promptssizer   r|   r   valerrmsgs                  r   prompt_for_configr      s~    $B
G
,
,
,Cn%))-<<G
 E.)//11KJJ[JJJOD %J %JS>># 	s~~d';'; 	%(c"^^D!! 	%4S#?P%Q%Qc"#t$$ 	4QJE333333F	J#t$$ -.%sCh  *-!#&&C&& - Y-<S"3. .%c** .>c3QW-X-X%c**T** -%c30ABB H,S#wGGC),!#& 	J 	J 	J6666C-c3@@cI	J
 N+1133 J JS>># 	s~~d';'; 		J#t$$ 	-
7E77D777%c30ABB Dt(<(< D(c7FCCC),!#& 	J 	J 	J6666C-c3@@cI	J s2   9B1F,,
G6GG!A"J
J+J&&J+r   repo_dirr   c           	         t          g           }t          |           }d}| d                             di           }d}| d                             |i           }|r t	          |||          }	||	         d         }
n[d}| d                             |g           }t          |||||||          }	t          j        d|	                              d	          }
|
rt          |
          nd
}
|
r|

                                rt          d          t          |                                          }||
z                                  }| S )a!  Prompt user to select the nested template to use.

    :param context: Source for field names and sample values.
    :param repo_dir: Repository directory.
    :param no_input: Do not prompt for user input and use only values from context.
    :returns: Path to the selected template.
    r   r   ry   r   r   pathr}   z\((.+)\)rF   NzIllegal template path)r   r
   r   r   r   r   researchgroupr   is_absoluterS   resolve)r   r   r   ru   rv   r   r   r   configr   r}   template_paths               r   choose_nested_templater   P  s[    $B
G
,
,
,CFn%))-<<G
C^$((b11F 8(fh??#;v& (,,S"55&sC7F
 
 9[#..44Q77!)3tH~~~tH 2X1133 20111H~~%%''H(1133Mr   )Nr   rB   )F)$r7   r_   r   collectionsr   pathlibr   rich.promptr   r   r   r   jinja2.exceptionsr	   cookiecutter.environmentr
   cookiecutter.exceptionsr   r   r   r<   r?   r\   rm   rf   rb   rh   rn   rr   r   r   r   r   r8   r9   r   r:   r   r   <module>r      s/   8 8  				 # # # # # #       D D D D D D D D D D D D , , , , , , 6 6 6 6 6 6 ? ? ? ? ? ?   (? ? ? ? ?' ? ? ?"I I I I,/ / /.# .# .# .#b    $	1 	1 	1 	1 	1D! 	1 	1 	1   .!; !; !;H4 D    M M M JL
D 
D 
D 
DM M M M`" "D "C "4 "TW " " " " " "r   