<s> import os <EOL> import re <EOL> from os . path import join , normpath <EOL> from webassets . filter import Filter <EOL> from webassets . utils import common_path_prefix <EOL> __all__ = ( ) <EOL> def addsep ( path ) : <EOL> """<STR_LIT>""" <EOL> if path and path [ - <NUM_LIT:1> ] != os . path . sep : <EOL> return path + os . path . sep <EOL> return path <EOL> def path2url ( path ) : <EOL> """<STR_LIT>""" <EOL> if os . name == '<STR_LIT>' : <EOL> return path . replace ( '<STR_LIT:\\>' , '<STR_LIT:/>' ) <EOL> return path <EOL> class PatternRewriter ( Filter ) : <EOL> """<STR_LIT>""" <EOL> patterns = { } <EOL> def input ( self , _in , out , ** kw ) : <EOL> content = _in . read ( ) <EOL> for func , pattern in self . patterns . items ( ) : <EOL> if not callable ( func ) : <EOL> func = getattr ( self , func ) <EOL> content = pattern . sub ( func , content ) <EOL> out . write ( content ) <EOL> urltag_re = re . compile ( r"""<STR_LIT>""" , re . VERBOSE ) <EOL> class CSSUrlRewriter ( PatternRewriter ) : <EOL> """<STR_LIT>""" <EOL> patterns = { <EOL> '<STR_LIT>' : urltag_re <EOL> } <EOL> def input ( self , _in , out , ** kw ) : <EOL> source , source_path , output , output_path = kw [ '<STR_LIT:source>' ] , kw [ '<STR_LIT>' ] , kw [ '<STR_LIT>' ] , kw [ '<STR_LIT>' ] <EOL> self . source_path = source_path <EOL> self . output_path = output_path <EOL> self . source_url = self . ctx . resolver . resolve_source_to_url ( <EOL> self . ctx , source_path , source ) <EOL> self . output_url = self . ctx . resolver . resolve_output_to_url ( <EOL> self . ctx , output ) <EOL> return super ( CSSUrlRewriter , self ) . input ( _in , out , ** kw ) <EOL> def rewrite_url ( self , m ) : <EOL> text_before = m . groups ( ) [ <NUM_LIT:0> ] <EOL> url = m . groups ( ) [ <NUM_LIT:1> ] <EOL> text_after = m . groups ( ) [ <NUM_LIT:2> ] <EOL> quotes_used = '<STR_LIT>' <EOL> if url [ : <NUM_LIT:1> ] in '<STR_LIT>' : <EOL> quotes_used = url [ : <NUM_LIT:1> ] <EOL> url = url [ <NUM_LIT:1> : ] <EOL> if url [ - <NUM_LIT:1> : ] in '<STR_LIT>' : <EOL> url = url [ : - <NUM_LIT:1> ] <EOL> url = self . replace_url ( url ) or url <EOL> result = '<STR_LIT>' % ( <EOL> text_before , quotes_used , url , quotes_used , text_after ) <EOL> return result <EOL> def replace_url ( self , url ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> for text , expect in [ <EOL> ( r'<STR_LIT>' , r'<STR_LIT>' ) , <EOL> ( r'<STR_LIT>' , r'<STR_LIT>' ) , <EOL> ( r'<STR_LIT>' , r'<STR_LIT>' ) , <EOL> ] : <EOL> m = urltag_re . search ( text ) <EOL> assert m . group ( ) == expect </s>
