a
    LcòfŠ<  ã                   @   sÐ   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Zd dlmZ d dl	m
Z
 d dlZd dlmZ d dlmZmZ d dlmZmZmZ d dlmZmZ d dlmZ d d	lmZ d d
lmZ G dd„ deƒZdS )é    N)Úimport_module)Úbuild_opener)Úsettings)ÚBaseCommandÚCommandError)Úfind_formattersÚhandle_extensionsÚrun_formatters)ÚContextÚEngine)Úarchive)Úparse_header_parameters)Úget_docs_versionc                   @   st   e Zd ZdZg Zg d¢ZdZdd„ Zddd„Zd	d
„ Z	ddd„Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚTemplateCommandaÛ  
    Copy either a Django application layout template or a Django project
    layout template into the specified directory.

    :param style: A color style object (see django.core.management.color).
    :param app_or_project: The string 'app' or 'project'.
    :param name: The name of the application or project.
    :param directory: The directory to which the template should be copied.
    :param options: The additional variables passed to project or app templates
    )ÚhttpÚhttpsÚftp))z.py-tplz.pyc              	   C   sx   |j ddd� |j dddd� |j dd	d� |j d
ddddgdd� |j ddddg dd� |j dddtjdddd� d S )NÚnamez#Name of the application or project.)ÚhelpÚ	directoryú?zOptional destination directory)Únargsr   z
--templatez*The path or URL to load the template from.z--extensionz-eÚ
extensionsÚappendÚpyztThe file extension(s) to render (default: "py"). Separate multiple extensions with commas, or use -e multiple times.)ÚdestÚactionÚdefaultr   z--namez-nÚfilesz_The file name(s) to render. Separate multiple file names with commas, or use -n multiple times.z	--excludez-xÚ zbThe directory name(s) to exclude, in addition to .git and __pycache__. Can be used multiple times.)r   r   r   Úconstr   )Úadd_argumentÚargparseÚSUPPRESS)ÚselfÚparser© r&   ú]/home/httpd/docs/test/DocsMgr/lib/python3.9/site-packages/django/core/management/templates.pyÚadd_arguments.   s@    ÿÿú
ú	øzTemplateCommand.add_argumentsNc           '      K   sø  || _ |dkrdnd| _g | _|d | _|  |¡ |d u r¦tj t ¡ |¡}zt 	|¡ W qî t
yx   td| ƒ‚Y qî ty¢ } zt|ƒ‚W Y d }~qîd }~0 0 nHtj tj |¡¡}|dkrÖ|  tj |¡d¡ tj |¡sîtd| ƒ‚tƒ }tt|d ƒƒ}g }	d	d
g}
|d D ] }|	 tdd„ | d¡ƒ¡ �q| d¡ }�rd|D ]}|
 | ¡ ¡ �qN| jdk�r¤| j d|d |¡f ¡ | j d|d |	¡f ¡ d| }d| }d| }d| }d dd„ | ¡ D ƒ¡}ti |¥||||||dtƒ dtj i¥dd�}t!j"�s t! #¡  t $¡  |  %|d |¡}t&|ƒd  }t '|¡D �]F\}}}||d … }| (||¡}|�rŒtj ||¡}tj	|d!d"� |d d … D ]F}d|v�rÈ| )d#¡�s¼|d
k�rÜ| *|¡ n||
v �r˜| *|¡ �q˜|D �]¤}| +d$¡�rú�qätj ||¡}tj ||| (||¡¡}| j,D ]2\} }!| +| ¡�r&|d t&| ƒ … |! } �qZ�q&tj |¡�r|td%|| j|f ƒ‚| +|¡�s’||	v �rt-|d&d'��}"|" .¡ }#W d   ƒ n1 �s¾0    Y  t/ƒ  0|#¡}$|$ 1|¡}#t-|d(d&d'��}%|% |#¡ W d   ƒ n1 �s0    Y  nt2 3||¡ | jdk�rB| j d)| ¡ z|  4||¡ |  5|¡ W n* t�y†   | j6 d*| | j7j8¡ Y n0 �qä�qF| j�râ| jdk�r°| j d+¡ | jD ]*}&tj 9|&¡�rÔt *|&¡ n
t2 :|&¡ �q¶t;|gfi |¤Ž d S ),NÚappÚanÚaÚ	verbosityz'%s' already existsr   zBDestination directory '%s' does not exist, please create it first.r   z.gitÚ__pycache__r   c                 S   s   |   ¡ S )N)Ústrip)Úxr&   r&   r'   Ú<lambda>y   ó    z(TemplateCommand.handle.<locals>.<lambda>ú,Úexcludeé   z/Rendering %s template files with extensions: %sz, z.Rendering %s template files with filenames: %sz%s_namez%s_templatez%s_directoryzcamel_case_%s_namer   c                 s   s   | ]}|d kr|V  qdS )Ú_Nr&   )Ú.0r/   r&   r&   r'   Ú	<genexpr>Š   r1   z)TemplateCommand.handle.<locals>.<genexpr>Zdocs_versionZdjango_versionF)Z
autoescapeÚtemplateé   T)Úexist_okÚ.)z.pyoz.pycz	.py.classz_%s already exists. Overlaying %s %s into an existing directory won't replace conflicting files.zutf-8)ÚencodingÚwzCreating %szkNotice: Couldn't set permission bits on %s. You're probably using an uncommon filesystem setup. No problem.zCleaning up temporary files.)<Úapp_or_projectÚa_or_anÚpaths_to_remover,   Úvalidate_nameÚosÚpathÚjoinÚgetcwdÚmakedirsÚFileExistsErrorr   ÚOSErrorÚabspathÚ
expanduserÚbasenameÚexistsr   Útupler   ÚextendÚmapÚsplitÚgetr   r.   ÚstdoutÚwriteÚtitler
   r   ÚdjangoÚ__version__r   Z
configuredÚ	configureÚsetupÚhandle_templateÚlenÚwalkÚreplaceÚ
startswithÚremoveÚendswithÚrewrite_template_suffixesÚopenÚreadr   Zfrom_stringÚrenderÚshutilÚcopyfileÚapply_umaskÚmake_writeableÚstderrÚstyleZNOTICEÚisfileÚrmtreer	   )'r$   r>   r   ÚtargetÚoptionsZtop_dirÚeZformatter_pathsr   Zextra_filesZexcluded_directoriesÚfiler3   r   Ú	base_nameZbase_subdirZbase_directoryZcamel_case_nameZcamel_case_valueÚcontextZtemplate_dirZprefix_lengthÚrootÚdirsr   Ú	path_restZrelative_dirÚ
target_dirÚdirnameÚfilenameÚold_pathÚnew_pathÚ
old_suffixÚ
new_suffixZtemplate_fileÚcontentr8   Únew_fileZpath_to_remover&   r&   r'   ÚhandleV   sø    

 ÿÿÿÿÿÿÿú÷


ÿ
ýþÿ(
,þü
zTemplateCommand.handlec                 C   sª   |du rt j tjd d|¡S | d¡r4|dd… }t j |¡}t j |¡}t j |¡r\|S |  	|¡rr|  
|¡}nt j |¡}t j |¡r”|  |¡S td| j|f ƒ‚dS )z«
        Determine where the app or project templates are.
        Use django.__path__[0] as the default because the Django install
        directory isn't known.
        Nr   Úconfzfile://é   zcouldn't handle %s template %s.)rB   rC   rD   rU   Ú__path__r]   rJ   ÚnormpathÚisdirÚis_urlÚdownloadrI   rL   Úextractr   r>   )r$   r8   ÚsubdirZexpanded_templateZabsolute_pathr&   r&   r'   rY   ê   s     


ÿzTemplateCommand.handle_templater   c                 C   sz   |d u rt dj| j| jd�ƒ‚| ¡ s<t dj|| j|d�ƒ‚zt|ƒ W n tyZ   Y n0 t dj|| j| j|d�ƒ‚d S )Nz you must provide {an} {app} name)r*   r)   zX'{name}' is not a valid {app} {type}. Please make sure the {type} is a valid identifier.)r   r)   Útypez�'{name}' conflicts with the name of an existing Python module and cannot be used as {an} {app} {type}. Please try another {type}.)r   r*   r)   rˆ   )r   Úformatr?   r>   Úisidentifierr   ÚImportError)r$   r   Zname_or_dirr&   r&   r'   rA     s6    þÿüÿ	úÿzTemplateCommand.validate_namec              
   C   sÆ  dd„ }d| j  }tj|dd�}| j |¡ ||ƒ\}}| jdkrR| j d| ¡ tj	 
||¡}tƒ }dd	tj› �fg|_zn| |¡�P}	t|d
ƒ�&}
|	 ¡ }|
 |	 ¡ ¡ W d  ƒ n1 s¾0    Y  W d  ƒ n1 sÜ0    Y  W n8 t�y  } ztd|||f ƒ‚W Y d}~n
d}~0 0 | d¡d }|d }|�r\t|ƒ\}}| d¡�pX|}n|}|  |¡d }|d }|�sš|�ršt |¡}|�rš||7 }||k�rÂtj	 
||¡}t ||¡ |S |S )zB
        Download the given URL and return the file name.
        c                 S   s8   |   d¡}| d¡d }|  d¡r,|d }n| }||fS )Nú/éÿÿÿÿ)ÚrstriprP   r_   )ÚurlÚtmprw   Údisplay_urlr&   r&   r'   Úcleanup_url-  s    


z-TemplateCommand.download.<locals>.cleanup_urlúdjango_%s_template_Ú	_download©ÚprefixÚsuffixr4   zDownloading %sz
User-AgentzDjango/ÚwbNz"couldn't download URL %s to %s: %srŒ   r�   zcontent-dispositionrw   r9   zcontent-type)r>   ÚtempfileÚmkdtempr@   r   r,   rR   rS   rB   rC   rD   r   rU   rV   Ú
addheadersra   Úinforb   rH   r   rP   r   rQ   ÚsplitextÚ	mimetypesÚguess_extensionrd   Úmove)r$   r�   r’   r–   Útempdirrw   r‘   Úthe_pathÚopenerÚsourcerl   Úheadersrn   Z	used_nameÚcontent_dispositionr5   ÚparamsZguessed_filenameÚextÚcontent_typeZguessed_pathr&   r&   r'   r…   (  sF    	

Nÿ

zTemplateCommand.downloadc                 C   s@   t  |¡\}}| ¡  d¡r8|dd… | }|dd… }||fS )z@
        Like os.path.splitext, but takes off .tar, too
        z.taréüÿÿÿN)Ú	posixpathr�   Úlowerr_   )r$   r¢   Úbaser¨   r&   r&   r'   r�   f  s
    zTemplateCommand.splitextc              
   C   s’   d| j  }tj|dd�}| j |¡ | jdkr>| j d| ¡ zt 	||¡ |W S  tj
tfyŒ } ztd|||f ƒ‚W Y d}~n
d}~0 0 dS )zŠ
        Extract the given file to a temporary directory and return
        the path of the directory with the extracted content.
        r“   Z_extractr•   r4   zExtracting %sz"couldn't extract file %s to %s: %sN)r>   r™   rš   r@   r   r,   rR   rS   r   r†   ZArchiveExceptionrH   r   )r$   rw   r–   r¡   rn   r&   r&   r'   r†   p  s    

ÿzTemplateCommand.extractc                 C   s*   d|vrdS |  dd¡d  ¡ }|| jv S )z)Return True if the name looks like a URL.ú:Fr9   r   )rP   r¬   Úurl_schemes)r$   r8   Úschemer&   r&   r'   r„   ‚  s    zTemplateCommand.is_urlc                 C   s<   t  d¡}t  |¡ t t  |¡j¡}t  ||| @ ¡ d S )Nr   )rB   ÚumaskÚstatÚS_IMODEÚst_modeÚchmod)r$   rx   ry   Úcurrent_umaskZcurrent_moder&   r&   r'   rf   ‰  s    

zTemplateCommand.apply_umaskc                 C   s:   t  |t j¡s6t  |¡}t |j¡tjB }t  ||¡ dS )zb
        Make sure that the file is writeable.
        Useful if our source is read-only.
        N)rB   ÚaccessÚW_OKr²   r³   r´   ÚS_IWUSRrµ   )r$   rw   ÚstZnew_permissionsr&   r&   r'   rg   �  s    
zTemplateCommand.make_writeable)N)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Zrequires_system_checksr¯   r`   r(   r~   rY   rA   r…   r�   r†   r„   rf   rg   r&   r&   r&   r'   r      s   (
 
#>
r   )r"   rž   rB   r«   rd   r²   r™   Ú	importlibr   Úurllib.requestr   rU   Zdjango.confr   Zdjango.core.management.baser   r   Zdjango.core.management.utilsr   r   r	   Zdjango.templater
   r   Zdjango.utilsr   Zdjango.utils.httpr   Zdjango.utils.versionr   r   r&   r&   r&   r'   Ú<module>   s"   