o
    ~¨Êh8  ã                   @   s>  d dl mZ d dlZd dlZd dlmZmZ d dlmZ d dlm	Z	 d dl
Z
d dlmZmZ d dlZ
d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZmZmZ d dlmZ d dlmZmZm Z  d dl!m"Z" ej#rzd dl$m%Z% nzd dl&m%Z% W n e'y‘   d dl%m%Z% Y nw G dd„ deƒZ(de(_)dS )é    )Úunicode_literalsN)ÚOrderedDictÚdefaultdict©Úfnmatch)Úimport_module)ÚBaseCommandÚCommandError)ÚContext)Úsix©Ú
smart_text)Úget_template)Úengines)Úget_offline_hexdigestÚwrite_offline_manifestÚget_offline_manifest)Úsettings)ÚOfflineGenerationErrorÚTemplateSyntaxErrorÚTemplateDoesNotExist)Úget_mod_func)ÚStringIOc                   @   sD   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	d
„Zddd„Zdd„ Z	dS )ÚCommandz6Compress content outside of the request/response cyclec                 C   sV   |j dddddd� |j ddd	d
ddd� |j dd	d
ddd� |j dg dddd� d S )Nz--extensionz-eÚappendÚ
extensionszwThe file extension(s) to examine (default: ".html", separate multiple extensions with commas, or use -e multiple times))ÚactionÚdestÚhelpz-fz--forceFÚ
store_truez\Force the generation of compressed content even if the COMPRESS_ENABLED setting is not True.Úforce)Údefaultr   r   r   z--follow-linksz¼Follow symlinks when traversing the COMPRESS_ROOT (which defaults to STATIC_ROOT). Be aware that using this can lead to infinite recursion if a link points to a parent directory of itself.Úfollow_linksz--enginez¥Specifies the templating engine. jinja2 and django are supported. It may be a specified more than once for multiple engines. If not specified, django engine is used.r   )Úadd_argument)ÚselfÚparser© r&   úY/var/www/html/env/lib/python3.10/site-packages/compressor/management/commands/compress.pyÚadd_arguments'   s   ÿþ
ü

üzCommand.add_argumentsc                 C   sf   g }t  ¡ D ]}t|dƒr| |j |jj¡¡ qg }|D ]}t|dƒr+| |j¡ q| |¡ q|S )NÚengineÚloaders)r   ÚallÚhasattrÚextendr)   Úget_template_loadersr*   r   )r$   Útemplate_source_loadersÚer*   Úloaderr&   r&   r'   Úget_loaders:   s   
ÿ€
zCommand.get_loadersc                 C   sZ   |dkrddl m} t ¡ }|tj|d�}|S |dkr)ddlm} |tjd�}|S tdƒ‚)	NÚjinja2r   )ÚJinja2Parser)ÚcharsetÚenvÚdjango)ÚDjangoParser)r5   z$Invalid templating engine specified.)Úcompressor.offline.jinja2r4   r   ÚCOMPRESS_JINJA2_GET_ENVIRONMENTÚFILE_CHARSETÚcompressor.offline.djangor8   r   )r$   r)   r4   r6   r%   r8   r&   r&   r'   Ú__get_parserT   s   úþzCommand.__get_parserNc           !         sŠ  |  dd¡}|  d¡‰|  ˆpdg¡‰t|  dd¡ƒ}|s tƒ }|  ¡ s(tdƒ‚tƒ }|dkrŸtƒ }|  ¡ D ].}z t|jƒ}t	|dd	ƒ}	|	d	u rK|j
}	| d
d„ |	dƒD ƒ¡ W q6 tttfyd   Y q6w |sktdƒ‚|dkr{| dd |¡ d ¡ |D ] }
tj|
|  dd¡d�D ]\‰}}| ‡‡fdd„|D ƒ¡ qŠq}n$|dkrÃt ¡ ‰ ˆ rÃtˆ dƒrÃ|t‡ fdd„ˆ j‡fdd„d�D ƒƒO }|sÉtdƒ‚|dkrÙ| dd |¡ d ¡ tj}t|tjƒ�rzt|ƒ\}}t	t|ƒ|ƒƒ }W n$ tttf�y } z	tdtj|f ƒ‚d	}~ww t|ttfƒ�s|g}t|ƒ}|  |¡}tƒ }|D ]Î}z|  |¡}W nf t!�yF   |dk�rC| d | ¡ Y �q& t"�yi } z|dk�r^| d!|t#|ƒf ¡ W Y d	}~�q&d	}~w t$�y   |dk�r|| d"| ¡ Y �q& t%�y•   |dk�r’| d#| ¡ Y �q&w |D ]Z}| &|¡}t'|ƒ}zt|j(||d$�ƒ}W n& t$t"f�yÔ } z|dk�rÉ| d%|t#|ƒf ¡ W Y d	}~�q˜d	}~ww |�rñ||_)| *|tƒ ¡}|D ]}| *|g ¡ +|¡ �qä�q˜�q&|�sütd&ƒ‚|dk�r| d'd d(d„ | ,¡ D ƒ¡ d ¡ | d)¡ d}g }g }tƒ }| -¡ D ]†\}}||_.||_/| -¡ D ]v\}}|D ]n}||v�rF| +|¡ | 0¡  | 1||¡�sS�q:| 2|||¡ | 3|||¡}t4|ƒ}||v �rl�q:z	| 5|||¡}W n t6�y� } zt7d*|j)t#|ƒf ƒ‚d	}~ww | 8tj9tj:¡}|||< | ;¡  | +|¡ |d7 }�q:�q4�q&t<|ƒ t=|ƒ} | d+|t=|ƒ| f ¡ ||fS ),a'  
        Searches templates containing 'compress' nodes and compresses them
        "offline" -- outside of the request/response cycle.

        The result is cached with a cache-key derived from the content of the
        compress nodes (not the content of the possibly linked files!).
        r)   r7   r   ÚhtmlÚ	verbosityr   zzNo template loaders defined. You must set TEMPLATE_LOADERS in your settings or set 'loaders' in your TEMPLATES dictionary.Úget_template_sourcesNc                 s   s   � | ]}t |ƒV  qd S ©Nr   )Ú.0Úoriginr&   r&   r'   Ú	<genexpr>~   s   € z#Command.compress.<locals>.<genexpr>Ú z¿No template paths found. None of the configured template loaders provided template paths. See https://docs.djangoproject.com/en/1.8/topics/templates/ for more information on template loaders.é   zConsidering paths:
	z
	Ú
ÚfollowlinksF)rH   c                 3   s>   � | ]‰ ˆ   d ¡st‡ fdd„ˆD ƒƒrtj ˆˆ ¡V  qdS )Ú.c                 3   s   � | ]
}t ˆ d | ƒV  qdS )z*%sNr   )rB   Úglob©Únamer&   r'   rD   ’   s   € z-Command.compress.<locals>.<genexpr>.<genexpr>N)Ú
startswithÚanyÚosÚpathÚjoin)rB   )r   ÚrootrK   r'   rD   �   s   € 
ÿþr3   Úlist_templatesc                    s   g | ]}ˆ j  ˆ |¡d  ‘qS )rF   )r1   Ú
get_source)rB   Útemplate)r6   r&   r'   Ú
<listcomp>–   s    z$Command.compress.<locals>.<listcomp>c                    s   t j | ¡d ˆ v S )Néÿÿÿÿ)rO   rP   Úsplitext)Ú_path)r   r&   r'   Ú<lambda>—   s   z"Command.compress.<locals>.<lambda>)Úfilter_funcz[No templates found. Make sure your TEMPLATE_LOADERS and TEMPLATE_DIRS settings are correct.zFound templates:
	z/Couldn't import offline context function %s: %szUnreadable template at: %s
zInvalid template %s: %s
zNon-existent template at: %s
z4UnicodeDecodeError while trying to read template %s
)ÚcontextzError parsing template %s: %s
z|No 'compress' template tags found in templates.Try running compress command with --follow-links and/or--extension=EXTENSIONSzFound 'compress' tags in:
	c                 s   s   � | ]}|j V  qd S rA   )Útemplate_name)rB   Útr&   r&   r'   rD   Ý   s   € ÿzCompressing... z)An error occurred during rendering %s: %szCdone
Compressed %d block(s) from %d template(s) for %d context(s).
)>ÚgetÚhandle_extensionsÚintr   r2   r   Úsetr   Ú
__module__Úgetattrr@   ÚupdateÚImportErrorÚAttributeErrorÚ	TypeErrorÚwriterQ   rO   Úwalkr   r:   r,   rS   ÚCOMPRESS_OFFLINE_CONTEXTÚ
isinstancer   Ústring_typesr   ÚlistÚtupleÚ_Command__get_parserr   ÚparseÚIOErrorr   r   r   ÚUnicodeDecodeErrorÚget_init_contextr
   Ú
walk_nodesr]   Ú
setdefaultr   ÚkeysÚitemsÚ_logÚ_log_verbosityÚpushÚprocess_templateÚprocess_nodeÚrender_nodelistr   Úrender_nodeÚ	Exceptionr	   ÚreplaceÚCOMPRESS_URLÚCOMPRESS_URL_PLACEHOLDERÚpopr   Úlen)!r$   ÚlogÚoptionsr)   r?   Ú	templatesÚpathsr1   Úmoduler@   rP   ÚdirsÚfilesÚcontextsÚfunctionr0   r%   Úcompressor_nodesr]   rU   Úcontext_dictr\   ÚnodesÚtemplate_nodesÚnodeÚblock_countÚcompressed_contextsÚresultsÚoffline_manifestÚrenderedÚkeyÚresultÚcontext_countr&   )r6   r   rR   r'   Úcompressa   s@  

ÿþ
ÿ
ÿþÿ
ÿÿ€ÿ


€

ÿü


ÿ€û€ñÿ

ÿÿþ



ÿ€ÿÿ
çÿÿzCommand.compress©r>   c                 C   sZ   g }|D ]}|  | dd¡ d¡¡ qt|ƒD ]\}}| d¡s(d||  ||< qt|ƒS )aÓ  
        organizes multiple extensions that are separated with commas or
        passed by using --extension/-e multiple times.

        for example: running 'django-admin compress -e js,txt -e xhtml -a'
        would result in an extension list: ['.js', '.txt', '.xhtml']

        >>> handle_extensions(['.html', 'html,js,py,py,py,.py', 'py,.py'])
        ['.html', '.js']
        >>> handle_extensions(['.html, txt,.tpl'])
        ['.html', '.tpl', '.txt']
        ú rE   ú,rI   z.%s)r-   r�   ÚsplitÚ	enumeraterM   rb   )r$   r   Úext_listÚextÚir&   r&   r'   r`     s   
€zCommand.handle_extensionsc                 K   s    t js| d¡stdƒ‚t js| d¡stdƒ‚| dtj¡ i }dd„ | dg ¡D ƒp.dg}|D ]}| ¡ }||d	< | j	d
i |¤Ž | 
tƒ ¡ q1t|ƒ d S )Nr    zTCompressor is disabled. Set the COMPRESS_ENABLED setting or use --force to override.zUOffline compression is disabled. Set COMPRESS_OFFLINE or use the --force to override.r†   c                 S   s   g | ]}|  ¡ ‘qS r&   )Ústrip)rB   r0   r&   r&   r'   rV   /  s    z"Command.handle.<locals>.<listcomp>r   r7   r)   r&   )r   ÚCOMPRESS_ENABLEDr_   r	   ÚCOMPRESS_OFFLINErv   ÚsysÚstdoutÚcopyrœ   re   r   r   )r$   r‡   Úmanifestr   r)   Úoptsr&   r&   r'   Úhandle!  s$   ÿ
ÿzCommand.handlerA   )r�   )
Ú__name__rc   Ú__qualname__r   r(   r2   rp   rœ   r`   r­   r&   r&   r&   r'   r   $   s    
 
,r   F)*Ú
__future__r   rO   r¨   Úcollectionsr   r   r   Ú	importlibr   r7   Údjango.core.management.baser   r	   Údjango.templater
   Údjango.utilsr   Údjango.utils.encodingr   Údjango.template.loaderr   r   Úcompressor.cacher   r   r   Úcompressor.confr   Úcompressor.exceptionsr   r   r   Úcompressor.utilsr   ÚPY3Úior   Ú	cStringIOrf   r   Úrequires_system_checksr&   r&   r&   r'   Ú<module>   s:    ÿ  
