
    ~+h1                     p   d Z ddlmZ ddlZ	 ddlmZmZ ddl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mZmZ dZ ej0                  d      Zej5                   ej6                                ddlZej:                  j=                  d      rej?                  ej@                         nej?                  ejB                          G d de"      Z# G d de#      Z$ G d de#      Z% G d de#      Z&ddZ' G d de(      Z) G d de(      Z* G d de"      Z+d Z,d Z-y# e
$ r ddlmZmZ ddlm	Z	 Y w xY w) z@Contains the core functionality that manages merging of assets.
    )with_statementN)Requesturlopen)	HTTPError)open)six)filter   )cmp_debug_levelsStringIO	hash_func)FileHunk
MemoryHunkmerge
FilterToolMoreThanOneFilterError	NoFilterszwebassets.debugWEBASSETS_DEBUGc                   .    e Zd ZdZd Zd Zd Zd Zd Zy)BaseHunkzAbstract base class.
    c                     t               NNotImplementedErrorselfs    B/var/www/html/venv/lib/python3.12/site-packages/webassets/merge.pymtimezBaseHunk.mtime,       !##    c                 4    t        | j                               S r   )r   datar   s    r   idzBaseHunk.id/   s    %%r    c                 R    t        |t              rt        |       t        |      k(  S y)NF)
isinstancer   r   )r   others     r   __eq__zBaseHunk.__eq__2   s#    eX&T?i&666r    c                     t               r   r   r   s    r   r"   zBaseHunk.data8   r   r    c                     t        |dd      5 }|j                  | j                                d d d        y # 1 sw Y   y xY wNwutf-8encoding)r   writer"   r   filenamefs      r   savezBaseHunk.save;   s7    (C'2 	!aGGDIIK 	! 	! 	!s	    8AN)	__name__
__module____qualname____doc__r   r#   r'   r"   r3    r    r   r   r   (   s     $&$!r    r   c                   (    e Zd ZdZd Zd Zd Zd Zy)r   z-Exposes a single file through as a hunk.
    c                     || _         y r   )r1   )r   r1   s     r   __init__zFileHunk.__init__D   s	     r    c                 P    d| j                   j                  d| j                  dS N< >)	__class__r4   r1   r   s    r   __repr__zFileHunk.__repr__G   s     NN33T]]CCr    c                      y r   r8   r   s    r   r   zFileHunk.mtimeJ       r    c                     t        | j                  dd      }	 |j                         |j                          S # |j                          w xY w)Nrr,   r-   )r   r1   readclose)r   r2   s     r   r"   zFileHunk.dataM   s5    g6	668GGIAGGIs	   : AN)r4   r5   r6   r7   r;   rB   r   r"   r8   r    r   r   r   @   s    !Dr    r   c                   $    e Zd ZdZddZd Zd Zy)UrlHunkzRepresents a file that is referenced by an Url.

    If an environment is given, it's cache will be used to cache the url
    contents, and to access it, as allowed by the etag/last modified headers.
    Nc                      || _         || _        y r   )urlenv)r   rL   rM   s      r   r;   zUrlHunk.__init__\   s    r    c                 P    d| j                   j                  d| j                  dS r=   )rA   r4   rL   r   s    r   rB   zUrlHunk.__repr__`   s     NN33TXX>>r    c                    t        | d      st        | j                        }| j                  rw| j                  j                  ra| j                  j                  j                  dd| j                  f      }|r-|\  }}|r|j                  d|       |r|j                  d|       	 t        |      }t        j                  |      5  |j                         }d d d        t        t        j                        r|j                  d      }|| _        | j                  r| j                  j                  r| j                  j                  j!                  dd| j                  f|j"                  j                  d      |j"                  j                  d      f       | j                  j                  j!                  dd	| j                  f| j                         | j                  S | j                  S # 1 sw Y   xY w# t$        $ r\}|j&                  d
k7  r | j                  j                  j                  dd	| j                  f      | _        Y d }~| j                  S d }~ww xY w)N_datarL   headerszIf-None-MatchzIf-Modified-Sincer,   ETagzLast-Modifiedcontentsi0  )hasattr
URLRequestrL   rM   cacheget
add_headerr   
contextlibclosingrG   r%   r   binary_typedecoderP   setrQ   r   code)r   requestrQ   etaglmodresponser"   es           r   r"   zUrlHunk.datac   s   tW% *G xxDHHNN((..,,Itxx02!(JD$W//FW//0CTJR"7+  ''1 +#==?D+dCOO4;;w/D!
 88HHNN&&	4884!))--f5!))--o>@A HHNN&&z488'DdjjQzztzz+ +  O66S=!XX^^//
DHH0MN

 zz'Os%   'G= G00G:=	I"AII"r   )r4   r5   r6   r7   r;   rB   r"   r8   r    r   rJ   rJ   U   s    ?$r    rJ   c                   0    e Zd ZdZddZd Zd Zd Zd Zy)	r   zContent that is no longer a direct representation of a source file. It
    might have filters applied, and is probably the result of merging multiple
    individual source files together.
    Nc                 (    || _         |xs g | _        y r   )rP   files)r   r"   rf   s      r   r;   zMemoryHunk.__init__   s    
[b
r    c                 N    d| j                   j                  dt        |       dS r=   )rA   r4   r   r   s    r   rB   zMemoryHunk.__repr__   s     !NN33Yt_EEr    c                      y r   r8   r   s    r   r   zMemoryHunk.mtime   rD   r    c                 z    t        | j                  d      r| j                  j                         S | j                  S )NrG   )rT   rP   rG   r   s    r   r"   zMemoryHunk.data   s+    4::v&::??$$zzr    c                     t        |dd      }	 |j                  | j                                |j                          y # |j                          w xY wr*   )r   r/   r"   rH   r0   s      r   r3   zMemoryHunk.save   s:    31	GGDIIK GGIAGGIs   A   Ar   )	r4   r5   r6   r7   r;   rB   r   r"   r3   r8   r    r   r   r      s!    
!F
r    r   c           	          |sd}t        |j                  | D cg c]  }|j                          c}            S c c}w )zJMerge the given list of hunks, returning a new ``MemoryHunk`` object.
    
)r   joinr"   )hunks	separatorhs      r   r   r      s4     	inn%>1affh%>?@@%>s   ;c                       e Zd Zd Zy)r   c                 >    t         j                  | |       || _        y r   )	Exceptionr;   filters)r   messagert   s      r   r;   zMoreThanOneFilterError.__init__   s    4)r    N)r4   r5   r6   r;   r8   r    r   r   r      s    r    r   c                       e Zd Zy)r   N)r4   r5   r6   r8   r    r   r   r      s    r    r   c                   6    e Zd ZdZdZdZd	dZd Zd
dZddZ	y)r   a  Can apply filters to hunk objects, while using the cache.

    If ``no_cache_read`` is given, then the cache will not be considered for
    this operation (though the result will still be written to the cache).

    ``kwargs`` are options that should be passed along to the filters.
    )inputoutput)r   concatNc                 6    || _         || _        |xs i | _        y r   )rV   no_cache_readkwargs)r   rV   r|   r}   s       r   r;   zFilterTool.__init__   s    
*lr    c                    | j                   rb| j                  sVt        j                  d|       | j                   j	                  |      }|dvr!t        j                  d|       t        |      S  |       j                         }| j                   r2t        j                  d|       | j                   j                  ||       t        |      S )z5Return cache value ``key``, or run ``func``.
        zChecking cache for key %s)FNzUsing cached result for %sz#Storing result in cache with key %s)rV   r|   logdebugrW   r   getvaluer]   )r   keyfunccontents       r   _wrap_cachezFilterTool._wrap_cache   s     ::%%		5s;**..--/II:C@%g..&//#::II;SBJJNN3('""r    c                   
 | j                   v sJ t        j                  d|       D cg c]  }t        |d      s| c}st        j                  dd       S | j                  j                         

j                  |xs i        
fd}g }
rD ]  }| |j                  di 
z  } dt              |f}	| j                  |	|      S c c}w )a  Apply the given list of filters to the hunk, returning a new
        ``MemoryHunk`` object.

        ``kwargs`` are options that should be passed along to the filters.
        If ``hunk`` is a file hunk, a ``source_path`` key will automatically
        be added to ``kwargs``.
        zANeed to run method "%s" of filters (%s) on hunk %s with kwargs=%sNzNo filters have "z#" methods, returning hunk unchangedc                      t        j                               } D ]L  }t        j                  d|       t        d      } t	        |      | |fi  |} | j                  d       N | S )Nz)Running method "%s" of  %s with kwargs=%s r   )r   r"   r   r   getattrseek)r"   r	   outrt   hunkkwargs_finaltypes      r   r   zFilterTool.apply.<locals>.func   sp    DIIK(D! 		E&,0sm%%dC@<@		! Kr    r   r8   )
VALID_TRANSFORMSr   r   r   r}   copyupdateget_additional_cache_keystupler   )r   r   rt   r   r}   r2   r   additional_cache_keysr	   r   r   s    ```      @r   applyzFilterTool.apply   s     t,,,,,		 #WdF	< &@D$)?1@II%), -K{{'')FLb)
	 !#! Z%)I)I)I)YL)YY%Z  tU7^T3HIT**Q As   C"C"c           	      |   | j                   v sJ t        j                  d       D cg c]  }t        |d      s| c}s"t        j                  dz         t	               t              dkD  r6t        ddj                  D cg c]  }|j                   c}      z        | j                  j                         j                  xs i        fd}g }rD ]  }	| |	j                  d	i z  } dt              |xs g |f}
| j                  |
|      S c c}w c c}w )
a  Apply a filter that is not a "stream in, stream out" transform (i.e.
        like the input() and output() filter methods).  Instead, the filter
        method is given the arguments in ``args`` and should then produce an
        output stream. This is used, e.g., for the concat() and open() filter
        methods.

        Only one such filter can run per operation.

        ``cache_key`` may be a list of additional values to use as the cache
        key, in addition to the default key (the filter and arguments).
        zJNeed to run method "%s" of one of the filters (%s) with args=%s, kwargs=%sNzNo filters have a "%s" methodr
   z$These filters cannot be combined: %sz, c                      d   } t        d      }t        j                  d|         t        |       |gi  |S )Nr   r   z1Running method "%s" of %s with args=%s, kwargs=%s)r   r   r   r   )r	   r   argsrt   r}   r   r   s     r   r   z#FilterTool.apply_func.<locals>.func7  sM    QZF3-CIIIfdF,!GFD!#===Jr    r   r8   )VALID_FUNCSr   r   r   r   lenr   rm   namer}   r   r   r   r   r   )r   rt   r   r   r}   	cache_keyr2   r   r   r	   r   r   s    ````      @r   
apply_funczFilterTool.apply_func  sR    t'''''		 ,-17D&	J &@D$)?1@II5<=+w<!(6IIw7!qvv78:;BD D {{'')FLb)	 	 !#! Z%)I)I)I)YL)YY%Z tU7^T9?DYZT**7 A 8s   D4D4D9)NFNr   )NN)
r4   r5   r6   r7   r   r   r;   r   r   r   r8   r    r   r   r      s)     ,&K#
#"4+l++r    r   c                 `    t        | dd       }|r|D ]  }||vs|j                  |        |S )aS  Merge two filter lists into one.

    Duplicate filters are removed. Since filter order is important, the order
    of the arguments to this function also matter. Duplicates are always
    removed from the second filter set if they exist in the first.

    The result will always be ``filters1``, with additional unique filters
    from ``filters2`` appended. Within the context of a hierarchy, you want
    ``filters2`` to be the parent.

    This function presumes that all the given filters inherit from ``Filter``,
    which properly implements operators to determine duplicate filters.
    N)listappend)filters1filters2resultr2   s       r   merge_filtersr   H  s>     (1+F 	!A;a 	! Mr    c                 v    | D cg c])  }|j                   t        ||j                         dk  r|+ c}S c c}w )zyReturn from the list in ``filters`` those filters which indicate that
    they should run for the given debug level.
    r   )max_debug_levelr   )rt   levelr2   s      r   select_filtersr   ^  sH      @!  (q'8'89Q>  @ @ @s   .6r   ).r7   
__future__r   rY   urllib.requestr   rU   r   urllib.errorr   ImportErrorurllib2loggingior   	webassetsr   webassets.six.movesr	   utilsr   r   r   __all__	getLoggerr   
addHandlerStreamHandlerosenvironrW   setLevelDEBUGERRORobjectr   r   rJ   r   r   rs   r   r   r   r   r   r8   r    r   <module>r      s   % "=&    & 8 82 g)* $w$$& ' 	::>>#$LLLL!v !0x *2h 2j DAY 		 	C+ C+L,@k
  "6!!"s   D D54D5