B
    ©0H[’5  ã               @   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Zd dlm	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 d dlmZmZ d d	lmZ d d
lmZ e dej¡Ze dej¡ZG dd„ deƒZ dS )é    N)Úimport_module)Úpath)Úurlretrieve)Úsettings)ÚBaseCommandÚCommandError)Úhandle_extensions)ÚContextÚEngine)Úarchive)Úget_docs_versionz	^([a-z]):z^([a-z])[:|]c               @   sp   e Zd ZdZdZdddgZdZ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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
    FÚhttpÚhttpsÚftpT))z.py-tplz.pyc             C   s^   |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� 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.)Úadd_argument)ÚselfÚparser© r    údC:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6bm3nxem\django\django\core\management\templates.pyÚadd_arguments0   s    zTemplateCommand.add_argumentsNc       #      K   sB  || _ g | _|d | _|  ||¡ |d kr”t t ¡ |¡}yt |¡ W q¾ t	k
rh   t
d| ƒ‚Y q¾ tk
r� } zt
|ƒ‚W d d }~X Y q¾X n*tj t |¡¡}tj |¡s¾t
d| ƒ‚tt|d ƒƒ}g }x*|d D ]}	| tdd„ |	 d¡ƒ¡ qÜW | jd	k�r>| j d
|d |¡f ¡ | j d|d |¡f ¡ d| }
d| }d| }d| }d dd„ | ¡ D ƒ¡}tt|f|
|||||dtƒ dtjiŽdd�}tj�sºt ¡  t ¡  |   |d |¡}t!|ƒd }�xt "|¡D �] \}}}||d … }| #|
|¡}|�r0t ||¡}t |¡�s0t $|¡ x6|d d … D ]&}| %d¡�sX|dk�r>| &|¡ �q>W �xx|D �]n}| 'd¡�r†�qpt ||¡}t ||| #|
|¡¡}x8| j(D ].\}}| '|¡�r°|d t!|ƒ … | }P �q°W t |¡�rút
d| ƒ‚| '|¡�s||k�rnt)|ddd ��}| *¡ }W d Q R X t+ƒ  ,|¡} |  -|¡}t)|d!dd ��}!|! |¡ W d Q R X nt. /||¡ | jd	k�r–| j d"| ¡ yt. 0||¡ |  1|¡ W n, tk
�rÜ   | j2 d#| | j3j4¡ Y nX �qpW �qäW | j�r>| jd	k�r
| j d$¡ x2| jD ](}"t 5|"¡�r.t &|"¡ n
t. 6|"¡ �qW d S )%NÚ	verbosityz'%s' already existszBDestination directory '%s' does not exist, please create it first.r   r   c             S   s   |   ¡ S )N)Ústrip)Úxr    r    r!   Ú<lambda>[   s    z(TemplateCommand.handle.<locals>.<lambda>ú,é   z0Rendering %s template files with extensions: %s
z, z/Rendering %s template files with filenames: %s
z%s_namez%s_templatez%s_directoryzcamel_case_%s_nameÚ c             s   s   | ]}|d kr|V  qdS )Ú_Nr    )Ú.0r%   r    r    r!   ú	<genexpr>h   s    z)TemplateCommand.handle.<locals>.<genexpr>Zdocs_versionZdjango_versionF)Z
autoescapeÚtemplateé   Ú.Ú__pycache__)z.pyoz.pycz	.py.classzi%s already exists, overlaying a project or app into an existing directory won't replace conflicting filesÚrzutf-8)ÚencodingÚwzCreating %s
zkNotice: Couldn't set permission bits on %s. You're probably using an uncommon filesystem setup. No problem.zCleaning up temporary files.
)7Úapp_or_projectÚpaths_to_remover#   Úvalidate_namer   ÚjoinÚosÚgetcwdÚmakedirsÚFileExistsErrorr   ÚOSErrorÚabspathÚ
expanduserÚexistsÚtupler   ÚextendÚmapÚsplitÚstdoutÚwriteÚtitler	   Údictr   ÚdjangoÚ__version__r   Z
configuredÚ	configureÚsetupÚhandle_templateÚlenÚwalkÚreplaceÚmkdirÚ
startswithÚremoveÚendswithÚrewrite_template_suffixesÚopenÚreadr
   Zfrom_stringÚrenderÚshutilÚcopyfileÚcopymodeÚmake_writeableÚstderrÚstyleZNOTICEÚisfileÚrmtree)#r   r4   r   ÚtargetÚoptionsZtop_dirÚer   Zextra_filesÚfileÚ	base_nameZbase_subdirZbase_directoryZcamel_case_nameZcamel_case_valueÚcontextZtemplate_dirZprefix_lengthÚrootÚdirsr   Ú	path_restÚrelative_dirÚ
target_dirÚdirnameÚfilenameZold_pathÚnew_pathZ
old_suffixZ
new_suffixZtemplate_fileÚcontentr-   Znew_fileZpath_to_remover    r    r!   ÚhandleB   s´    




 zTemplateCommand.handlec             C   sž   |dkrt  tjd d|¡S | d¡r2|dd… }t  |¡}t  |¡}t  |¡rT|S |  |¡rj|  	|¡}n
t  
|¡}t  |¡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.)r   r7   rH   Ú__path__rQ   r>   ÚnormpathÚisdirÚis_urlÚdownloadr=   r?   Úextractr   r4   )r   r-   ÚsubdirZexpanded_templateZabsolute_pathr    r    r!   rL   º   s    







zTemplateCommand.handle_templatec             C   s~   |dkrdnd}|d kr*t dj||d�ƒ‚| ¡ sDt dj||d�ƒ‚yt|ƒ W n tk
rd   Y nX t dj|||d	�ƒ‚d S )
NÚappÚanÚaz you must provide {an} {app} name)rz   ry   zT'{name}' is not a valid {app} name. Please make sure the name is a valid identifier.)r   ry   z}'{name}' conflicts with the name of an existing Python module and cannot be used as {an} {app} name. Please try another name.)r   rz   ry   )r   ÚformatÚisidentifierr   ÚImportError)r   r   r4   Za_or_anr    r    r!   r6   Ô   s&    

zTemplateCommand.validate_namec          
   C   sF  dd„ }d| j  }tj|dd�}| j |¡ ||ƒ\}}| jdkrR| j d| ¡ yt|t	 
||¡ƒ\}}W n4 tk
r  }	 ztd|||	f ƒ‚W d	d	}	~	X Y nX | d
¡d }
| d¡}|rÜt |¡\}}| d¡pØ|
}n|
}|  |¡d }| d¡}|�s|�rt |¡}|�r||7 }|
|k�rBt	 
||¡}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ú/éÿÿÿÿ)ÚrstriprC   rS   )ÚurlÚtmprl   Údisplay_urlr    r    r!   Úcleanup_urlø   s    


z-TemplateCommand.download.<locals>.cleanup_urlzdjango_%s_template_Z	_download)ÚprefixÚsuffixr(   zDownloading %s
z"couldn't download URL %s to %s: %sNr   r€   zcontent-dispositionrl   r.   zcontent-type)r4   ÚtempfileÚmkdtempr5   r   r#   rD   rE   r   r   r7   ÚIOErrorr   rC   ÚgetÚcgiÚparse_headerÚsplitextÚ	mimetypesÚguess_extensionrX   Úmove)r   r‚   r…   r†   Útempdirrl   r„   Úthe_pathÚinforb   Z	used_nameÚcontent_dispositionr*   ÚparamsZguessed_filenameÚextÚcontent_typeZguessed_pathr    r    r!   rv   ô   s:    	

 



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Ž   ÚlowerrS   )r   r“   Úbaser—   r    r    r!   rŽ   *  s
    zTemplateCommand.splitextc          
   C   sŽ   d| j  }tj|dd�}| j |¡ | jdkr>| j d| ¡ yt 	||¡ |S  tj
tfk
rˆ } ztd|||f ƒ‚W dd}~X Y nX dS )z‚
        Extract the given file to a temporarily and return
        the path of the directory with the extracted content.
        zdjango_%s_template_Z_extract)r†   r‡   r(   zExtracting %s
z"couldn't extract file %s to %s: %sN)r4   rˆ   r‰   r5   r   r#   rD   rE   r   rw   ZArchiveExceptionrŠ   r   )r   rl   r†   r’   rb   r    r    r!   rw   4  s    

zTemplateCommand.extractc             C   s*   d|krdS |  dd¡d  ¡ }|| jkS )z)Return True if the name looks like a URL.ú:Fr.   r   )rC   r›   Úurl_schemes)r   r-   Úschemer    r    r!   ru   E  s    zTemplateCommand.is_urlc             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)r8   ÚaccessÚW_OKÚstatÚS_IMODEÚst_modeÚS_IWUSRÚchmod)r   rl   ÚstZnew_permissionsr    r    r!   r[   L  s    
zTemplateCommand.make_writeable)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Zrequires_system_checksrž   Zleave_locale_alonerT   r"   ro   rL   r6   rv   rŽ   rw   ru   r[   r    r    r    r!   r      s   


x 6
r   )!rŒ   r�   r8   rš   ÚrerX   r¢   rˆ   Ú	importlibr   r   Úurllib.requestr   rH   Zdjango.confr   Zdjango.core.management.baser   r   Zdjango.core.management.utilsr   Zdjango.templater	   r
   Zdjango.utilsr   Zdjango.utils.versionr   ÚcompileÚIZ	_drive_reZ_url_drive_rer   r    r    r    r!   Ú<module>   s(   