o
    ^"<fU5                     @   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                   @   sl   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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   W/var/www/html/kck/venv/lib/python3.10/site-packages/django/core/management/templates.pyadd_arguments(   s   
zTemplateCommand.add_argumentsNc           #      K   sv  || _ |dkr	dnd| _g | _|d | _| | |d u rJtjt |}zt	| W n@ t
y:   td|  tyI } zt|d }~ww |dkrX| tj|d tjtj|}tj|sntd| tt|d }g }|d	 D ]}	|td
d |	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st  t   | !|d |}t"|d }t#|D ]\}}}||d  }|$|
|}|rtj||}tj	|dd |d d  D ]}|%d s1|d!kr6|&| q$|D ]}|'d"rDq:tj||}tj|||$|
|}| j(D ]\}}|'|rq|d t"|  | } nqZtj|rtd#|| j|f |'|s||v rt)|d$d%}|* }W d    n	1 sw   Y  t+ ,|} | -|}t)|d&d$d%}!|!| W d    n	1 sw   Y  nt./|| | jdkr| jd'|  zt.0|| | 1| W q: ty   | j2d(| | j3j4 Y q:w q| jr7| jdkr| jd) | jD ]}"tj5|"r0t&|" q t.6|" q d S 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   s    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>docs_versiondjango_versionF)
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   
configured	configuresetuphandle_templatelenwalkreplace
startswithremoveendswithrewrite_template_suffixesopenreadr	   from_stringrendershutilcopyfilecopymodemake_writeablestderrstyleNOTICEisfilermtree)#r   r:   r   targetoptionstop_direr   extra_filesfile	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contentr3   new_filepath_to_remover   r   r    handle:   s   





	


,
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.)r>   r?   r@   rP   __path__rY   rG   normpathisdiris_urldownloadrF   rH   extractr   r:   )r   r3   subdirexpanded_templateabsolute_pathr   r   r    rU      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   formatr;   r:   isidentifierr   ImportError)r   r   name_or_dirr   r   r    r=      s8   	zTemplateCommand.validate_namec              
   C   s8  dd }d| j  }tj|dd}| j| ||\}}| jdkr)| jd|  zt|t	j
||\}}W n tyL }	 z	td|||	f d	}	~	ww |d
d }
|d}|rjt|\}}|dph|
}n|
}| |d }|d}|s|rt|}|r||7 }|
|krt	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/)rstriprL   r[   )urltmpr~   display_urlr   r   r    cleanup_url   s   

z-TemplateCommand.download.<locals>.cleanup_urldjango_%s_template_	_downloadprefixsuffixr+   zDownloading %sz"couldn't download URL %s to %s: %sNr   r   zcontent-dispositionr~   r4   zcontent-type)r:   tempfilemkdtempr<   r   r%   rM   rN   r   r>   r?   r@   rD   r   rL   getcgiparse_headersplitext	mimetypesguess_extensionra   move)r   r   r   r   tempdirr~   r   the_pathinform   	used_namecontent_dispositionr-   paramsguessed_filenameextcontent_typeguessed_pathr   r   r    r      s@   
	



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.tarN)	posixpathr   lowerr[   )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|  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   r+   zExtracting %sz"couldn't extract file %s to %s: %sN)r:   r   r   r<   r   r%   rM   rN   r
   r   ArchiveExceptionrD   r   )r   r~   r   r   rm   r   r   r    r   4  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.:Fr4   r   )rL   r   url_schemes)r   r3   schemer   r   r    r   E  s   
zTemplateCommand.is_urlc                 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)r>   accessW_OKstatS_IMODEst_modeS_IWUSRchmod)r   r~   stnew_permissionsr   r   r    rd   L  s
   
zTemplateCommand.make_writeabler&   )r   )__name__
__module____qualname____doc__requires_system_checksr   r\   r!   r   rU   r=   r   r   r   r   rd   r   r   r   r    r      s    


!6
r   )r   r   r>   r   ra   r   r   	importlibr   urllib.requestr   rP   django.confr   django.core.management.baser   r   django.core.management.utilsr   django.templater   r	   django.utilsr
   django.utils.versionr   r   r   r   r   r    <module>   s"    