<s> from __future__ import unicode_literals <EOL> import os <EOL> import re <EOL> import mimetypes <EOL> from base64 import b64encode <EOL> from compressor . conf import settings <EOL> from compressor . filters import FilterBase <EOL> class DataUriFilter ( FilterBase ) : <EOL> """<STR_LIT>""" <EOL> def input ( self , filename = None , ** kwargs ) : <EOL> if not filename or not filename . startswith ( settings . COMPRESS_ROOT ) : <EOL> return self . content <EOL> output = self . content <EOL> for url_pattern in self . url_patterns : <EOL> output = url_pattern . sub ( self . data_uri_converter , output ) <EOL> return output <EOL> def get_file_path ( self , url ) : <EOL> if "<STR_LIT:?>" in url : <EOL> url = url . split ( "<STR_LIT:?>" ) [ <NUM_LIT:0> ] <EOL> if "<STR_LIT:#>" in url : <EOL> url = url . split ( "<STR_LIT:#>" ) [ <NUM_LIT:0> ] <EOL> return os . path . join ( <EOL> settings . COMPRESS_ROOT , url [ len ( settings . COMPRESS_URL ) : ] ) <EOL> def data_uri_converter ( self , matchobj ) : <EOL> url = matchobj . group ( <NUM_LIT:1> ) . strip ( '<STR_LIT>' ) <EOL> if not url . startswith ( '<STR_LIT>' ) and not url . startswith ( '<STR_LIT>' ) : <EOL> path = self . get_file_path ( url ) <EOL> if os . stat ( path ) . st_size <= settings . COMPRESS_DATA_URI_MAX_SIZE : <EOL> with open ( path , '<STR_LIT:rb>' ) as file : <EOL> data = b64encode ( file . read ( ) ) . decode ( '<STR_LIT:ascii>' ) <EOL> return '<STR_LIT>' % ( <EOL> mimetypes . guess_type ( path ) [ <NUM_LIT:0> ] , data ) <EOL> return '<STR_LIT>' % url <EOL> class CssDataUriFilter ( DataUriFilter ) : <EOL> """<STR_LIT>""" <EOL> url_patterns = ( <EOL> re . compile ( r'<STR_LIT>' ) , <EOL> ) </s>
