<s> from putils . patterns import Singleton <EOL> from putils . filesystem import Dir <EOL> import mimetypes <EOL> import scss <EOL> from scss import Scss <EOL> from jsmin import jsmin <EOL> import os <EOL> import shutil <EOL> class StaticCompiler ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , path ) : <EOL> self . css_parser = Scss ( ) <EOL> scss . LOAD_PATHS = path <EOL> def compile_file ( self , filepath , need_compilation = True ) : <EOL> result = self . get_content ( filepath ) <EOL> if need_compilation : <EOL> mimetype = mimetypes . guess_type ( filepath ) [ <NUM_LIT:0> ] <EOL> result = self . compile_text ( result , mimetype ) <EOL> return result <EOL> def compile_text ( self , text , mimetype ) : <EOL> result = "<STR_LIT>" <EOL> if mimetype == "<STR_LIT>" : <EOL> result = self . css_parser . compile ( text ) <EOL> elif mimetype == "<STR_LIT>" : <EOL> result = jsmin ( text ) <EOL> else : <EOL> result = text <EOL> return result <EOL> def get_content ( self , file ) : <EOL> return open ( file ) . read ( ) <EOL> class StaticBuilder ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , path , static_not_compile ) : <EOL> self . path = path <EOL> self . static_not_compile = static_not_compile <EOL> self . compiler = StaticCompiler ( self . path ) <EOL> def build ( self ) : <EOL> try : <EOL> shutil . rmtree ( self . path + "<STR_LIT>" ) <EOL> except : <EOL> pass <EOL> try : <EOL> Dir . walk ( self . path , self . build_file ) <EOL> except : <EOL> pass <EOL> def build_file ( self , file ) : <EOL> rel_path = file . replace ( self . path , "<STR_LIT>" ) <EOL> need_compilation = True <EOL> if rel_path in self . static_not_compile : <EOL> need_compilation = False <EOL> new_path = self . path + "<STR_LIT>" + rel_path <EOL> result = self . compiler . compile_file ( file , need_compilation = need_compilation ) <EOL> if result : <EOL> try : <EOL> os . makedirs ( os . path . dirname ( new_path ) ) <EOL> except : <EOL> pass <EOL> with open ( new_path , "<STR_LIT:w>" ) as f : <EOL> f . write ( result ) </s>
