
    ~+h*                        d Z ddlZddlZddlmZ ddlZddlZddlZddl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 ddlmZ dd	lmZ d
Z G d de      Z G d de      Z G d de      Zd Z G d de      Zy# e$ r Y ]w xY w)zLoaders are helper classes which will read environments and/or
bundles from a source, like a configuration file.

This can be used as an alternative to an imperative setup.
    N)path)six)Environment)Bundle)EnvironmentError)register_filter)import_module)LoaderLoaderErrorPythonLoader
YAMLLoader
GlobLoaderc                       e Zd ZdZy)r   zFLoaders should raise this when they can't deal with a given file.
    N)__name__
__module____qualname____doc__     D/var/www/html/venv/lib/python3.12/site-packages/webassets/loaders.pyr   r      s    r   r   c                   N    e Zd ZdZd Zd Zd ZddZd Ze	d        Z
dd	Zd
 Zy)r   zmWill load an environment or a set of bundles from
    `YAML <http://en.wikipedia.org/wiki/YAML>`_ files.
    c                 f    	 t          t         | _         || _        y # t        $ r t        d      w xY w)NzPyYAML is not installed)yaml	NameErrorr   file_or_filename)selfr   s     r   __init__zYAMLLoader.__init__)   s8    	 DI 0	  	>"#<==	>s    0c              #      K   |j                  dg       }t        |t        j                        r|f}|D ]'  }t        |t              r| j                  |      }| ) yw)zYield bundle contents from the given dict.

        Each item yielded will be either a string representing a file path
        or a bundle.contentsN)get
isinstancer   string_typesdict_get_bundle)r   datar   contents       r   _yield_bundle_contentsz!YAMLLoader._yield_bundle_contents2   s]     
 88J+h 0 01yH 	G'4(**73M	s   AAc                 *   t        |j                  dd      |j                  dd      |j                  dd      |j                  di       |j                  di       |j                  dd            }t        t        | j	                  |            i |S )	z.Return a bundle initialised by the given dict.filtersNoutputdebugextraconfigdepends)r)   r*   r+   r,   r-   r.   )r#   r    r   listr'   )r   r%   kwargss      r   r$   zYAMLLoader._get_bundle?   s    HHY-88Hd+((7D)((7B'88Hb)HHY-/ tD77=>I&IIr   Nc                    i }t        j                  |      D ]  \  }}|i }| j                  |      ||<    |j                         D ]v  \  }}t	        |j
                        }t        |j
                        D ]"  \  }	}
|
|v r	||
   ||	<   |s|
|v s||
   ||	<   $ t        |      }||j
                  k7  sp||_        x |S )z0Return a dict that keys bundle names to bundles.)r   	iteritemsr$   itemsr/   r   	enumeratetuple)r   objknown_bundlesbundleskeyr%   bundle_namebundler   iitems              r   _get_bundleszYAMLLoader._get_bundlesJ   s    s+ 	2IC|++D1GCL	2 $+==? 	+KFOO,H$V__5 647?")$-HQK"t}'<"/"5HQK	6 XH6??*"*	+ r   c                     t        | j                  t        j                        r!t	        | j                        | j                  fS | j                  }|t        |dd      fS )zbReturns a (fileobj, filename) tuple.

        The filename can be False if it is unknown.
        nameF)r!   r   r   r"   opengetattr)r   files     r   _openzYAMLLoader._openb   sU    
 d++S-=-=>--.0E0EEE$$WT65111r   c                     ddl m} |S )z( method that can be overridden in tests r   )resolve)zope.dottedname.resolverF   )clsresolve_dotteds     r   _get_import_resolverzYAMLLoader._get_import_resolverm   s     	Fr   c                     | j                         \  }}	 | j                  j                  |      xs i }| j                  ||      |j	                          S # |j	                          w xY w)a  Load a list of :class:`Bundle` instances defined in the YAML file.

        Expects the following format:

        .. code-block:: yaml

            bundle-name:
                filters: sass,cssutils
                output: cache/default.css
                contents:
                    - css/jquery.ui.calendar.css
                    - css/jquery.ui.slider.css
            another-bundle:
                # ...

        Bundles may reference each other:

        .. code-block:: yaml

            js-all:
                contents:
                    - jquery.js
                    - jquery-ui    # This is a bundle reference
            jquery-ui:
                contents: jqueryui/*.js

        If an ``environment`` argument is given, it's bundles
        may be referenced as well. Note that you may pass any
        compatibly dict-like object.

        Finally, you may also use nesting:

        .. code-block:: yaml

            js-all:
                contents:
                    - jquery.js
                    # This is a nested bundle
                    - contents: "*.coffee"
                      filters: coffeescript

        )rD   r   	safe_loadr>   close)r   environmentf_r6   s        r   load_bundleszYAMLLoader.load_bundless   sU    Z zz|1	))%%a(.BC$$S+6GGIAGGIs   0A A(c                    | j                         \  }}	 | j                  j                  |      xs i }t               }dD ]  }||v st	        ||||           |r\d|j
                  v rNt        j                  t        j                  t        j                  |      |j
                  d               |_
        d|v rO	 | j                         }|d   D ]6  }	  ||      }t        j                   |      rt#        |       -t        d       d|v r|j
                  j%                  |d          | j'                  |j)                  di             }	t+        j,                  |	      D ]  \  }
}|j/                  |
|        ||j1                          S # t        $ r t        d      w xY w# t        $ r t        d|z        w xY w# |j1                          w xY w)	aW  Load an :class:`Environment` instance defined in the YAML file.

        Expects the following format:

        .. code-block:: yaml

            directory: ../static
            url: /media
            debug: True
            updater: timestamp
            filters:
                - my_custom_package.my_filter
            config:
                compass_bin: /opt/compass
                another_custom_config_value: foo

            bundles:
                # ...

        All values, including ``directory`` and ``url`` are optional. The
        syntax for defining bundles is the same as for
        :meth:`~.YAMLLoader.load_bundles`.

        Sample usage::

            from webassets.loaders import YAMLLoader
            loader = YAMLLoader('asset.yml')
            env = loader.load_environment()

            env['some-bundle'].urls()
        )r+   cacheversions
url_expire
auto_buildurl	directorymanifest	load_pathcache_file_modeexpireupdaterrX   r)   z]In order to use custom filters in the YAMLLoader you must install the zope.dottedname packagezUnable to resolve class %sz7Custom filters must be classes not modules or functionsr-   r8   )rD   r   rL   r   setattrr-   r   normpathjoindirnamerX   rJ   ImportErrorr   r   inspectisclassr   updater>   r    r   r2   registerrM   )r   rO   filenamer6   envsettingrI   filter_classrH   r8   r@   r;   s               r   load_environmentzYAMLLoader.load_environment   s   @ jjl84	))%%a(.BC-C1 8
 c>C#g,78 K3::5 $IIdll84!jj57!8 CH%)%>%>%@N
 %(	N 	8LW,\: s+',) +7 8 8	8 3

!!#h-0 ''	2(>?G #g 6 +fT6*+ GGI5 # H*GH HH ' W)*F*UVVW& GGIsC   2G A4G =F G F'B G F$$G 'F??G GN)r   r   r   r   r   r'   r$   r>   rD   classmethodrJ   rQ   rk   r   r   r   r   r   $   sA    1	J0	2  
2hUr   r   c                   &    e Zd ZdZdZd Zd Zd Zy)r   zgBasically just a simple helper to import a Python file and
    retrieve the bundles defined there.
    rN   c                    t        |t        j                        r|| _        y t        j
                  j                  dd       	 	 d|v r|j                  d      \  }}|| _        t        |      | _        	 t        j
                  j                  d       y # t        $ r}t        |      d }~ww xY w# t        j
                  j                  d       w xY w)Nr    :)r!   types
ModuleTypemodulesysr   insertsplitrN   r	   rb   r   pop)r   module_namerh   es       r   r   zPythonLoader.__init__  s    k5#3#34%DKHHOOAr"	 )k)+6+<+<S+A(S+.("/"<DK Q # )%a.() Qs$   /B 	B.B))B..B1 1!Cc                     i }t        | j                        D ].  }t        | j                  |      }t        |t              s*|||<   0 |S )zuLoad ``Bundle`` objects defined in the Python module.

        Collects all bundles in the global namespace.
        )dirrt   rB   r!   r   )r   r8   r@   values       r   rQ   zPythonLoader.load_bundles  sK    
 $ 	&DDKK.E%( %	& r   c                 |    	 t        | j                  | j                        S # t        $ r}t	        |      d}~ww xY w)zLoad an ``Environment`` defined in the Python module.

        Expects as default a global name ``environment`` to be defined,
        or overridden by passing a string ``module:environment`` to the
        constructor.
        N)rB   rt   rN   AttributeErrorr   )r   rz   s     r   rk   zPythonLoader.load_environment"  s8    	!4;;(8(899 	!a. 	!s   " 	;6;N)r   r   r   r   rN   r   rQ   rk   r   r   r   r   r      s      K  

!r   r   c                     g }t        j                  |       D ]6  \  }}t        j                  ||      }|j	                  fd|D               8 |S )zN
    From:
    http://stackoverflow.com/questions/2186525/2186639#2186639
    c              3   ^   K   | ]$  }t         j                  j                  |       & y wrl   )osr   r`   ).0rO   bases     r   	<genexpr>z!recursive_glob.<locals>.<genexpr>7  s      @rww||D!,@s   *-)r   walkfnmatchfilterextend)treerootpatternresultsdirsfiles	goodfilesr   s         @r   recursive_globr   /  sT    
 GWWX. AdENN5'2	@i@@A Nr   c                       e Zd ZdZddZd Zy)r   zQBase class with some helpers for loaders which need to search
    for files.
    c                     t        |t              rt        t        |d   |d               S t        t	        j                  |            S )Nr      )r!   r5   iterr   glob)r   rO   	recursives      r   
glob_fileszGlobLoader.glob_files@  s9    aqtQqT233		!%%r   c                     t        |d      }	 |j                         }	  |||      |j                          S # t        $ r Y nw xY w	 |j                          y# |j                          w xY w)z2Call ``then_run`` with the file contents.
        rbN)rA   readrM   r   )r   rg   then_runrC   r   s        r   	with_filezGlobLoader.with_fileF  sd     Hd#	yy{H(3
 JJL	  JJLDJJLs&   A 8 	AA AA A+N)F)r   r   r   r   r   r   r   r   r   r   r   ;  s    &r   r   )r   r   ru   r   r   r   rc   rr   	webassetsr   r   rb   r   webassets.bundler   webassets.exceptionsr   webassets.filterr   webassets.importlibr	   __all__	Exceptionr   objectr   r   r   r   r   r   r   <module>r      s         	  ! # 1 , -
) 
X Xv-!6 -!`	 Y	  		s   B B
	B
