
    _f<                        d dl Z d dl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 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mZmZ d dlmZ d dlmZmZm Z  d dl!m"Z"  e            Z# G d de          Z$dS )    N)Lock)OrderedDictdefaultdictfnmatch)import_module)BaseCommandCommandError)Context	smart_str)get_template)engines)get_offline_hexdigestwrite_offline_manifestget_offline_manifest)settings)OfflineGenerationErrorTemplateSyntaxErrorTemplateDoesNotExist)get_mod_funcc                   X    e Zd ZdZg Zd Zd Zd Zd Ze	d             Z
ddZd	 Zd
 ZdS )Commandz6Compress content outside of the request/response cyclec                     |                     ddddd           |                     ddd	d
dd           |                     dd	d
dd           |                     dg 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-linkszFollow 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--enginezSpecifies 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parsers     Y/var/www/html/env/lib/python3.11/site-packages/compressor/management/commands/compress.pyadd_argumentszCommand.add_arguments*   s     	 	
 	
 	
 	4 	 	
 	
 	
 	#   	 		
 		
 		
 	I  	 	
 	
 	
 	
 	
    c                 R   g }t          j                    D ]I}t          |d          r7|                    |j                            |j        j                             Jg }|D ]B}t          |d          r|                    |j                   -|                    |           C|S )Nengineloaders)r   allhasattrextendr+   get_template_loadersr,   r   )r%   template_source_loaderser,   loaders        r'   get_loaderszCommand.get_loadersQ   s    "$ 	 	Aq(## '..H11!(2BCC    . 	' 	'Fvy)) 'v~....v&&&&r)   c                     t          j        d          rt           j        nd}|dk    r'ddlm} t          j                    } |||          }n(|dk    rddlm}  ||	          }nt          d
          |S )NFILE_CHARSETzutf-8jinja2r   )Jinja2Parser)charsetenvdjango)DjangoParser)r9   z$Invalid templating engine specified.)	r   is_overriddenr6   compressor.offline.jinja2r8   COMPRESS_JINJA2_GET_ENVIRONMENTcompressor.offline.djangor<   r   )r%   r+   r9   r8   r:   r&   r<   s          r'   __get_parserzCommand.__get_parserl   s    %-%;N%K%KXH!!QX 	 X>>>>>>:<<C!\'s;;;FFx>>>>>>!\'222FF()OPPPr)   c                    ! |                                  st          d          t                      }|dk    r$t                      }|                                  D ]v}	 t          |j                  }	t          |	dd          }
|
|j        }
|                    d  |
d          D                        Z# t          t          t          f$ r Y sw xY w|st          d          |dk    r.|                    d	d
                    |          z   dz              |D ]B t          j         |          D ])\  !}}|                     !fd|D                        *Cn`|dk    rZt          j                    rEt#          d          r5|t          fd                    fd          D                       z  }|st          d          |dk    r.|                    dd
                    |          z   dz              t          j        }t)          |t*                    rw	 t-          |          \  }	} t          t          |	          |                      }n]# t          t          t          f$ r$}t          dt          j        d|          d}~ww xY wt)          |t.          t0          f          s|g}|                     |          }g }|dk    r|                    d           |D ]}	 |                    |          }||_        |                    |           5# t:          $ r! |dk    r|                    d|z             Y _t<          $ r9}|dk    r)|                    d|dt?          |          d           Y d}~d}~wt@          $ r! |dk    r|                    d|z             Y tB          $ r! |dk    r|                    d|z             Y w xY wd}d}tE                      }g }|D ]}tE                      }|D ]}tG          |$                    |                    }	 t/          |%                    ||                    }nR# t@          t<          f$ r>}|dk    r.|                    d|j        dt?          |          d           Y d}~d}~ww xY w|rU|&                    |tE                                }|D ]0}|dz  }|&                    |g                               |           1tN          j(        )                    d !          }|*                                D ]2\  }}||_+        ||_,        |-                    | j.        |||||           3|/                    d"#           |dz  }|r|d         |st          d$          |dk    r(|                    d%ta          |          ||fz             |ta          |          |1                                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!).
        zzNo template loaders defined. You must set TEMPLATE_LOADERS in your settings or set 'loaders' in your TEMPLATES dictionary.r;   get_template_sourcesNc              3   4   K   | ]}t          |          V  d S )Nr   ).0origins     r'   	<genexpr>z#Command.compress.<locals>.<genexpr>   s;       ! !.4	&))! ! ! ! ! !r)    zNo template paths found. None of the configured template loaders provided template paths. See https://docs.djangoproject.com/en/2.1/topics/templates/ for more information on template loaders.   zConsidering paths:
	z
	
)followlinksc              3      K   | ]r                     d           t          fdD                       2t          j                            t          j                                                V  sdS ).c              3   >   K   | ]}t          d |z            V  dS )z*%sNr   )rE   globnames     r'   rG   z-Command.compress.<locals>.<genexpr>.<genexpr>   s1      SSedl ; ;SSSSSSr)   N)
startswithanyospathrelpathjoin)rE   rP   r   rT   roots    @r'   rG   z#Command.compress.<locals>.<genexpr>   s       % % #s33%  SSSS
SSSSS	%T4(@(@$GG% % % % % %r)   r7   list_templatesc                 R    g | ]#}j                             |          d          $S )   )r3   
get_source)rE   templater:   s     r'   
<listcomp>z$Command.compress.<locals>.<listcomp>   s@       $ 
--c8<<Q?  r)   c                 R    t           j                            |           d         v S )N)rS   rT   splitext)_pathr   s    r'   <lambda>z"Command.compress.<locals>.<lambda>   s$    bg6F6Fu6M6Mb6Q)7* r)   )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 : rZ   zCompressing... zUnreadable template at: %s
zInvalid template zNon-existent template at: %s
z4UnicodeDecodeError while trying to read template %s
r   )contextzError parsing template    )max_workersT)waitz|No 'compress' template tags found in templates.Try running compress command with --follow-links and/or--extension=EXTENSIONSzCdone
Compressed %d block(s) from %d template(s) for %d context(s).
)2r4   r   setr   
__module__getattrrC   updateImportErrorAttributeError	TypeErrorwriterV   rS   walkr   r?   r.   rX   COMPRESS_OFFLINE_CONTEXT
isinstancestrr   listtuple_Command__get_parserparsetemplate_namer   IOErrorr   r   r   UnicodeDecodeErrorr   r   get_init_context
walk_nodes
setdefault
concurrentfuturesThreadPoolExecutoritems_log_log_verbositysubmit_compress_templateshutdownlenvalues)"r%   r+   r   	verbosityr#   log	templatespathsr3   modulerC   dirsfilescontextsfunctionr2   r&   fine_templatesry   r\   contexts_countnodes_countoffline_manifesterrorscontext_dictcompressor_nodesre   nodestemplate_nodesnodepoolr:   rT   rW   s"     `                            @@@r'   compresszCommand.compress~   s    !! 	((   EE	XEEE**,,  
*6+<==F+26;QSW+X+X(+3/5/J,LL ! !8L8LR8P8P! ! !     $^Y?   D  ,   A~~		2V[[5G5GG$NOOO  )+<)P)P)P  %D$$$ % % % % % %$)% % %     x:<<C 	ws$455 	S   (+(:(:)* )* )* )* ); ) )    	  	((  
 >>II,v{{9/E/EELMMM4h$$ 
	"#/#9#9 C7=#8#8(CCEE"K;   !k888!!=  
 HtUm44 	" zH""6**>>II'(((& 	 	M!<<66)6&%%h////   >>II<}LMMM&   >>III7D}}iPQllllS   '   >>II>NOOO%   >>II(*78    &==$ '	  '	 L*}}* L L!&"9"9,"G"GHH	 !2!28W!2!M!MNNEE,.AB    A~~			'555y||||E   HHHH  L%5%@%@ +--& &N !& L L#q(&11$;;BB7KKKK%88Q8GGD#3#9#9#;#;  % #*3'+$    MMtM$$$aNN  	)O 	()   >>IIW'((+~FG    %5!6!68H8O8O8Q8QQQsm   "AB99CC(7I   J7JJ41L&&(O$	O$/N+O$:'O$#O$5$QR)+4R$$R)c                    |                                 D ]]\  }}|D ]S}|                                 |                    ||          s.|                    |||           |                    |||          }t          |          }	t          5  |	| v r	 d d d            d | |	<   d d d            n# 1 swxY w Y   	 |                    |||          }
nW# t          $ rJ}|	                    t          d|j        dt          |                               | |	= Y d }~  d S d }~ww xY w|
                    t          j        t          j                  }
|
| |	<   |                                 U_d S )Nz#An error occurred during rendering rd   )r   pushprocess_templateprocess_noderender_nodelistr   offline_manifest_lockrender_node	Exceptionr   r
   ry   r   replacer   COMPRESS_URLCOMPRESS_URL_PLACEHOLDERpop)r   r   r&   r\   r   r   node_contextsre   renderedkeyresultr2   s               r'   r   zCommand._compress_template5  s   #(;;== #	 #	D-( " "..xAA ##Hgt<<<!11(GTJJ+H55 + 1 1... 1 1 1 1 1 1 1 -1$S)	1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
#//'4HHFF    MM$$,$:$:$:IaLLLJ    )-FFFFFFF  )8+L  )/ %E"#	 #	s0   B2!B22B69B6>C
D* =D%%D*htmlc                    g }|D ]>}|                     |                    dd                              d                     ?t          |          D ](\  }}|                    d          sd||         z  ||<   )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']
         rH   ,rM   z.%s)r/   r   split	enumeraterQ   ri   )r%   r   ext_listextis        r'   handle_extensionszCommand.handle_extensions\  s      	= 	=COOCKKR0066s;;<<<<)) 	2 	2FAs>>#&& 2#hqk18}}r)   c                       | j         di | d S )N )handle_inner)r%   optionss     r'   handlezCommand.handleq  s!    $$G$$$$$r)   c                    t           j        s$|                    d          st          d          t           j        s$|                    d          st          d          |                    dt
          j                  }|                    dd          }|                    dd          }|                     |                    d	          pd
g          }d |                    dg           D             pdg}i }d}g }	|D ]N}
|                     |
||||          \  }}}|		                    |           ||z  }|
                    |           Ot          |           ||	f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   r   rZ   r#   Fr   r   c                 6    g | ]}|                                 S r   )strip)rE   r2   s     r'   r]   z(Command.handle_inner.<locals>.<listcomp>  s     AAA17799AAAr)   r   r;   r   )r   COMPRESS_ENABLEDgetr
   COMPRESS_OFFLINEsysstdoutr   r   r/   rl   r   )r%   r   r   r   r#   r   r   final_offline_manifestfinal_block_countfinal_resultsr+   r   block_countresultss                 r'   r   zCommand.handle_innert  s   ( 	W1E1E 	6   ( 	;;w'' "G  
 kk%,,KKQ//	{{>599++GKK,E,E,Q&RR
AAgkk)R&@&@AAAOhZ!# 	< 	<F59]]
I|S6 62k7   ))),"))*:;;;;5666 -//r)   N)r   )__name__rj   __qualname__r   requires_system_checksr(   r4   rw   r   staticmethodr   r   r   r   r   r)   r'   r   r   %   s        CD%
 %
 %
N  6  $uR uR uRn $ $ \$L   *% % %0 0 0 0 0r)   r   )%rS   r   concurrent.futuresr   	threadingr   collectionsr   r   r   	importlibr   r;   django.core.management.baser	   r
   django.templater   django.utils.encodingr   django.template.loaderr   r   compressor.cacher   r   r   compressor.confr   compressor.exceptionsr   r   r   compressor.utilsr   r   r   r   r)   r'   <module>r      s   				 



           0 0 0 0 0 0 0 0       # # # # # #  A A A A A A A A     # # # # # # + + + + + +      $ # # # # #         
 % $ $ $ $ $         
 * ) ) ) ) ) m0 m0 m0 m0 m0k m0 m0 m0 m0 m0r)   