<s> import mimetypes <EOL> import os <EOL> from StringIO import StringIO <EOL> from functools import partial <EOL> from flask import send_file , current_app , url_for , request <EOL> from jinja2 import Markup <EOL> from gears . assets import build_asset <EOL> from gears . compat import bytes <EOL> from gears . environment import Environment , DEFAULT_PUBLIC_ASSETS <EOL> from gears . exceptions import FileNotFound <EOL> from gears . finders import FileSystemFinder <EOL> class Gears ( object ) : <EOL> css_template = '<STR_LIT>' <EOL> js_template = '<STR_LIT>' <EOL> def __init__ ( self , app = None , defaults = True , assets_folder = '<STR_LIT>' , <EOL> compilers = None , compressors = None , public_assets = None , <EOL> extra_public_assets = None , cache = None , gzip = False ) : <EOL> self . defaults = defaults <EOL> self . assets_folder = assets_folder <EOL> self . compilers = compilers <EOL> self . compressors = compressors <EOL> self . public_assets = public_assets <EOL> self . extra_public_assets = extra_public_assets <EOL> self . cache = None <EOL> self . gzip = gzip <EOL> if app is not None : <EOL> self . init_app ( app ) <EOL> def init_app ( self , app ) : <EOL> app . extensions [ '<STR_LIT>' ] = { } <EOL> self . init_environment ( app ) <EOL> self . init_asset_view ( app ) <EOL> @ app . context_processor <EOL> def gears_processor ( ) : <EOL> return dict ( <EOL> css_tag = partial ( self . html_tag , self . css_template ) , <EOL> js_tag = partial ( self . html_tag , self . js_template ) , <EOL> ) <EOL> def init_environment ( self , app ) : <EOL> environment = Environment ( <EOL> root = self . get_static_folder ( app ) , <EOL> public_assets = self . get_public_assets ( app ) , <EOL> cache = self . get_cache ( app ) , <EOL> gzip = self . gzip , <EOL> ) <EOL> if self . defaults : <EOL> environment . register_defaults ( ) <EOL> environment . finders . register ( self . get_default_finder ( app ) ) <EOL> if self . compilers is not None : <EOL> for extension , compiler in self . compilers . items ( ) : <EOL> environment . compilers . register ( extension , compiler ) <EOL> if self . compressors is not None : <EOL> for mimetype , compressor in self . compressors . items ( ) : <EOL> environment . compressors . register ( mimetype , compressor ) <EOL> app . extensions [ '<STR_LIT>' ] [ '<STR_LIT>' ] = environment <EOL> def init_asset_view ( self , app ) : <EOL> app . extensions [ '<STR_LIT>' ] [ '<STR_LIT>' ] = app . view_functions [ '<STR_LIT>' ] <EOL> app . view_functions [ '<STR_LIT>' ] = self . asset_view <EOL> def asset_view ( self , filename ) : <EOL> environment = current_app . extensions [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> static_view = current_app . extensions [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> try : <EOL> asset = build_asset ( environment , filename ) <EOL> except FileNotFound : <EOL> return static_view ( filename ) <EOL> if request . args . get ( '<STR_LIT:body>' ) : <EOL> asset = asset . processed_source . encode ( '<STR_LIT:utf-8>' ) <EOL> mimetype , encoding = mimetypes . guess_type ( filename ) <EOL> source = bytes ( asset ) <EOL> return send_file ( StringIO ( source ) , mimetype = mimetype , conditional = True ) <EOL> def html_tag ( self , template , logical_path , debug = False ) : <EOL> environment = self . get_environment ( current_app ) <EOL> if debug or self . debug ( current_app ) : <EOL> asset = build_asset ( environment , logical_path ) <EOL> urls = [ ] <EOL> for requirement in asset . requirements : <EOL> logical_path = requirement . attributes . logical_path <EOL> url = url_for ( '<STR_LIT>' , filename = logical_path , body = <NUM_LIT:1> ) <EOL> urls . append ( url ) <EOL> else : <EOL> if logical_path in environment . manifest . files : <EOL> logical_path = environment . manifest . files [ logical_path ] <EOL> urls = ( url_for ( '<STR_LIT>' , filename = logical_path ) , ) <EOL> return Markup ( '<STR_LIT:\n>' . join ( template . format ( url = url ) for url in urls ) ) <EOL> def get_environment ( self , app ) : <EOL> return app . extensions [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> def get_default_finder ( self , app ) : <EOL> return FileSystemFinder ( directories = ( self . get_assets_folder ( app ) , ) ) <EOL> def get_static_folder ( self , app ) : <EOL> return app . config . get ( '<STR_LIT>' , app . static_folder ) <EOL> def get_assets_folder ( self , app ) : <EOL> return os . path . join ( app . root_path , self . assets_folder ) <EOL> def get_public_assets ( self , app ) : <EOL> if self . public_assets is not None : <EOL> public_assets = tuple ( self . public_assets ) <EOL> else : <EOL> public_assets = DEFAULT_PUBLIC_ASSETS <EOL> if self . extra_public_assets is not None : <EOL> public_assets += tuple ( self . extra_public_assets ) <EOL> return public_assets <EOL> def get_cache ( self , app ) : <EOL> return self . cache <EOL> def debug ( self , app ) : <EOL> return app . config . get ( '<STR_LIT>' , app . debug ) </s>
