
    _f>                         d dl 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 d dlmZ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 d dlmZ d dlmZmZm Z  d\  Z!Z"d\  Z#Z$ G d d          Z%dS )    N)import_module)url2pathname)ContentFile)	mark_safe)render_to_string)cached_property)get_hexdigest	get_mtime)settings)CompressorErrorUncompressableFileErrorFilterDoesNotExist)CachedCompilerFilter)compressor_file_storage)post_compress)	get_classget_mod_funcstaticfiles)inlinefile)inputoutputc                      e Zd ZdZi Z	 	 	 	 	 	 ddZd Zed             Zd Z	d Z
d	 Zdd
Zd Zd Zed             Zed             Zed             Zed             Zd dZd Zd dZ	 d!dZd Zd"dZddZd#dZd#dZd#dZddZdS )$
Compressorzi
    Base compressor object to be subclassed for content type
    depending implementations details.
    N   c                    |t           j        |         | _        n|| _        ||| _        n|| _        |pd| _        t           j                            d          | _        t           j        | _	        g | _
        |pi | _        || _        i | _        t          t           j                  | _        t"          j        | _        d | _        || _        || _        d S )N /)r   COMPRESS_FILTERSfiltersoutput_prefixcontentCOMPRESS_OUTPUT_DIRstrip
output_dirDEFAULT_CHARSETcharsetsplit_contentcontextresource_kindextra_contextdictCOMPRESS_PRECOMPILERSprecompiler_mimetypesr   finders_storagelog	verbosity)
selfr*   r"   r!   r)   r    r1   r2   argskwargss
             A/var/www/html/env/lib/python3.11/site-packages/compressor/base.py__init__zCompressor.__init__$   s     ?#4]CDLL"DL !.D!.D}""6<<SAA/}"*%)(*H%I%I""*"    c                     t          | j        | j        | j        | j                  }|                    |            | j        | j        fi |S )N)r"   r)   r!   r    )r,   r"   r)   r!   r    update	__class__r*   )r3   r5   keywordss      r6   copyzCompressor.copyE   s\    LL,L	
 
 
 	t~d0==H===r8   c                     ddl m} |S )Nr   )default_storage)compressor.storager?   )r3   r?   s     r6   storagezCompressor.storageO   s    666666r8   c                     t           )z
        To be implemented in a subclass, should return an
        iterable with four values: kind, value, basename, element
        )NotImplementedErrorr3   s    r6   split_contentszCompressor.split_contentsU   s
    
 "!r8   c                 p    	 t          | d|z            }|r|S n# t          $ r Y nw xY wd| j        d|dS )z?
        Returns the template path for the given mode.
        ztemplate_name_%szcompressor/_z.html)getattrAttributeErrorr*   )r3   modetemplates      r6   get_template_namezCompressor.get_template_name\   sg    	t%7$%>??H    	 	 	D	*.*<*<*<dddCCs    
''c                 0   	 | j         j        }n# t          $ r t          j        }Y nw xY wt          |          }|                    |          st          d|d|d          |                    |dd          }|	                    dd          d         S )z
        Takes full path to a static file (eg. "/static/css/style.css") and
        returns path with storage's base url removed (eg. "css/style.css").
        'z&' isn't accessible via COMPRESS_URL ('z') and can't be compressedr   r   ?r   )
rA   base_urlrI   r   COMPRESS_URLstr
startswithr   replacesplit)r3   urlrP   basenames       r6   get_basenamezCompressor.get_basenameh   s    
	-|,HH 	- 	- 	-,HHH	- x==~~h'' 	)) !$XXX/  
 ;;xQ//~~c1%%a((s    ((c                    g }|r]t           j                            |          d         }|                    t           j                            |          d                    |                    t          |d          | j        g           t           j                            | j	        | j
        d                    |                    S )a  
        Returns file path for an output file based on contents.

        Returned path is relative to compressor storage's base url, for
        example "CACHE/css/58a8c0714e59.css".

        When `basename` argument is provided then file name (without extension)
        will be used as a part of returned file name, for example:

        get_filepath(content, "my_file.css") -> 'CACHE/css/my_file.58a8c0714e59.css'
        r   r      .)ospathrU   appendsplitextextendr	   r*   joinr%   r!   )r3   r"   rW   partsfilenames        r6   get_filepathzCompressor.get_filepath   s      	8w}}X..q1HLL))(33A6777mGR00$2DEFFFw||DOT-?%QQQr8   c                    d}t           j        s	 | j        r8| j        dk    r-| j                            d                    |                     | j                            |          }| j                            |          sd}nd# t          $ rW | j        r%| j        dk    r| j                            d           t          j        |          rt          j        |          }Y nw xY w|s| j        r| j        rr| j        dk    rgt           j        s.| j                            d                    |                     n-| j                            d                    |                     | j                            t          |                    }|r|S t          d|dt           j        d| j        rd	pd
          )z
        Returns full path to a file, for example:

        get_filename('css/one.css') -> '/full/path/to/static/css/one.css'
        N   zLooking for '{}' in storage
zCRemote storages don't implement path, looking for the file locally
z4'{}' was not found in storage, using static finders
zUsing static finders for '{}'
rN   z+' could not be found in the COMPRESS_ROOT 'z or with staticfiles.r[   )r   DEBUGr1   r2   writeformatrA   r]   existsrC   r   r/   findr   r   COMPRESS_ROOT)r3   rW   rc   s      r6   get_filenamezCompressor.get_filename   s     ~ 	FF 8 U! 3 3HNN#B#I#I(#S#STTT<,,X66|**844 $#H& F F F8 ! 3 3HNN^   +1(;; F6;HEEHF  
	ADL 
	Ax WDNa//~ WHNNOVV$     HNN#D#K#KH#U#UVVV|((h)?)?@@H 	O%% &&&8!8?C?	
 
 	
s   A5B AC'&C'c           
      4   |dk    rd}t          j        |d|          5 }	 |                                cddd           S # t          $ r}t	          d|d|          d}~wt
          $ r}t	          d|d|d	|          d}~ww xY w# 1 swxY w Y   dS )
zS
        Reads file contents using given `charset` and returns it as text.
        zutf-8z	utf-8-sigrNzIOError while processing 'z': z%UnicodeDecodeError while processing 'z' with charset z: )codecsopenreadIOErrorr   UnicodeDecodeError)r3   rc   r'   fdes        r6   get_filecontentzCompressor.get_filecontent   s"    g!G[300 	Bwwyy	 	 	 	 	 	 	 	    -->FhhJ   &   -- )1'''11>  	 	 	 	 	 	 	 	 	 	s:   BA
B
A  B
-BB

BBBc                 P     t          t          j                  | j                  S N)r   r   COMPRESS_PARSERr"   rD   s    r6   parserzCompressor.parser   s    2y1224<@@@r8   c                 $    d | j         D             S )Nc                 ,    g | ]}t          |          S  )r   ).0
filter_clss     r6   
<listcomp>z-Compressor.cached_filters.<locals>.<listcomp>   s     EEE*	*%%EEEr8   )r    rD   s    r6   cached_filterszCompressor.cached_filters   s    EEEEEEr8   c                 >    d |                                  D             S )Nc                 f    g | ].\  }}}}|t           k    t          t          |                    /S r~   )SOURCE_FILErR   r
   )r   kindvaluerW   elems        r6   r   z%Compressor.mtimes.<locals>.<listcomp>   sE     
 
 
+eXt{"" 	%  !!"""r8   )rE   rD   s    r6   mtimeszCompressor.mtimes   s/    
 
/3/B/B/D/D
 
 
 	
r8   c                     t          d                    | j        g| j        z                                 | j                  d          S )Nr   rZ   )r	   ra   r"   r   encoder'   rD   s    r6   cachekeyzCompressor.cachekey   s@    GGT\NT[01188FF
 
 	
r8   Fc              #   f  K   t           j        p|}|                                 D ]
\  }}}}d}| j                            |          }|                    d| j                  }	t          ||||	d}
|t          k    r't          |
|          }
| 
                    ||	          }| j        r | j        |fi |
\  }}|r | j        || j        fi |
V  |r=| j        D ]}|j        r | j        ||gfi |
}|                     ||d|          V  | j                            |          V  dS )z
        The heart of content parsing, iterates over the
        list of split contents and looks at its kind
        to decide what to do with it. Should yield a
        bunch of precompiled and/or rendered hunks.
        Fr'   )methodr   r   rW   r'   )rc   T)forcedrW   N)r   COMPRESS_ENABLEDrE   r{   elem_attribsgetr'   METHOD_INPUTr   r,   rw   r.   
precompilefilterr   run_with_compression_disabledhandle_outputelem_str)r3   r   enabledr   r   rW   r   precompiledattribsr'   optionsr   s               r6   hunkszCompressor.hunks   s      +5v+/+>+>+@+@ 	1 	1'D%4Kk..t44Gkk)T\::G&$" G {""w777,,UG<<) G%4T_U%F%Fg%F%F"U 1!dk%)<HHHHHHHH 1"&"5 L LJ!? L +EJ< K K7 K K((uTH(UUUUUUk**40000007	1 	1r8   c                 F    |                      || j        t                    S )zl
        Passes the concatenated content to the 'output' methods
        of the compressor filters.
        )r   )r   r   METHOD_OUTPUT)r3   r"   s     r6   filter_outputzCompressor.filter_output  s    
 {{7D$7{NNNr8   c                 d    g }|                      |          D ]}|                    |           |S )zk
        Passes each hunk (file or code) to the 'input' methods
        of the compressor filters.
        )r   r^   )r3   r   r"   hunks       r6   filter_inputzCompressor.filter_input   s>    
 JJv&& 	! 	!DNN4    r8   c           	      b   |sd|fS | j                             |          }|                    dd          }|d|fS | j                            |          }	|	|| j        v rd|fS t          d|z            t          |	          \  }
}	 t          |
          }n@# t          t          f$ r, t          || j        |||	|          }d |j        d	i |fcY S w xY w	 t          ||          }n # t          $ r t          d|	z            w xY w |||| j        ||          }d |j        d	i |fS )
z
        Processes file using a pre compiler.

        This is the place where files like coffee script are processed.
        FtypeNzQCouldn't find any precompiler in COMPRESS_PRECOMPILERS setting for mimetype '%s'.)r"   filter_typerc   r'   commandmimetypeTzCould not find "%s".)attrsr   r'   rc   r~   )r{   r   r   r.   output_mimetypesr   r   r   ImportError	TypeErrorr   r*   r   rH   rI   r   )r3   r"   r   r   rc   r'   r5   r   r   filter_or_commandmod_namecls_namemodr   precompiler_classs                  r6   r   zCompressor.precompile*  s     	"'>!((..99VT**'>! 6::8DD$4000g~%!!#+,   **;<<(	0))CCY' 		0 		0 		0) .!)!  F ////////		0	Q 'X 6 6 	Q 	Q 	Q$%;>O%OPPP	Q""*
 
 
 \V\++F++++s   B :CCC- -D
c                     |D ]J}t           ||| j                  |          }	 t          |          r |di |};# t          $ r Y Gw xY w|S )N)r   r~   )rH   r*   callablerC   )r3   r"   r    r   r5   r   filter_funcs          r6   r   zCompressor.filter]  s    ! 	 	J!
70BCCCV KK(( 4)k33F33G&   s   >
A
Ar   c                     d                     |                     |                    }|sdS t          j        s|r-|                     |          }|                     ||||          S |S )z
        The general output method, override in subclass if you need to do
        any custom modification. Calls other mode specific methods or simply
        returns the content directly.
        
r   )ra   r   r   r   r   r   )r3   rJ   r   rW   r   filtered_outputs         r6   r   zCompressor.outputi  sv     4,,V4455 	2$ 	O 	O"0088O%%dOVXNNNr8   c                     t          | d|z  d           }t          |          r |||||          S t          d|z            )Nz	output_%sz)Couldn't find output method for mode '%s')rH   r   r   )r3   rJ   r"   r   rW   output_funcs         r6   r   zCompressor.handle_outputz  sS    dK$$6==K   	@;tWfh???IDPQQQr8   c                 f   |                      ||          }| j                            |          r|r@| j                            |t	          |                    | j                                       t          | j                            |                    }| 	                    |d|i          S )z
        The output method that saves the content to a file and renders
        the appropriate template with the file's URL.
        )rW   rV   )
rd   rA   rj   saver   r   r'   r   rV   render_output)r3   rJ   r"   r   rW   new_filepathrV   s          r6   output_filezCompressor.output_file  s    
 ((8(DD|""<00 	WF 	WLlKt|8T8T,U,UVVV((6677!!$555r8   c                 2    |                      |d|i          S )za
        The output method that directly returns the content for inline
        display.
        r"   )r   r3   rJ   r"   r   rW   s        r6   output_inlinezCompressor.output_inline  s    
 !!$G(<===r8   c                 2    |                      ||||          S )z|
        The output method that returns <link> with rel="preload" and
        proper href attribute for given file.
        )r   r   s        r6   output_preloadzCompressor.output_preload  s    
 gvx@@@r8   c                    d| j         vr
i | j         d<   | j         d                             |pi            | j         d                             | j                   t          | j         d          r| j                                         }n| j         }t          j        | j        | j        ||           | 	                    |          }t          ||          S )z~
        Renders the compressor output with the appropriate template for
        the given mode and template context.
        
compressedflatten)senderr   rJ   r)   )r)   )r)   r:   r+   hasattrr   r   sendr;   r*   rL   r   )r3   rJ   r)   final_contexttemplate_names        r6   r   zCompressor.render_output  s     t|++)+DL&\"))'-R888\"))$*<===4<++ 	) L0022MM LM>#!		
 	
 	
 	
 ..t44}EEEEr8   )NNNNNr   ry   )F)NNNN)r   FN)FN)__name__
__module____qualname____doc__r   r7   r=   r   rA   rE   rL   rX   rd   rm   rw   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r~   r8   r6   r   r      s>        
 
 # # # #B> > >   _
" " "
D 
D 
D) ) )4R R R R&1
 1
 1
f  * A A _A F F _F 
 
 _
 
 
 _

$1 $1 $1 $1LO O O    EI1, 1, 1, 1,f
 
 
   "R R R R	6 	6 	6 	6> > > >A A A AF F F F F Fr8   r   )&r\   rp   	importlibr   urllib.requestr   django.core.files.baser   django.utils.safestringr   django.template.loaderr   django.utils.functionalr   compressor.cacher	   r
   compressor.confr   compressor.exceptionsr   r   r   compressor.filtersr   r@   r   compressor.signalsr   compressor.utilsr   r   r   SOURCE_HUNKr   r   r   r   r~   r8   r6   <module>r      s   				  # # # # # # ' ' ' ' ' ' . . . . . . - - - - - - 3 3 3 3 3 3 3 3 3 3 3 3 5 5 5 5 5 5 5 5 $ $ $ $ $ $         
 4 3 3 3 3 3 6 6 6 6 6 6 , , , , , , A A A A A A A A A A , [/ mYF YF YF YF YF YF YF YF YF YFr8   