
    Zoc                     \    d Z ddl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Z G d d	      Zy)
z:mod:`sassutils.wsgi` --- WSGI middleware for development purpose
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    N)resource_filename   )Manifest)CompileError)SassMiddlewarec                   2    e Zd ZdZi dfdZd Zed        Zy)r   a\	  WSGI middleware for development purpose.  Every time a CSS file has
    requested it finds a matched Sass/SCSS source file and then compiled
    it into CSS.

    It shows syntax errors in three ways:

    Heading comment
        The result CSS includes detailed error message in the heading
        CSS comment e.g.:

        .. code-block:: css

            /*
            Error: invalid property name
            */

    Red text in ``body:before``
        The result CSS draws detailed error message in ``:before``
        pseudo-class of ``body`` element e.g.:

        .. code-block:: css

            body:before {
                content: 'Error: invalid property name';
                color: maroon;
                background-color: white;
            }

        In most cases you could be aware of syntax error by refreshing your
        working document because it will removes all other styles and leaves
        only a red text.

    :mod:`logging`
        It logs syntax errors if exist during compilation to
        ``sassutils.wsgi.SassMiddleware`` logger with level ``ERROR``.

        To enable this::

            from logging import Formatter, StreamHandler, getLogger
            logger = getLogger('sassutils.wsgi.SassMiddleware')
            handler = StreamHandler(level=logging.ERROR)
            formatter = Formatter(fmt='*' * 80 + '\n%(message)s\n' + '*' * 80)
            handler.setFormatter(formatter)
            logger.addHandler(handler)

        Or simply::

            import logging
            logging.basicConfig()

    :param app: the WSGI application to wrap
    :type app: :class:`collections.abc.Callable`
    :param manifests: build settings.  the same format to
                      :file:`setup.py` script's ``sass_manifests``
                      option
    :type manifests: :class:`collections.abc.Mapping`
    :param package_dir: optional mapping of package names to directories.
                        the same format to :file:`setup.py` script's
                        ``package_dir`` option
    :type package_dir: :class:`collections.abc.Mapping`

    .. versionchanged:: 0.4.0
       It creates also source map files with filenames followed by
       :file:`.map` suffix.

    .. versionadded:: 0.8.0
       It logs syntax errors if exist during compilation to
       ``sassutils.wsgi.SassMiddleware`` logger with level ``ERROR``.

    200 OKc                    t        |      st        dt        |      z         || _        t	        j
                  |      | _        t        |t        j                  j                        st        dt        |      z         || _        t        |      | _        | j                  D ],  }|| j                  v rt        |d      }|| j                  |<   . g | _        | j                  j!                         D ]j  \  }}|j"                  }|j%                  d      sd|z   }|j'                  d      s|dz  }| j                  |   }| j                  j)                  |||f       l y )Nz2app must be a WSGI-compliant callable object, not z*package_dir must be a mapping object, not  /)callable	TypeErrorreprappr   normalize_manifests	manifests
isinstancecollectionsabcMappingerror_statusdictpackage_dirr   pathsitems	wsgi_path
startswithendswithappend)	selfr   r   r   r   package_namepathmanifestr   s	            0/usr/lib/python3/dist-packages/sassutils/wsgi.py__init__zSassMiddleware.__init__Y   s[    }c#  !55i@+{'>'>?<[!"  ), NN 	2Lt///$\26D-1D\*		2
 
&*nn&:&:&< 	B"L( **I'',)O	%%c*S 	**<8KJJy+x@A	B    c                    |j                  dd      }|j                  d      r| j                  D ]  \  }}}|j                  |      s|t	        |      d  }|j                  ||      }	 |j                  ||d      }	d } |ddg        |t"        j$                  j'                  ||	            c S  | j)                  ||      S # t        $ r Y  t        $ r}
t        j                  t        dz         }|j                  t        |
              || j                  dg       dt        |
      j                  d	      d
d| j!                  t        |
            j                  d	      dddgcY d }
~
c S d }
~
ww xY w)N	PATH_INFOr   z.cssT)
source_mapz.SassMiddleware)Content-Typeztext/css; charset=utf-8s   /*
zutf-8s   
*/

s   body:before { content: s(   ; color: maroon; background-color: whites'   ; white-space: pre-wrap; display: blocks=   ; font-family: "Courier New", monospace; user-select: text; }c              3      K   t        | d      5 }	 |j                  d      }|r| nn	 d d d        y # 1 sw Y   y xY ww)Nrbi   )openread)r"   in_chunks      r$   	read_filez*SassMiddleware.__call__.<locals>.read_file   sK     dD) &S$'HHTNE$&+ %  
 !&& & &s   A5	A>Ar	   )r*   ztext/css)getr   r   r   lenunresolve_filename	build_oneOSErrorr   logging	getLogger__name__errorstrr   encodequote_css_stringosr"   joinr   )r    environstart_responser"   prefixr   r#   css_filenamesass_filenameresulteloggerr1   s                r$   __call__zSassMiddleware.__call__z   sz   {{;,== 15 )D-Xv.#CKL1 ( ; ;!%//#%#' 0 F0& x*F)GH k6!BCCS)DT xx00;  # $..x:K/KLFLLQ("))DE
  Qw!72--c!f5<<WECB2 s%   *C	E9E9BE4,E94E9c                 >    ddj                  d | D              z   dz   S )z&Quotes a string as CSS string literal.'r   c              3   8   K   | ]  }d t        |      z    yw)z\%06xN)ord).0cs     r$   	<genexpr>z2SassMiddleware.quote_css_string.<locals>.<genexpr>   s     :1XA.:s   )r?   )ss    r$   r=   zSassMiddleware.quote_css_string   s#     RWW::::S@@r&   N)r9   
__module____qualname____doc__r%   rH   staticmethodr=    r&   r$   r   r      s4    EP +-BB-1^ A Ar&   r   )rS   collections.abcr   r7   os.pathr>   pkg_resourcesr   builderr   sassr   __all__r   rU   r&   r$   <module>r\      s1       +  
[A [Ar&   