<s> from . import base <EOL> from protorpc import messages <EOL> import logging <EOL> import os <EOL> import re <EOL> import sass <EOL> SUFFIXES = frozenset ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> SUFFIX_PATTERN = re . compile ( '<STR_LIT>' + '<STR_LIT:|>' . join ( map ( re . escape , SUFFIXES ) ) + '<STR_LIT>' ) <EOL> class Config ( messages . Message ) : <EOL> sass_dir = messages . StringField ( <NUM_LIT:1> ) <EOL> out_dir = messages . StringField ( <NUM_LIT:2> ) <EOL> suffix = messages . StringField ( <NUM_LIT:3> , default = '<STR_LIT>' ) <EOL> output_style = messages . StringField ( <NUM_LIT:4> , default = '<STR_LIT>' ) <EOL> source_comments = messages . BooleanField ( <NUM_LIT:5> ) <EOL> image_path = messages . StringField ( <NUM_LIT:6> ) <EOL> class SassPreprocessor ( base . BasePreprocessor ) : <EOL> KIND = '<STR_LIT>' <EOL> Config = Config <EOL> def run ( self , build = True ) : <EOL> sass_dir = os . path . abspath ( os . path . join ( self . root , self . config . sass_dir . lstrip ( '<STR_LIT:/>' ) ) ) <EOL> out_dir = os . path . abspath ( os . path . join ( self . root , self . config . out_dir . lstrip ( '<STR_LIT:/>' ) ) ) <EOL> self . build_directory ( sass_dir , out_dir ) <EOL> def build_directory ( self , sass_path , css_path , _root_sass = None , _root_css = None ) : <EOL> if self . config . image_path : <EOL> image_path = os . path . abspath ( os . path . join ( self . root , self . config . image_path . lstrip ( '<STR_LIT:/>' ) ) ) <EOL> else : <EOL> image_path = None <EOL> _root_sass = sass_path if _root_sass is None else _root_sass <EOL> _root_css = css_path if _root_css is None else _root_css <EOL> result = { } <EOL> if not os . path . isdir ( css_path ) : <EOL> os . makedirs ( css_path ) <EOL> for name in os . listdir ( sass_path ) : <EOL> if not SUFFIX_PATTERN . search ( name ) or name . startswith ( '<STR_LIT:_>' ) : <EOL> continue <EOL> sass_fullname = os . path . join ( sass_path , name ) <EOL> if os . path . isfile ( sass_fullname ) : <EOL> basename = os . path . splitext ( name ) [ <NUM_LIT:0> ] <EOL> css_fullname = os . path . join ( css_path , basename ) + self . config . suffix <EOL> try : <EOL> kwargs = { <EOL> '<STR_LIT:filename>' : sass_fullname , <EOL> '<STR_LIT>' : [ _root_sass ] , <EOL> '<STR_LIT>' : self . config . output_style , <EOL> } <EOL> if self . config . output_style is not None : <EOL> kwargs [ '<STR_LIT>' ] = self . config . output_style <EOL> if image_path is not None : <EOL> kwargs [ '<STR_LIT>' ] = image_path <EOL> if self . config . image_path is not None : <EOL> kwargs [ '<STR_LIT>' ] = image_path <EOL> css = sass . compile ( ** kwargs ) <EOL> except sass . CompileError as e : <EOL> logging . error ( str ( e ) ) <EOL> return result <EOL> with open ( css_fullname , '<STR_LIT:w>' ) as css_file : <EOL> if isinstance ( css , unicode ) : <EOL> css = css . encode ( '<STR_LIT:utf-8>' ) <EOL> css_file . write ( css ) <EOL> result [ sass_fullname ] = css_fullname <EOL> elif os . path . isdir ( sass_fullname ) : <EOL> css_fullname = os . path . join ( css_path , name ) <EOL> subresult = self . build_directory ( sass_fullname , css_fullname , <EOL> _root_sass , _root_css ) <EOL> result . update ( subresult ) <EOL> for sass_path , out_path in result . iteritems ( ) : <EOL> self . logger . info ( <EOL> '<STR_LIT>' . format ( sass_path . replace ( self . root , '<STR_LIT>' ) , <EOL> out_path . replace ( self . root , '<STR_LIT>' ) ) ) <EOL> return result <EOL> def list_watched_dirs ( self ) : <EOL> return [ self . config . sass_dir ] </s>
