ó
pÁìVc           @€ s†  d  d l  m Z m Z 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 y d  d l m Z Wn! e k
 rµ d  d l m Z n Xd  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  d l& m' Z' d \ Z( Z) d \ Z* Z+ d e, f d „  ƒ  YZ- d S(   iÿÿÿÿ(   t   with_statementt   unicode_literalsN(   t   ContentFile(   t   Context(   t   render_to_string(   t   import_module(   t	   mark_safe(   t   url2pathname(   t   get_hexdigestt	   get_mtime(   t   settings(   t   CompressorErrort   UncompressableFileErrort   FilterDoesNotExist(   t   CompilerFilter(   t   compressor_file_storage(   t   post_compress(   t	   get_classt   get_mod_funct   staticfiles(   t   cached_propertyu   inlineu   fileu   inputu   outputt
   Compressorc           B€ s.  e  Z d  Z d Z d d d 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 e d „ Z d „  Z e d „ Z d d d d d „ Z d „  Z d e d „ Z d d „ Z e d d „ Z e d d „ Z d d „ Z RS(   ui   
    Base compressor object to be subclassed for content type
    depending implementations details.
    c         O€ s‹   | p	 d |  _  | p d |  _ t j j d ƒ |  _ t j |  _ g  |  _ | pQ i  |  _	 i  |  _
 t t j ƒ |  _ t j |  _ d  |  _ d  S(   Nu    u
   compressedu   /(   t   contentt   output_prefixR
   t   COMPRESS_OUTPUT_DIRt   stript
   output_dirt   DEFAULT_CHARSETt   charsett   split_contentt   contextt   extra_contextt   dictt   COMPRESS_PRECOMPILERSt   all_mimetypesR   t   finderst   Nonet   _storage(   t   selfR   R   R   t   argst   kwargs(    (    se   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_compressor-1.4-py2.7.egg/compressor/base.pyt   __init__&   s    		c         C€ s   d d l  m } | S(   Niÿÿÿÿ(   t   default_storage(   t   compressor.storageR*   (   R&   R*   (    (    se   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_compressor-1.4-py2.7.egg/compressor/base.pyt   storage2   s    c         C€ s
   t  ‚ d S(   u…   
        To be implemented in a subclass, should return an
        iterable with four values: kind, value, basename, element
        N(   t   NotImplementedError(   R&   (    (    se   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_compressor-1.4-py2.7.egg/compressor/base.pyt   split_contents7   s    c         C€ sF   y! t  |  d | ƒ } | r  | SWn t k
 r4 n Xd |  j | f S(   u?   
        Returns the template path for the given mode.
        u   template_name_%su   compressor/%s_%s.html(   t   getattrt   AttributeErrort   type(   R&   t   modet   template(    (    se   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_compressor-1.4-py2.7.egg/compressor/base.pyt   get_template_name>   s    c         C€ s~   y |  j  j } Wn t k
 r, t j } n X| j | ƒ sU t d | | f ƒ ‚ n  | j | d d ƒ } | j d d ƒ d S(   u    
        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").
        uE   '%s' isn't accessible via COMPRESS_URL ('%s') and can't be compressedu    i   u   ?i    (	   R,   t   base_urlR0   R
   t   COMPRESS_URLt
   startswithR   t   replacet   split(   R&   t   urlR5   t   basename(    (    se   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_compressor-1.4-py2.7.egg/compressor/base.pyt   get_basenameJ   s    c         C€ s†   g  } | rB t  j j | ƒ d } | j t  j j | ƒ d ƒ n  | j t | d ƒ |  j g ƒ t  j j |  j	 |  j
 d j | ƒ ƒ S(   uª  
        Returns file path for an output file based on contents.

        Returned path is relative to compressor storage's base url, for
        example "CACHE/css/e41ba2cc6982.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.e41ba2cc6982.css'
        i   i    i   u   .(   t   ost   pathR9   t   appendt   splitextt   extendR   R1   t   joinR   R   (   R&   R   R;   t   partst   filename(    (    se   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_compressor-1.4-py2.7.egg/compressor/base.pyt   get_filepath[   s     c         C€ sÐ   d } y1 |  j j | ƒ } |  j j | ƒ s6 d } n  Wn2 t k
 rk t j | ƒ rl t j | ƒ } ql n X| r— |  j r— |  j j t | ƒ ƒ } n  | r¡ | St	 d | t
 j |  j r¿ d pÂ d f ƒ ‚ d S(   u†   
        Returns full path to a file, for example:

        get_filename('css/one.css') -> '/full/path/to/static/css/one.css'
        u3   '%s' could not be found in the COMPRESS_ROOT '%s'%su    or with staticfiles.u   .N(   R$   R,   R>   t   existsR-   R   R#   t   findR   R   R
   t   COMPRESS_ROOT(   R&   R;   RD   (    (    se   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_compressor-1.4-py2.7.egg/compressor/base.pyt   get_filenamen   s     	c      	   C€ s‡   t  j | d | ƒ l } y | j ƒ  SWnT t k
 rQ } t d | | f ƒ ‚ n, t k
 r| } t d | | | f ƒ ‚ n XWd QXd S(   uS   
        Reads file contents using given `charset` and returns it as text.
        u   ru!   IOError while processing '%s': %su<   UnicodeDecodeError while processing '%s' with charset %s: %sN(   t   codecst   opent   readt   IOErrorR   t   UnicodeDecodeError(   R&   RD   R   t   fdt   e(    (    se   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_compressor-1.4-py2.7.egg/compressor/base.pyt   get_filecontent‹   s    c         C€ s   t  t j ƒ |  j ƒ S(   N(   R   R
   t   COMPRESS_PARSERR   (   R&   (    (    se   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_compressor-1.4-py2.7.egg/compressor/base.pyt   parser›   s    c         C€ s    g  |  j  D] } t | ƒ ^ q
 S(   N(   t   filtersR   (   R&   t
   filter_cls(    (    se   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_compressor-1.4-py2.7.egg/compressor/base.pyt   cached_filtersŸ   s    c         C€ sA   g  |  j  ƒ  D]0 \ } } } } | t k r t t | ƒ ƒ ^ q S(   N(   R.   t   SOURCE_FILEt   strR	   (   R&   t   kindt   valueR;   t   elem(    (    se   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_compressor-1.4-py2.7.egg/compressor/base.pyt   mtimes£   s    c         C€ s/   t  d j |  j g |  j ƒ j |  j ƒ d ƒ S(   Nu    i   (   R   RB   R   R\   t   encodeR   (   R&   (    (    se   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_compressor-1.4-py2.7.egg/compressor/base.pyt   cachekey©   s    	c      	   c€ s4  t  j p | } x|  j ƒ  D]\ } } } } t } |  j j | ƒ } | j d |  j ƒ }	 i t d 6| d 6| d 6| d 6|	 d 6}
 | t	 k r· t
 |
 d | ƒ}
 |  j | |	 ƒ } n  |  j rÛ |  j | |
  \ } } n  | rõ |  j | |
  Vq | r|  j | | d t d | ƒVq |  j j | ƒ Vq Wd	 S(
   uß   
        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.
        u   charsetu   methodu   elemu   kindu   basenameRD   t   forcedR;   N(   R
   t   COMPRESS_ENABLEDR.   t   FalseRS   t   elem_attribst   getR   t   METHOD_INPUTRW   R    RQ   R"   t
   precompilet   filtert   handle_outputt   Truet   elem_str(   R&   R_   t   enabledRY   RZ   R;   R[   t   precompiledt   attribsR   t   options(    (    se   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_compressor-1.4-py2.7.egg/compressor/base.pyt   hunks®   s,    
	c         C€ s   |  j  | d t ƒS(   ul   
        Passes the concatenated content to the 'output' methods
        of the compressor filters.
        t   method(   Rf   t   METHOD_OUTPUT(   R&   R   (    (    se   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_compressor-1.4-py2.7.egg/compressor/base.pyt   filter_outputÓ   s    c         C€ s1   g  } x$ |  j  | ƒ D] } | j | ƒ q W| S(   uk   
        Passes each hunk (file or code) to the 'input' methods
        of the compressor filters.
        (   Rn   R?   (   R&   R_   R   t   hunk(    (    se   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_compressor-1.4-py2.7.egg/compressor/base.pyt   filter_inputÚ   s    c      
   K€ sb  | s t  | f S|  j j | ƒ } | j d d
 ƒ } | rX|  j j | ƒ }	 |	 d
 k rz | d k rUt d | ƒ ‚ qUqXt |	 ƒ \ }
 } y t |
 ƒ } WnH t	 k
 ræ t
 | d |  j d | d | d |	 ƒ} t | j |   f SXy t | | ƒ } Wn! t k
 rt d	 |	 ƒ ‚ qXX| | | d |  j d | d | ƒ} t | j |   f Sn  t  | f S(   u   
        Processes file using a pre compiler.

        This is the place where files like coffee script are processed.
        u   typeu   text/cssu   text/javascriptuQ   Couldn't find any precompiler in COMPRESS_PRECOMPILERS setting for mimetype '%s'.t   filter_typeRD   R   t   commandu   Could not find "%s".N(   u   text/cssu   text/javascript(   Ra   RS   Rb   Rc   R$   R"   R   R   R   t   ImportErrorR   R1   Rh   t   inputR/   R0   R   (   R&   R   RY   R[   RD   R   R(   t   attrst   mimetypet   filter_or_commandt   mod_namet   cls_namet   modRf   t   precompiler_class(    (    se   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_compressor-1.4-py2.7.egg/compressor/base.pyRe   ä   s8    
	c         K€ si   xb |  j  D]W } t | | d |  j ƒ| ƒ } y t | ƒ rL | |   } n  Wq
 t k
 r` q
 Xq
 W| S(   NRt   (   RV   R/   R1   t   callableR-   (   R&   R   Ro   R(   RU   t   filter_func(    (    se   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_compressor-1.4-py2.7.egg/compressor/base.pyRf     s    u   filec         C€ sW   d j  |  j | ƒ ƒ } | s" d St j s1 | rS |  j | ƒ } |  j | | | ƒ S| S(   uÆ   
        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.
        u   
u    (   RB   Rs   R
   R`   Rq   Rg   (   R&   R2   R_   t   outputt   filtered_output(    (    se   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_compressor-1.4-py2.7.egg/compressor/base.pyR     s    c         C€ sI   t  |  d | d  ƒ } t | ƒ r5 | | | | | ƒ St d | ƒ ‚ d  S(   Nu	   output_%su)   Couldn't find output method for mode '%s'(   R/   R$   R   R   (   R&   R2   R   R_   R;   t   output_func(    (    se   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_compressor-1.4-py2.7.egg/compressor/base.pyRg   (  s
    c         C€ s…   |  j  | d | ƒ} |  j j | ƒ s. | rV |  j j | t | j |  j ƒ ƒ ƒ n  t |  j j | ƒ ƒ } |  j	 | i | d 6ƒ S(   u†   
        The output method that saves the content to a file and renders
        the appropriate template with the file's URL.
        R;   u   url(
   RE   R,   RF   t   saveR   R]   R   R   R:   t   render_output(   R&   R2   R   R_   R;   t   new_filepathR:   (    (    se   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_compressor-1.4-py2.7.egg/compressor/base.pyt   output_file1  s
    (c         C€ s   |  j  | i | d 6ƒ S(   ua   
        The output method that directly returns the content for inline
        display.
        u   content(   R…   (   R&   R2   R   R_   R;   (    (    se   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_compressor-1.4-py2.7.egg/compressor/base.pyt   output_inline<  s    c      
   C€ s¦   d |  j  k r i  |  j  d <n  |  j  d j | p5 i  ƒ |  j  d j |  j ƒ t |  j  ƒ } t j d |  j d |  j d | d | ƒ |  j | ƒ } t	 | d | ƒS(   u~   
        Renders the compressor output with the appropriate template for
        the given mode and template context.
        u
   compressedt   senderR1   R2   R   t   context_instance(
   R   t   updateR   R   R   t   sendt	   __class__R1   R4   R   (   R&   R2   R   t   final_contextt   template_name(    (    se   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_compressor-1.4-py2.7.egg/compressor/base.pyR…   C  s    N(   t   __name__t
   __module__t   __doc__R$   R1   R)   R   R,   R.   R4   R<   RE   RI   RQ   RS   RV   R\   R^   Ra   Rn   Rq   Rs   Re   Rf   R   Rg   R‡   Rˆ   R…   (    (    (    se   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_compressor-1.4-py2.7.egg/compressor/base.pyR      s2   					%	
	'		(   u   inlineu   file(   u   inputu   output(.   t
   __future__R    R   R=   RJ   t   django.core.files.baseR   t   django.templateR   t   django.template.loaderR   t   django.utils.importlibR   t   django.utils.safestringR   t   urllib.requestR   Rv   t   urllibt   compressor.cacheR   R	   t   compressor.confR
   t   compressor.exceptionsR   R   R   t   compressor.filtersR   R+   R   t   compressor.signalsR   t   compressor.utilsR   R   R   t   compressor.utils.decoratorsR   t   SOURCE_HUNKRW   Rd   Rp   t   objectR   (    (    (    se   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_compressor-1.4-py2.7.egg/compressor/base.pyt   <module>   s,   