<s> from os import path <EOL> from glob import glob <EOL> from functools import partial <EOL> from collections import defaultdict <EOL> from flask . ext . assets import Environment , Bundle <EOL> from webassets . filter import register_filter , get_filter <EOL> from config . cdn import proxied <EOL> from utils . flaskutils import RegisterJst <EOL> from utils . ext . bunch import Bunch <EOL> from utils . ext . path import Path <EOL> from . app import app <EOL> Coffee = partial ( Bundle , filters = '<STR_LIT>' , debug = False ) <EOL> SCSS = partial ( Bundle , filters = '<STR_LIT>' , debug = False ) <EOL> JS = partial ( Bundle , filters = '<STR_LIT>' ) <EOL> CSS = partial ( Bundle , filters = '<STR_LIT>' ) <EOL> assets = Environment ( app ) <EOL> assets . url = proxied ( '<STR_LIT>' ) <EOL> class Special ( str ) : pass <EOL> class Depends ( Special ) : pass <EOL> asset_spec = { <EOL> "<STR_LIT>" : ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , ) , <EOL> "<STR_LIT>" : ( '<STR_LIT>' , ) , <EOL> } <EOL> def get_rules ( kind ) : <EOL> if kind == '<STR_LIT>' : <EOL> return Bunch ( <EOL> final_filter = [ "<STR_LIT>" , get_filter ( '<STR_LIT>' , replace = proxied ) ] , <EOL> compilers = { "<STR_LIT>" : "<STR_LIT>" } , <EOL> kwargs = defaultdict ( dict ) <EOL> ) <EOL> elif kind == '<STR_LIT>' : <EOL> return Bunch ( final_filter = "<STR_LIT>" , compilers = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } , <EOL> kwargs = defaultdict ( dict ) ) <EOL> raise RuntimeError ( "<STR_LIT>" % ( kind , ) ) <EOL> def yield_expanded ( iterable ) : <EOL> already_yielded = set ( ) <EOL> base = Path ( app . static_folder ) <EOL> for element in iterable : <EOL> if isinstance ( element , Special ) : <EOL> yield element <EOL> continue <EOL> for atom in sorted ( base . relpathto ( expanded ) for expanded in glob ( base / element ) ) : <EOL> if atom in already_yielded : <EOL> continue <EOL> already_yielded . add ( atom ) <EOL> yield atom <EOL> def register_assets ( spec ) : <EOL> for name , elements in spec . iteritems ( ) : <EOL> name , kind = path . splitext ( name ) <EOL> rules = get_rules ( kind ) <EOL> filemap = defaultdict ( list ) <EOL> for element in yield_expanded ( elements ) : <EOL> _ , extension = path . splitext ( element ) <EOL> if isinstance ( element , Depends ) : <EOL> rules . kwargs [ extension ] [ '<STR_LIT>' ] = element <EOL> continue <EOL> filemap [ extension ] . append ( element ) <EOL> contents = list ( filemap . get ( kind , [ ] ) ) <EOL> for compiled_extension , compiled_filters in rules [ '<STR_LIT>' ] . iteritems ( ) : <EOL> if compiled_extension in filemap : <EOL> kwargs = rules . kwargs . get ( compiled_extension , { } ) <EOL> contents . append ( Bundle ( * filemap [ compiled_extension ] , filters = compiled_filters , debug = False , <EOL> output = "<STR_LIT>" % ( name , compiled_extension , kind ) , ** kwargs ) ) <EOL> assets . register ( <EOL> name + kind , <EOL> Bundle ( * contents , filters = rules . final_filter , output = "<STR_LIT>" % ( name , kind ) , <EOL> ** rules . kwargs [ kind ] ) <EOL> ) <EOL> register_filter ( RegisterJst ) <EOL> register_assets ( asset_spec ) </s>
