<s> """<STR_LIT>""" <EOL> import os , sys <EOL> from os import path <EOL> import glob , fnmatch <EOL> import inspect <EOL> import types <EOL> from webassets import six <EOL> try : <EOL> import yaml <EOL> except ImportError : <EOL> pass <EOL> from webassets import six <EOL> from webassets import Environment <EOL> from webassets . bundle import Bundle <EOL> from webassets . exceptions import EnvironmentError <EOL> from webassets . filter import register_filter <EOL> from webassets . importlib import import_module <EOL> __all__ = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , ) <EOL> class LoaderError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class YAMLLoader ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , file_or_filename ) : <EOL> try : <EOL> yaml <EOL> except NameError : <EOL> raise EnvironmentError ( '<STR_LIT>' ) <EOL> else : <EOL> self . yaml = yaml <EOL> self . file_or_filename = file_or_filename <EOL> def _yield_bundle_contents ( self , data ) : <EOL> """<STR_LIT>""" <EOL> contents = data . get ( '<STR_LIT>' , [ ] ) <EOL> if isinstance ( contents , six . string_types ) : <EOL> contents = contents , <EOL> for content in contents : <EOL> if isinstance ( content , dict ) : <EOL> content = self . _get_bundle ( content ) <EOL> yield content <EOL> def _get_bundle ( self , data ) : <EOL> """<STR_LIT>""" <EOL> kwargs = dict ( <EOL> filters = data . get ( '<STR_LIT>' , None ) , <EOL> output = data . get ( '<STR_LIT>' , None ) , <EOL> debug = data . get ( '<STR_LIT>' , None ) , <EOL> extra = data . get ( '<STR_LIT>' , { } ) , <EOL> config = data . get ( '<STR_LIT>' , { } ) , <EOL> depends = data . get ( '<STR_LIT>' , None ) ) <EOL> return Bundle ( * list ( self . _yield_bundle_contents ( data ) ) , ** kwargs ) <EOL> def _get_bundles ( self , obj , known_bundles = None ) : <EOL> """<STR_LIT>""" <EOL> bundles = { } <EOL> for key , data in six . iteritems ( obj ) : <EOL> if data is None : <EOL> data = { } <EOL> bundles [ key ] = self . _get_bundle ( data ) <EOL> for bundle_name , bundle in bundles . items ( ) : <EOL> contents = list ( bundle . contents ) <EOL> for i , item in enumerate ( bundle . contents ) : <EOL> if item in bundles : <EOL> contents [ i ] = bundles [ item ] <EOL> elif known_bundles and item in known_bundles : <EOL> contents [ i ] = known_bundles [ item ] <EOL> contents = tuple ( contents ) <EOL> if contents != bundle . contents : <EOL> bundle . contents = contents <EOL> return bundles <EOL> def _open ( self ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( self . file_or_filename , six . string_types ) : <EOL> return open ( self . file_or_filename ) , self . file_or_filename <EOL> file = self . file_or_filename <EOL> return file , getattr ( file , '<STR_LIT:name>' , False ) <EOL> @ classmethod <EOL> def _get_import_resolver ( cls ) : <EOL> """<STR_LIT>""" <EOL> from zope . dottedname . resolve import resolve as resolve_dotted <EOL> return resolve_dotted <EOL> def load_bundles ( self , environment = None ) : <EOL> """<STR_LIT>""" <EOL> f , _ = self . _open ( ) <EOL> try : <EOL> obj = self . yaml . load ( f ) or { } <EOL> return self . _get_bundles ( obj , environment ) <EOL> finally : <EOL> f . close ( ) <EOL> def load_environment ( self ) : <EOL> """<STR_LIT>""" <EOL> f , filename = self . _open ( ) <EOL> try : <EOL> obj = self . yaml . load ( f ) or { } <EOL> env = Environment ( ) <EOL> for setting in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:url>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if setting in obj : <EOL> setattr ( env , setting , obj [ setting ] ) <EOL> if filename and '<STR_LIT>' in env . config : <EOL> env . directory = path . normpath ( <EOL> path . join ( path . dirname ( filename ) , <EOL> env . config [ '<STR_LIT>' ] ) ) <EOL> if '<STR_LIT>' in obj : <EOL> try : <EOL> resolve_dotted = self . _get_import_resolver ( ) <EOL> except ImportError : <EOL> raise EnvironmentError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> for filter_class in obj [ '<STR_LIT>' ] : <EOL> try : <EOL> cls = resolve_dotted ( filter_class ) <EOL> except ImportError : <EOL> raise LoaderError ( "<STR_LIT>" % filter_class ) <EOL> if inspect . isclass ( cls ) : <EOL> register_filter ( cls ) <EOL> else : <EOL> raise LoaderError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if '<STR_LIT>' in obj : <EOL> env . config . update ( obj [ '<STR_LIT>' ] ) <EOL> bundles = self . _get_bundles ( obj . get ( '<STR_LIT>' , { } ) ) <EOL> for name , bundle in six . iteritems ( bundles ) : <EOL> env . register ( name , bundle ) <EOL> return env <EOL> finally : <EOL> f . close ( ) <EOL> class PythonLoader ( object ) : <EOL> """<STR_LIT>""" <EOL> environment = "<STR_LIT>" <EOL> def __init__ ( self , module_name ) : <EOL> if isinstance ( module_name , types . ModuleType ) : <EOL> self . module = module_name <EOL> else : <EOL> sys . path . insert ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> try : <EOL> try : <EOL> if "<STR_LIT::>" in module_name : <EOL> module_name , env = module_name . split ( "<STR_LIT::>" ) <EOL> self . environment = env <EOL> self . module = import_module ( module_name ) <EOL> except ImportError as e : <EOL> raise LoaderError ( e ) <EOL> finally : <EOL> sys . path . pop ( <NUM_LIT:0> ) <EOL> def load_bundles ( self ) : <EOL> """<STR_LIT>""" <EOL> bundles = { } <EOL> for name in dir ( self . module ) : <EOL> value = getattr ( self . module , name ) <EOL> if isinstance ( value , Bundle ) : <EOL> bundles [ name ] = value <EOL> return bundles <EOL> def load_environment ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return getattr ( self . module , self . environment ) <EOL> except AttributeError as e : <EOL> raise LoaderError ( e ) <EOL> def recursive_glob ( treeroot , pattern ) : <EOL> """<STR_LIT>""" <EOL> results = [ ] <EOL> for base , dirs , files in os . walk ( treeroot ) : <EOL> goodfiles = fnmatch . filter ( files , pattern ) <EOL> results . extend ( os . path . join ( base , f ) for f in goodfiles ) <EOL> return results <EOL> class GlobLoader ( object ) : <EOL> """<STR_LIT>""" <EOL> def glob_files ( self , f , recursive = False ) : <EOL> if isinstance ( f , tuple ) : <EOL> return iter ( recursive_glob ( f [ <NUM_LIT:0> ] , f [ <NUM_LIT:1> ] ) ) <EOL> else : <EOL> return iter ( glob . glob ( f ) ) <EOL> def with_file ( self , filename , then_run ) : <EOL> """<STR_LIT>""" <EOL> file = open ( filename , '<STR_LIT:rb>' ) <EOL> try : <EOL> contents = file . read ( ) <EOL> try : <EOL> return then_run ( filename , contents ) <EOL> except LoaderError : <EOL> pass <EOL> finally : <EOL> file . close ( ) </s>
