
    D6i                     d    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 d dlmZ  G d d      Zy	)
    )tee)compile)current_apprequest)MemoryCache)Parser)does_content_type_matchc                   f    e Zd ZdZdddddg g dddg i dfdZd Zed        Zd	 Zd
 Z	d Z
d Zd Zy)MinifyzFExtension to minify flask response for html, javascript, css and less.NT   Fc                 d   || _         || _        || _        || _        || _        || _        || _        || _        |	| _        |
| _	        || _
        t        | j                  |      | _        t        |||      | _        | j                  j!                  ||||       |xr | j#                  |       y y)a  Extension to minify flask response for html, javascript, css and less.

        Parameters
        ----------
        app: Flask.app
            Flask app instance to be passed.
        html: bool
            to minify HTML.
        js: bool
            to minify JavaScript output.
        cssless: bool
            to minify CSS or Less.
        fail_safe: bool
            to avoid raising error while minifying.
        bypass: list
            list of endpoints to bypass minifying for. (Regex)
        bypass_caching: list
            list of endpoints to bypass caching for. (Regex)
        caching_limit: int
            to limit the number of minified response variations.
        passive: bool
            to disable active minifying.
        static: bool
            to enable minifying static files css, less and js.
        script_types: list
            list of script types to limit js minification to.
        parsers: dict
            parsers to handle minifying specific tags.
        go: bool
            use optimized golang minifier if available.

        Notes
        -----
        if `caching_limit` is set to 0, we'll not cache any endpoint responses,
        so if you want to disable caching just do that.

        `endpoint` is the name of the function decorated with the
        `@app.route()` so in the following example the endpoint will be `root`:
            @app.route('/root/<id>')
            def root(id):
                return id

        when using a `Blueprint` the decorated endpoint will be suffixed with
        the blueprint name; `Blueprint('blueprint_name')` so here the endpoint
        will be `blueprint_name.root`.

        `bypass` and `bypass_caching` can handle regex patterns so if you want
        to bypass all routes on a certain blueprint you can just pass
        the pattern as such:
            minify(app, bypass=['blueprint_name.*'])

        when using `script_types` include '' (empty string) in the list to
        include script blocks which are missing the type attribute.
        )goN)htmljsscript_typescssless	fail_safebypassbypass_caching_apppassivestaticr   r   get_endpointcacher   parserupdate_runtime_optionsinit_app)selfappr   r   r   r   r   r   caching_limitr   r   r   parsersr   s                 T/home/azureuser/techstart-app/venv/lib/python3.12/site-packages/flask_minify/main.py__init__zMinify.__init__   s    L 	(",	 !2!2MB
WiB7**4WlK"c""    c                     | j                   j                         5  t        t        dd      xs d}|dk(  rt        t        dd      xs d}|cddd       S # 1 sw Y   yxY w)zGet the current response endpoint, with a failsafe.

        Returns
        -------
        str
            the current endpoint.
        endpoint r   pathN)r   app_contextgetattrr   )r   r(   s     r"   r   zMinify.get_endpointe   sZ     XX!!# 	7J39rDxw39r	 	 	s   1AAc                 *    | j                   xs t        S )zIf app was passed take it, otherwise fallback to `Flask.current_app`.

        Returns
        -------
        Flask App
            The current Flask application.
        )r   r   )r   s    r"   r   z
Minify.appu   s     yy'K'r$   c                 ~    || _         |j                  | j                         |j                  | j                         y)z6Handle initiation of multiple apps NOTE:Factory MethodN)r   after_requestmainteardown_appcontextteardown)r   r   s     r"   r   zMinify.init_app   s-    	$))$.r$   c                      y)z7Nothing todo on app context teardown XXX:Factory MethodN )r   	exceptions     r"   r0   zMinify.teardown   s    r$   c                       j                   j                        \  }} fd}|r |       S  j                  j                  |      S )aP  Check if the content is already cached and restore or store it.

        Parameters
        ----------
        content: str
            a script or style html tag content.
        tag: bool
            html tag the content belongs to.

        Returns
        -------
        str
            stored or restored minifed content.
        c                  <    j                   j                         S N)r   minify)contentr   tags   r"   <lambda>z/Minify.get_minified_or_cached.<locals>.<lambda>   s    t{{11'3? r$   )get_endpoint_matchesr   r   
get_or_set)r   r8   r9   _bypassedget_minifieds   ```   r"   get_minified_or_cachedzMinify.get_minified_or_cached   sF     //0C0CD8?>!zz$$Wl;;r$   c                     | j                         t        fdt        t        |      D              \  }}t	        |d      dk7  }||fS )aA  Get the patterns that matches the current endpoint.

        Parameters
        ----------
        patterns: list
            regex patterns or strings to match endpoint.

        Returns
        -------
        (iterable, bool)
            patterns that match the current endpoint, and True if any matches found
        c              3   F   K   | ]  }|j                        s|  y wr6   )search).0pr&   s     r"   	<genexpr>z.Minify.get_endpoint_matches.<locals>.<genexpr>   s!      "
AHHX4FA"
s   !!r   )r   r   map
compile_renext)r   patternsmatches
duplicateshas_matchesr&   s        @r"   r;   zMinify.get_endpoint_matches   sT     $$&! "
:x0"
 
 :q)Q.##r$   c                    | j                  | j                        \  }}|xs | j                  }t        |      \  }}}|xr | j                  xs" |xr | j
                  xs |xr | j                  }|rZ|sX|s| j                  rJ|s|rFd|_        |j                  d      }	|rdn|rdnd}
| j                  |	|
      }|j                  |       |S )a  Where a dragon once lived!

        Parameters
        ----------
        response: Flask.response
            instance form the `after_request` handler.

        Returns
        -------
        Flask.Response
            minified flask response if it fits the requirements.
        FT)as_textr   scriptstyle)r;   r   r   r	   r   r   r   r   direct_passthroughget_datar@   set_data)r   responser=   r>   should_bypassr   r   r   should_minifyr8   r9   minifieds               r"   r.   zMinify.main   s     //<8 0DLL3H=grdiiRW%=R2>$'' 	 B.3+"++D+9 $fb(g66wD!!(+r$   )__name__
__module____qualname____doc__r#   r   propertyr   r   r0   r@   r;   r.   r2   r$   r"   r   r      sg    L U#n  ( (/<.$*r$   r   N)	itertoolsr   rer   rH   flaskr   r   flask_minify.cacher   flask_minify.parsersr   flask_minify.utilsr	   r   r2   r$   r"   <module>rd      s#     $ & * ' 6H Hr$   