<s> import base64 <EOL> import json <EOL> import re <EOL> import os <EOL> from django . conf import settings <EOL> from django . contrib . staticfiles . finders import get_finders <EOL> from django . contrib . staticfiles . templatetags . staticfiles import static as django_static <EOL> import sass <EOL> from compressor . filters . base import FilterBase <EOL> def static ( path ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' . format ( django_static ( path ) ) <EOL> OUTPUT_STYLE = getattr ( settings , '<STR_LIT>' , '<STR_LIT>' ) <EOL> SOURCE_COMMENTS = getattr ( settings , '<STR_LIT>' , settings . DEBUG ) <EOL> CUSTOM_FUNCTIONS = getattr ( settings , '<STR_LIT>' , { '<STR_LIT>' : static } ) <EOL> SOURCEMAPS = getattr ( settings , '<STR_LIT>' , False ) <EOL> INCLUDE_PATHS = None <EOL> def get_include_paths ( ) : <EOL> """<STR_LIT>""" <EOL> global INCLUDE_PATHS <EOL> if INCLUDE_PATHS is not None : <EOL> return INCLUDE_PATHS <EOL> include_paths = [ ] <EOL> for finder in get_finders ( ) : <EOL> try : <EOL> storages = finder . storages <EOL> except AttributeError : <EOL> continue <EOL> for storage in storages . values ( ) : <EOL> try : <EOL> include_paths . append ( storage . path ( '<STR_LIT:.>' ) ) <EOL> except NotImplementedError : <EOL> pass <EOL> INCLUDE_PATHS = include_paths <EOL> return include_paths <EOL> def prefix_sourcemap ( sourcemap , base_path ) : <EOL> decoded_sourcemap = json . loads ( sourcemap ) <EOL> source_urls = [ ] <EOL> include_paths = get_include_paths ( ) <EOL> for source_filename in decoded_sourcemap [ '<STR_LIT>' ] : <EOL> full_source_path = os . path . normpath ( os . path . join ( base_path , source_filename ) ) <EOL> for path in include_paths : <EOL> if full_source_path . startswith ( path ) : <EOL> remainder = full_source_path [ len ( path ) + <NUM_LIT:1> : ] <EOL> source_urls . append ( django_static ( remainder ) ) <EOL> break <EOL> else : <EOL> source_urls . append ( source_filename ) <EOL> decoded_sourcemap [ '<STR_LIT>' ] = source_urls <EOL> return json . dumps ( decoded_sourcemap ) <EOL> def embed_sourcemap ( output , sourcemap ) : <EOL> encoded_sourcemap = base64 . standard_b64encode ( <EOL> sourcemap . encode ( '<STR_LIT:utf-8>' ) <EOL> ) <EOL> sourcemap_fragment = '<STR_LIT>' . format ( encoded_sourcemap . decode ( '<STR_LIT:utf-8>' ) ) <EOL> url_re = re . compile ( r'<STR_LIT>' , re . M ) <EOL> output = url_re . sub ( sourcemap_fragment , output ) <EOL> return output <EOL> def compile ( ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> kwargs = kwargs . copy ( ) <EOL> kwargs [ '<STR_LIT>' ] = ( kwargs . get ( '<STR_LIT>' ) or [ ] ) + get_include_paths ( ) <EOL> custom_functions = CUSTOM_FUNCTIONS . copy ( ) <EOL> custom_functions . update ( kwargs . get ( '<STR_LIT>' , { } ) ) <EOL> kwargs [ '<STR_LIT>' ] = custom_functions <EOL> if SOURCEMAPS and kwargs . get ( '<STR_LIT:filename>' , None ) : <EOL> base_path = os . path . dirname ( kwargs [ '<STR_LIT:filename>' ] ) <EOL> sourcemap_filename = os . path . join ( base_path , '<STR_LIT>' ) <EOL> kwargs [ '<STR_LIT>' ] = sourcemap_filename <EOL> libsass_output , sourcemap = sass . compile ( ** kwargs ) <EOL> sourcemap = prefix_sourcemap ( sourcemap , base_path ) <EOL> output = embed_sourcemap ( libsass_output , sourcemap ) <EOL> else : <EOL> output = sass . compile ( ** kwargs ) <EOL> return output <EOL> class SassCompiler ( FilterBase ) : <EOL> def __init__ ( self , content , attrs = None , filter_type = None , charset = None , filename = None ) : <EOL> super ( SassCompiler , self ) . __init__ ( content = content , <EOL> filter_type = filter_type , <EOL> filename = filename ) <EOL> def input ( self , ** kwargs ) : <EOL> if self . filename : <EOL> return compile ( filename = self . filename , <EOL> output_style = OUTPUT_STYLE , <EOL> source_comments = SOURCE_COMMENTS ) <EOL> else : <EOL> return compile ( string = self . content , <EOL> output_style = OUTPUT_STYLE ) </s>
