<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , with_statement <EOL> import collections <EOL> import logging <EOL> import os <EOL> import os . path <EOL> from pkg_resources import resource_filename <EOL> from sass import CompileError <EOL> from . builder import Manifest <EOL> __all__ = '<STR_LIT>' , <EOL> class SassMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , app , manifests , package_dir = { } , <EOL> error_status = '<STR_LIT>' ) : <EOL> if not callable ( app ) : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' + repr ( app ) ) <EOL> self . app = app <EOL> self . manifests = Manifest . normalize_manifests ( manifests ) <EOL> if not isinstance ( package_dir , collections . Mapping ) : <EOL> raise TypeError ( '<STR_LIT>' + <EOL> repr ( package_dir ) ) <EOL> self . error_status = error_status <EOL> self . package_dir = dict ( package_dir ) <EOL> for package_name in self . manifests : <EOL> if package_name in self . package_dir : <EOL> continue <EOL> path = resource_filename ( package_name , '<STR_LIT>' ) <EOL> self . package_dir [ package_name ] = path <EOL> self . paths = [ ] <EOL> for package_name , manifest in self . manifests . items ( ) : <EOL> wsgi_path = manifest . wsgi_path <EOL> if not wsgi_path . startswith ( '<STR_LIT:/>' ) : <EOL> wsgi_path = '<STR_LIT:/>' + wsgi_path <EOL> if not wsgi_path . endswith ( '<STR_LIT:/>' ) : <EOL> wsgi_path += '<STR_LIT:/>' <EOL> package_dir = self . package_dir [ package_name ] <EOL> self . paths . append ( ( wsgi_path , package_dir , manifest ) ) <EOL> def __call__ ( self , environ , start_response ) : <EOL> path = environ . get ( '<STR_LIT>' , '<STR_LIT:/>' ) <EOL> if path . endswith ( '<STR_LIT>' ) : <EOL> for prefix , package_dir , manifest in self . paths : <EOL> if not path . startswith ( prefix ) : <EOL> continue <EOL> css_filename = path [ len ( prefix ) : ] <EOL> sass_filename = css_filename [ : - <NUM_LIT:4> ] <EOL> try : <EOL> result = manifest . build_one ( package_dir , <EOL> sass_filename , <EOL> source_map = True ) <EOL> except ( IOError , OSError ) : <EOL> break <EOL> except CompileError as e : <EOL> logger = logging . getLogger ( __name__ + '<STR_LIT>' ) <EOL> logger . error ( str ( e ) ) <EOL> start_response ( <EOL> self . error_status , <EOL> [ ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) ] <EOL> ) <EOL> return [ <EOL> b'<STR_LIT>' , str ( e ) . encode ( '<STR_LIT:utf-8>' ) , b'<STR_LIT>' , <EOL> b'<STR_LIT>' , <EOL> self . quote_css_string ( str ( e ) ) . encode ( '<STR_LIT:utf-8>' ) , <EOL> b'<STR_LIT>' , <EOL> b'<STR_LIT>' , <EOL> b'<STR_LIT>' <EOL> b'<STR_LIT>' <EOL> ] <EOL> def read_file ( path ) : <EOL> with open ( path , '<STR_LIT:rb>' ) as in_ : <EOL> while <NUM_LIT:1> : <EOL> chunk = in_ . read ( <NUM_LIT> ) <EOL> if chunk : <EOL> yield chunk <EOL> else : <EOL> break <EOL> start_response ( '<STR_LIT>' , [ ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) ] ) <EOL> return read_file ( os . path . join ( package_dir , result ) ) <EOL> return self . app ( environ , start_response ) <EOL> @ staticmethod <EOL> def quote_css_string ( s ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT:'>" + '<STR_LIT>' . join ( '<STR_LIT>' % ord ( c ) for c in s ) + "<STR_LIT:'>" </s>
