o
    ˷e<                     @   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%   W/var/www/ideatree/venv/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qtj|rtd%|| j|f |+|s||v rt-|d&d'}!|!. }"W d    n	1 sw   Y  t/ 0|"}#|#1|}"t-|d(d&d'}$|$|" W d    n	1 sw   Y  nt23|| | 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 }|rt|\}}|dp|}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/)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.tarN)	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$    