B
    Ö-
dU5  ã               @   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 d dlmZmZ d dlmZ d d	lmZ G d
d„ deƒZdS )é    N)Úimport_module)Úurlretrieve)Úsettings)ÚBaseCommandÚCommandError)Úhandle_extensions)ÚContextÚEngine)Úarchive)Úget_docs_versionc               @   sn   e Zd ZdZg Zdddg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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   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   úD/tmp/pip-install-nbqhn9mb/Django/django/core/management/templates.pyÚadd_arguments(   s    zTemplateCommand.add_argumentsNc       #      K   sx  || _ |dkrdnd| _g | _|d | _|  |¡ |d kr¦tj t ¡ |¡}yt 	|¡ W qî t
k
rz   td| ƒ‚Y qî tk
r¢ } zt|ƒ‚W d d }~X Y qîX nH|dkrÂ|  tj |¡d¡ tj tj |¡¡}tj |¡sîtd| ƒ‚tt|d ƒƒ}g }x,|d	 D ] }	| td
d„ |	 d¡ƒ¡ qW | jdkrp| j d|d |¡f ¡ | j d|d |¡f ¡ d| }
d| }d| }d| }d dd„ | ¡ D ƒ¡}t||
|||||dtƒ dtji–dd}tjsèt ¡  t  ¡  |  !|d |¡}t"|ƒd }xt #|¡D ]\}}}||d … }| $|
|¡}|rXtj ||¡}tj	|dd x6|d d … D ]&}| %d ¡s€|d!krf| &|¡ qfW x„|D ]z}| 'd"¡r®q˜tj ||¡}tj ||| $|
|¡¡}x8| j(D ].\}}| '|¡rÜ|d t"|ƒ … | }P qÜW tj |¡r0td#|| j|f ƒ‚| '|¡sF||kr¢t)|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 q˜W qW | jrt| jdkr>| j d)¡ x4| jD ]*}"tj 5|"¡rdt &|"¡ n
t. 6|"¡ qFW d S )*NÚappÚanÚaÚ	verbosityz'%s' already existsr   zBDestination directory '%s' does not exist, please create it first.r   r   c             S   s   |   ¡ S )N)Ústrip)Úxr   r   r    Ú<lambda>V   ó    z(TemplateCommand.handle.<locals>.<lambda>ú,é   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_nameÚ c             s   s   | ]}|d kr|V  qdS )Ú_Nr   )Ú.0r'   r   r   r    ú	<genexpr>d   s    z)TemplateCommand.handle.<locals>.<genexpr>Zdocs_versionZdjango_versionF)Z
autoescapeÚtemplateé   T)Úexist_okÚ.Ú__pycache__)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.)7Úapp_or_projectÚa_or_anÚpaths_to_remover%   Úvalidate_nameÚosÚpathÚjoinÚgetcwdÚmakedirsÚFileExistsErrorr   ÚOSErrorÚbasenameÚabspathÚ
expanduserÚexistsÚtupler   ÚextendÚmapÚsplitÚ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ÚcopymodeÚmake_writeableÚstderrÚstyleZNOTICEÚisfileÚrmtree)#r   r7   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_restZrelative_dirÚ
target_dirÚdirnameÚfilenameZold_pathÚnew_pathZ
old_suffixÚ
new_suffixZtemplate_fileÚcontentr0   Znew_fileZpath_to_remover   r   r    Úhandle:   sÀ    

 


 zTemplateCommand.handlec             C   sª   |dk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.)r;   r<   r=   rM   Ú__path__rU   rD   ÚnormpathÚisdirÚis_urlÚdownloadrC   rE   Úextractr   r7   )r   r0   ÚsubdirZexpanded_templateZabsolute_pathr   r   r    rQ   ¹   s    


zTemplateCommand.handle_templater   c             C   s|   |d krt dj| j| jdƒ‚| ¡ s<t dj|| j|dƒ‚yt|ƒ W n tk
r\   Y nX 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   Úformatr8   r7   Úisidentifierr   ÚImportError)r   r   Zname_or_dirr   r   r    r:   Ó   s(    
zTemplateCommand.validate_namec          
   C   sJ  dd„ }d| j  }tj|dd}| j |¡ ||ƒ\}}| jdkrR| j d| ¡ yt|t	j
 ||¡ƒ\}}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rFt	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ú/éÿÿÿÿ)ÚrstriprI   rW   )ÚurlÚtmpro   Údisplay_urlr   r   r    Úcleanup_urlø   s    


z-TemplateCommand.download.<locals>.cleanup_urlzdjango_%s_template_Z	_download)ÚprefixÚsuffixr+   zDownloading %sz"couldn't download URL %s to %s: %sNr   r‚   zcontent-dispositionro   r1   zcontent-type)r7   ÚtempfileÚmkdtempr9   r   r%   rJ   rK   r   r;   r<   r=   rA   r   rI   ÚgetÚcgiÚparse_headerÚsplitextÚ	mimetypesÚguess_extensionr\   Úmove)r   r„   r‡   rˆ   Útempdirro   r†   Úthe_pathÚinforf   Z	used_nameÚcontent_dispositionr-   ÚparamsZguessed_filenameÚextÚcontent_typeZguessed_pathr   r   r    rz   ô   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   ÚlowerrW   )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 temporary directory and return
        the path of the directory with the extracted content.
        zdjango_%s_template_Z_extract)rˆ   r‰   r+   zExtracting %sz"couldn't extract file %s to %s: %sN)r7   rŠ   r‹   r9   r   r%   rJ   rK   r
   r{   ZArchiveExceptionrA   r   )r   ro   rˆ   r“   rf   r   r   r    r{   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.ú:Fr1   r   )rI   rœ   Úurl_schemes)r   r0   Úschemer   r   r    ry   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)r;   ÚaccessÚW_OKÚstatÚS_IMODEÚst_modeÚS_IWUSRÚchmod)r   ro   ÚstZnew_permissionsr   r   r    r_   L  s    
zTemplateCommand.make_writeable)N)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Zrequires_system_checksrŸ   rX   r!   rs   rQ   r:   rz   r   r{   ry   r_   r   r   r   r    r      s   



!6
r   )r   r   r;   r›   r\   r£   rŠ   Ú	importlibr   Úurllib.requestr   rM   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   r   r   r   r   r    Ú<module>   s    