
    E^eZA                     R   d Z ddlZddlZddlZddlZddlZddlZddlmZ ddl	m
Z
 ddlmZ ddlmZmZ ddlmZ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m Z m!Z!  ej"        e#          Z$d Z%dddZ&	 d dZ'd!dZ(	 d!de)de*dddede+f
dZ,d Z-d Z.	 	 	 	 	 	 d"dZ/dS )#z;Functions for generating a project from a project template.    N)OrderedDict)Path)	is_binary)FileSystemLoaderEnvironment)TemplateSyntaxErrorUndefinedError)StrictEnvironment)ContextDecodingExceptionNonTemplatedInputDirExceptionOutputDirExistsExceptionUndefinedVariableInTemplate)find_template)run_hook_from_repo_dir)make_sure_path_existsrmtreework_inc                 ~    	 |d         d         D ]}t          j         | |          r dS n# t          $ r Y dS w xY wdS )aE  Check whether the given `path` should only be copied and not rendered.

    Returns True if `path` matches a pattern in the given `context` dict,
    otherwise False.

    :param path: A file-system path referring to a file or dir that
        should be rendered or just copied.
    :param context: cookiecutter context.
    cookiecutter_copy_without_renderTF)fnmatchKeyError)pathcontextdont_renders      5lib/python3.11/site-packages/cookiecutter/generate.pyis_copy_only_pathr      sq    ">23IJ 	 	Kt[11 tt	    uu 5s   &, , 
::Fin_dictionary_variablec          	      t   |                                 D ]!\  }}|| vr| |         }t          |t                    r|r|| |<   0t          |t                    rLt          |                              t          |                    r|| |<   zt          | d| d|           ||v r,|                    |           |                    d|           t          | d| d| d          t          |t                    r.t          |t                    rt          ||d           || |<   || |<   #d	S )
zAModify the given context in place based on the overwrite_context.z$ provided for multi-choice variable z, but valid choices are r   z provided for choice variable z, but the choices are .Tr   N)
items
isinstancelistsetissubset
ValueErrorremoveinsertdictapply_overwrites_to_context)r   overwrite_contextr   variable	overwritecontext_values         r   r+   r+   0   s     16688 )* )*)7"")mT** #	*% $-!)T**  y>>**3}+=+=>> (1GH%%$$ M M#M M=JM M   -- "((333!((I6666$$ L L#L L;HL L L   t,, 	*It1L1L 	*'y    !.GH !*GHS)* )*    cookiecutter.jsonc                    t          g           }	 t          | d          5 }t          j        |t                     }ddd           n# 1 swxY w Y   nY# t          $ rL}t
          j                            |           }t          |          }d| d| d}	t          |	          |d}~ww xY wt
          j        
                    |           d         }
|

                    d	          d
         }|||<   |r@	 t          ||           n.# t          $ r!}t          j        d|            Y d}~nd}~ww xY w|rt          ||           t                              d|           |S )a  Generate the context for a Cookiecutter project template.

    Loads the JSON file as a Python object, with key being the JSON filename.

    :param context_file: JSON file containing key/value pairs for populating
        the cookiecutter's variables.
    :param default_context: Dictionary containing config to take into account.
    :param extra_context: Dictionary containing configuration overrides
    utf-8encoding)object_pairs_hookNz#JSON decoding error while loading 'z'. Decoding error details: ''   r!   r   zInvalid default received: zContext generated is %s)r   openjsonloadr'   osr   abspathstrr   splitr+   warningswarnloggerdebug)context_filedefault_contextextra_contextr   file_handleobje
full_fpathjson_exc_messageour_exc_message	file_name	file_stemerrors                r   generate_contextrP   `   s    "ooG?,111 	H[)K;GGGC	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	? 	? 	? W__\22
q66<* < <(8< < < 	 '77Q>	? l++A.I$$Q'IGI  @	@'_==== 	@ 	@ 	@M>u>>????????	@ 8#C777
LL*G444NsR   A A
A 
AA AA 
B, AB''B,7D 
D3D..D3c                 @   t                               d|           |                    |          }t          j                            |  |j        di |          }t          j                            |          }|rt                               d|           dS |r<t          j                            |          rt                               d|           dS t                               d|           t                               d|           t          |          rHt                               d||           t          j        ||           t          j        ||           dS |                    t          j        j        d          }	 |                    |          }	n# t           $ r}
d	|
_         d}
~
ww xY w |	j        di |}|d
                             dd	          r7|d
         d         }t                               dt'          |                     nt)          |d          5 }|                                 ddd           n# 1 swxY w Y   t-          |j        t0                    r|j        d         n|j        }t                               dt'          |                     t                               d|           t)          |dd|          5 }|                    |           ddd           n# 1 swxY w Y   t          j        ||           dS )a/  Render filename of infile as name of outfile, handle infile correctly.

    Dealing with infile appropriately:

        a. If infile is a binary file, copy it over without rendering.
        b. If infile is a text file, render its contents and write the
           rendered infile to outfile.

    Precondition:

        When calling `generate_file()`, the root template dir must be the
        current working directory. Using `utils.work_in()` is the recommended
        way to perform this directory change.

    :param project_dir: Absolute path to the resulting generated project.
    :param infile: Input file to generate the file from. Relative to the root
        template dir.
    :param context: Dict for populating the cookiecutter's variables.
    :param env: Jinja2 template execution environment.
    zProcessing file %sz$The resulting file name is empty: %sNz%The resulting file already exists: %szCreated file at %sz Check %s to see if it's a binaryz)Copying binary %s to %s without rendering/Fr   
_new_linesz%Using configured newline character %sr3   r4   r   z#Using detected newline character %szWriting contents to file %sw)r5   newline )rB   rC   from_stringr<   r   joinrenderisdirexistsr   shutilcopyfilecopymodereplacesepget_templater   
translatedgetreprr9   readliner#   newlinestuplewrite)project_dirinfiler   envskip_if_file_existsoutfile_tmploutfilefile_name_is_emptyinfile_fwd_slashestmpl	exceptionrendered_filerU   rdfhs                  r   generate_filerv      s^   * LL%v... ??6**Lgll;(;(;(F(Fg(F(FGGGw// ;WEEE rw~~g66 <gFFF
LL%w/// LL3V<<< @&'RRR((((((  S99 233     %		
  DK**'**M~""<77 K.),7<d7mmLLLL
 &7+++ 	rKKMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	$.r{E$B$BS"+a..:DMMJJJ
LL.888	gsWg	>	>	>  "
                              OFG$$$$$s<   F! !
F8+F33F8,IIIK>>LLdirnamer   
output_dirzos.PathLike[str]environmentoverwrite_if_existsc                 X   |                     |           } |j        di |}t          ||          }t                              d||           |                                }|r3|rt                              d|           n$d| d}	t          |	          t          |           || fS )zBRender name of a directory, create the directory, return its path.z+Rendered dir %s must exist in output_dir %sz2Output directory %s already exists, overwriting itzError: "z" directory already existsrV   )rW   rY   r   rB   rC   r[   r   r   )
rw   r   rx   ry   rz   	name_tmplrendered_dirnamedir_to_createoutput_dir_existsmsgs
             r   render_and_create_dirr      s     ''00I'y'22'22%566M
LL5}j   &,,.. 	- 	0LLDm    G]FFFC*3///m,,,////r0   c                 $    d| v rd| v rdS t           )z2Ensure that dirname is a templated directory name.z{{z}}T)r   )rw   s    r   ensure_dir_is_templatedr     s    w47??t++r0   c                 b    t          j        dt          d           t          | ||||           dS )a|  Run hook from repo directory, clean project directory if hook fails.

    :param repo_dir: Project template input directory.
    :param hook_name: The hook to execute.
    :param project_dir: The directory to execute the script from.
    :param context: Cookiecutter project context.
    :param delete_project_on_failure: Delete the project directory on hook
        failure?
    zmThe '_run_hook_from_repo_dir' function is deprecated, use 'cookiecutter.hooks.run_hook_from_repo_dir' instead   N)r@   rA   DeprecationWarningr   )repo_dir	hook_nameri   r   delete_project_on_failures        r   _run_hook_from_repo_dirr   
  sN     M	B		   )['3L    r0   r!   Tc           
      2
   t          |           }t                              d|           |pt          g           }|                    di                               di           }t
          j                            |          d         }	t          |	           t          d|dd|}
	 t          |	|||
|          \  }}n*# t          $ r}d|	 d}t          |||          |d	}~ww xY wt
          j                            |          }t                              d
|           |o| }|rt          | d|||           t          |          5  t!          ddg          |
_        t          j        d          D ]o\  }}}g }g }|D ]}t
          j                            t
          j                            ||                    }t+          ||          r1t                              d|           |                    |           |                    |           |D ]}t
          j                            t
          j                            ||                    }t
          j                            t
          j                            ||                    } |
                    |          j        di |}t                              d||           t
          j                            |          rt5          j        |           t5          j        ||           ||d	d	<   |D ]}t
          j                            |||          }		 t          |	|||
|           8# t          $ rN}|rt7          |           t
          j                            |	|          }d| d}t          |||          |d	}~ww xY w|D ]'}t
          j                            t
          j                            ||                    }t+          ||          r|
                    |          } |j        di |}t
          j                            ||          }t                              d||           t5          j        ||           t5          j        ||           	 tA          ||||
|           # t          $ r.}|rt7          |           d| d}t          |||          |d	}~ww xY wq	 d	d	d	           n# 1 swxY w Y   |rt          | d|||           |S )a}  Render the templates and saves them to files.

    :param repo_dir: Project template input directory.
    :param context: Dict for populating the template's variables.
    :param output_dir: Where to output the generated project dir into.
    :param overwrite_if_exists: Overwrite the contents of the output directory
        if it exists.
    :param skip_if_file_exists: Skip the files in the corresponding directories
        if they already exist
    :param accept_hooks: Accept pre and post hooks if set to `True`.
    :param keep_project_on_failure: If `True` keep generated project directory even when
        generation fails
    zGenerating project from %s...r   _jinja2_env_varsr8   T)r   keep_trailing_newlinez$Unable to create project directory 'r7   NzProject directory is %spre_gen_projectr!   z../templateszFound copy only path %sz&Copying dir %s to %s without renderingzUnable to create directory 'z'Copying file %s to %s without renderingzUnable to create file 'post_gen_projectrV   )!r   rB   rC   r   rc   r<   r   r?   r   r
   r   r	   r   r=   r   r   r   loaderwalknormpathrX   r   appendrW   rY   rZ   r\   r   copytreerelpathr]   r^   rv   ) r   r   rx   rz   rl   accept_hookskeep_project_on_failuretemplate_direnvvarsunrendered_dirrk   ri   output_directory_createderrr   r   rootdirsfiles	copy_dirsrender_dirsdd_copy_dirindiroutdir_dirfrj   rm   outfile_renderedrn   s                                    r   generate_filesr   !  s   , !**L
LL0,???(RGkk."--112DbIIGW]]<003NN+++

SG4
S
S7
S
SCF0EGZ6I1
 1
---  F F FF^FFF)#sG<<#EF '//+..K
LL*K888 !9 XAX=X 
'g?X	
 	
 	
 
		 FR FR%sN&;<<
!# C	R C	RD$ IK 	* 	*W%%bgll4&;&;<< %R11 *LL!:A>>>$$Q''''&&q))))% / /((dH)E)EFF))"',,{E*J*JKK7007BB'BBEufUUU
 7==(( *M&)))v.... "DG R R!#k4!C!C	R)&SBU    & R R R0 ,{+++7??>:FFD@@@@C5c3HHcQR  R R))"',,tQ*?*?@@$VW55 	#&??6#:#:L':|':'E'EW'E'E$ gll;8HIIGLLA67   OFG444OFG444R!#VWc;N    & R R R0 ,{+++=F===C5c3HHcQ	R!R_C	RFR FR FR FR FR FR FR FR FR FR FR FR FR FR FRP  
%	
 	
 	
 su   *C 
C(C##C(G?S7M"!S7"
N:	,A	N5	5N:	:CS7R-,S7-
S%	7)S 	 S%	%S77S;>S;)r1   NN)F)Nr!   FFTF)0__doc__r   r:   loggingr<   r\   r@   collectionsr   pathlibr   binaryornot.checkr   jinja2r   r   jinja2.exceptionsr   r	   cookiecutter.environmentr
   cookiecutter.exceptionsr   r   r   r   cookiecutter.findr   cookiecutter.hooksr   cookiecutter.utilsr   r   r   	getLogger__name__rB   r   r+   rP   rv   r>   r*   boolr   r   r   r   rV   r0   r   <module>r      sR   A A    				   # # # # # #       ' ' ' ' ' ' 0 0 0 0 0 0 0 0 A A A A A A A A 6 6 6 6 6 6            , + + + + + 5 5 5 5 5 5 E E E E E E E E E E		8	$	$  * ;@-* -* -* -* -*b KO, , , ,^O% O% O% O%n !&0 000 #0 	0
 0 0 0 0B, , ,  2 !K K K K K Kr0   