o
    ˜¨Êh<  ã                   @   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 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)Ú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%   úR/var/www/html/env/lib/python3.10/site-packages/django/core/management/templates.pyÚadd_arguments*   s@   ÿÿú
ú	
øzTemplateCommand.add_argumentsNc           &      K   sâ  g | _ || _|dkrdnd| _g | _|d | _|  |¡ |d u rMtj t 	¡ |¡}zt 
|¡ W n@ ty=   td| ƒ‚ tyL } zt|ƒ‚d }~ww tj tj |¡¡}|dkre|  tj |¡d¡ tj |¡sqtd| ƒ‚tt|d ƒƒ}g }d	d
g}	|d D ]}
| tdd„ |
 d¡ƒ¡ qƒ| d¡ }r¦|D ]	}|	 | ¡ ¡ qœ| 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 �](\}}}||d … }| (||¡}|�r9tj ||¡}tj
|d!d"� |d d … D ]$}d|v�rX| )d#¡�sQ|d
k�rV| *|¡ �q?||	v �rb| *|¡ �q?|D ]×}| +d$¡�rp�qftj ||¡}tj ||| (||¡¡}| j,D ]\}} | +|¡�r�|d t&|ƒ … |  } n�q†tj |¡�r°td%|| j|f ƒ‚| +|¡�s»||v �rÿt-|d&d'��}!|! .¡ }"W d   ƒ n	1 �sÑw   Y  t/ƒ  0|"¡}#|# 1|¡}"t-|d(d&d'��}$|$ |"¡ W d   ƒ n	1 �sùw   Y  nt2 3||¡ | j  |¡ | jdk�r| j d)| ¡ z|  4||¡ |  5|¡ W �qf t�y=   | j6 d*| | j7j8¡ Y �qfw �q| j�rj| jdk�rP| j d+¡ | jD ]}%tj 9|%¡�rct *|%¡ �qSt2 :|%¡ �qSt;| j ƒ 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>r   s    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>ƒ   s   € z)TemplateCommand.handle.<locals>.<genexpr>Údocs_versionÚdjango_versionF)Ú
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.)<Úwritten_filesÚapp_or_projectÚa_or_anÚpaths_to_remover+   Úvalidate_nameÚosÚpathÚjoinÚgetcwdÚmakedirsÚFileExistsErrorr   ÚOSErrorÚabspathÚ
expanduserÚbasenameÚexistsÚtupler   ÚextendÚmapÚsplitÚgetr   r.   ÚstdoutÚwriteÚtitler	   r   ÚdjangoÚ__version__r   Ú
configuredÚ	configureÚsetupÚhandle_templateÚlenÚwalkÚreplaceÚ
startswithÚremoveÚendswithÚrewrite_template_suffixesÚopenÚreadr
   Úfrom_stringÚrenderÚshutilÚcopyfileÚapply_umaskÚmake_writeableÚstderrÚstyleÚNOTICEÚisfileÚrmtreer   )&r#   rA   r   ÚtargetÚoptionsÚtop_dirÚer   Úextra_filesÚexcluded_directoriesÚfiler2   r   Ú	base_nameÚbase_subdirÚbase_directoryÚcamel_case_nameÚcamel_case_valueÚcontextÚtemplate_dirÚprefix_lengthÚrootÚdirsr   Ú	path_restÚrelative_dirÚ
target_dirÚdirnameÚfilenameÚold_pathÚnew_pathÚ
old_suffixÚ
new_suffixÚtemplate_fileÚcontentr:   Únew_fileÚpath_to_remover%   r%   r&   ÚhandleR   s  

€ÿÿÿ
ÿÿÿÿÿú÷

€

€ÿþýþÿ
ÿ
ÿ€þ
üÿÖ2
zTemplateCommand.handlec                 C   s¦   |du rt j tjd d|¡S | d¡r|dd… }t j |¡}t j |¡}t j |¡r.|S |  	|¡r9|  
|¡}nt j |¡}t j |¡rJ|  |¡S td| j|f ƒ‚)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.)rE   rF   rG   rX   Ú__path__ra   rM   ÚnormpathÚisdirÚis_urlÚdownloadrL   rO   Úextractr   rA   )r#   r:   ÚsubdirÚexpanded_templateÚabsolute_pathr%   r%   r&   r]   å   s    


ÿzTemplateCommand.handle_templater   c                 C   sx   |d u rt dj| j| jd�ƒ‚| ¡ st dj|| j|d�ƒ‚zt|ƒ W n
 ty.   Y d S w t dj|| j| j|d�ƒ‚)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   ÚformatrB   rA   Úisidentifierr   ÚImportError)r#   r   Úname_or_dirr%   r%   r&   rD      s<   þÿüÿ	ÿúÿzTemplateCommand.validate_namec              
   C   s¬  dd„ }d| j  }tj|dd�}| j |¡ ||ƒ\}}| jdkr)| j d| ¡ tj	 
||¡}tƒ }dd	tj› �fg|_z7| |¡�(}	t|d
ƒ�}
|	 ¡ }|
 |	 ¡ ¡ W d  ƒ n1 s_w   Y  W d  ƒ n1 snw   Y  W n ty‰ } z	td|||f ƒ‚d}~ww | d¡d }|d }|r¥t|ƒ\}}| d¡p£|}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   s>   |   d¡}| d¡d }|  d¡r|d }||fS | }||fS )Nú/éÿÿÿÿ)ÚrstriprS   rc   )ÚurlÚtmpr‡   Údisplay_urlr%   r%   r&   Úcleanup_url(  s   

ÿz-TemplateCommand.download.<locals>.cleanup_urlúdjango_%s_template_Ú	_download©ÚprefixÚsuffixr3   zDownloading %sz
User-AgentzDjango/ÚwbNz"couldn't download URL %s to %s: %sr¡   r¢   zcontent-dispositionr‡   r;   zcontent-type)rA   ÚtempfileÚmkdtemprC   r   r+   rU   rV   rE   rF   rG   r   rX   rY   Ú
addheadersre   Úinforf   rK   r   rS   r   rT   ÚsplitextÚ	mimetypesÚguess_extensionri   Úmove)r#   r¤   r§   r«   Útempdirr‡   r¦   Úthe_pathÚopenerÚsourcerr   Úheadersru   Ú	used_nameÚcontent_dispositionr4   ÚparamsÚguessed_filenameÚextÚcontent_typeÚguessed_pathr%   r%   r&   r—   #  sR   
	
þ€ €ÿ€ÿ
zTemplateCommand.downloadc                 C   s@   t  |¡\}}| ¡  d¡r|dd… | }|dd… }||fS )z@
        Like os.path.splitext, but takes off .tar, too
        z.taréüÿÿÿN)Ú	posixpathr²   Úlowerrc   )r#   r·   Úbaser¿   r%   r%   r&   r²   a  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 ƒ‚d}~ww )zŠ
        Extract the given file to a temporary directory and return
        the path of the directory with the extracted content.
        r¨   Ú_extractrª   r3   zExtracting %sz"couldn't extract file %s to %s: %sN)rA   r®   r¯   rC   r   r+   rU   rV   r   r˜   ÚArchiveExceptionrK   r   )r#   r‡   r«   r¶   ru   r%   r%   r&   r˜   k  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.ú:Fr;   r   )rS   rÄ   Úurl_schemes)r#   r:   Úschemer%   r%   r&   r–   }  s   
zTemplateCommand.is_urlc                 C   s<   t  d¡}t  |¡ t t  |¡j¡}t  ||| @ ¡ d S )Nr   )rE   ÚumaskÚstatÚS_IMODEÚst_modeÚchmod)r#   rˆ   r‰   Úcurrent_umaskÚcurrent_moder%   r%   r&   rk   „  s   

zTemplateCommand.apply_umaskc                 C   s>   t  |t j¡st  |¡}t |j¡tjB }t  ||¡ dS dS )zb
        Make sure that the file is writeable.
        Useful if our source is read-only.
        N)rE   ÚaccessÚW_OKrÌ   rÍ   rÎ   ÚS_IWUSRrÏ   )r#   r‡   ÚstÚnew_permissionsr%   r%   r&   rl   Š  s
   
ýzTemplateCommand.make_writeabler-   )r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úrequires_system_checksrÉ   rd   r'   r�   r]   rD   r—   r²   r˜   r–   rk   rl   r%   r%   r%   r&   r      s     
( 
#>
r   )r!   r³   rE   rÃ   ri   rÌ   r®   Ú	importlibr   Úurllib.requestr   rX   Údjango.confr   Údjango.core.management.baser   r   Údjango.core.management.utilsr   r   Údjango.templater	   r
   Údjango.utilsr   Údjango.utils.httpr   Údjango.utils.versionr   r   r%   r%   r%   r&   Ú<module>   s$    