<s> from time import strftime <EOL> import argparse <EOL> import sys <EOL> import codecs <EOL> import os <EOL> import os . path <EOL> import time <EOL> import hamlpy <EOL> import nodes as hamlpynodes <EOL> try : <EOL> str = unicode <EOL> except NameError : <EOL> pass <EOL> class Options ( object ) : <EOL> CHECK_INTERVAL = <NUM_LIT:3> <EOL> DEBUG = False <EOL> VERBOSE = False <EOL> OUTPUT_EXT = '<STR_LIT>' <EOL> compiled = dict ( ) <EOL> class StoreNameValueTagPair ( argparse . Action ) : <EOL> def __call__ ( self , parser , namespace , values , option_string = None ) : <EOL> tags = getattr ( namespace , '<STR_LIT>' , { } ) <EOL> if tags is None : <EOL> tags = { } <EOL> for item in values : <EOL> n , v = item . split ( '<STR_LIT::>' ) <EOL> tags [ n ] = v <EOL> setattr ( namespace , '<STR_LIT>' , tags ) <EOL> arg_parser = argparse . ArgumentParser ( ) <EOL> arg_parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , help = '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> arg_parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , metavar = '<STR_LIT>' , default = '<STR_LIT>' , help = '<STR_LIT>' , type = str , nargs = '<STR_LIT:+>' ) <EOL> arg_parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , metavar = '<STR_LIT>' , default = Options . OUTPUT_EXT , help = '<STR_LIT>' , type = str ) <EOL> arg_parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , metavar = '<STR_LIT:S>' , default = Options . CHECK_INTERVAL , help = '<STR_LIT>' . format ( Options . CHECK_INTERVAL ) , type = int ) <EOL> arg_parser . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' , type = str ) <EOL> arg_parser . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' , type = str , nargs = '<STR_LIT:?>' ) <EOL> arg_parser . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' , type = str , nargs = <NUM_LIT:1> , action = StoreNameValueTagPair ) <EOL> arg_parser . add_argument ( '<STR_LIT>' , dest = '<STR_LIT>' , type = str , choices = ( '<STR_LIT:">' , "<STR_LIT:'>" ) , default = "<STR_LIT:'>" , action = '<STR_LIT:store>' , help = "<STR_LIT>" ) <EOL> arg_parser . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' , default = False , action = '<STR_LIT:store_true>' ) <EOL> def watched_extension ( extension ) : <EOL> """<STR_LIT>""" <EOL> for ext in hamlpy . VALID_EXTENSIONS : <EOL> if extension . endswith ( '<STR_LIT:.>' + ext ) : <EOL> return True <EOL> return False <EOL> def watch_folder ( ) : <EOL> """<STR_LIT>""" <EOL> argv = sys . argv [ <NUM_LIT:1> : ] if len ( sys . argv ) > <NUM_LIT:1> else [ ] <EOL> args = arg_parser . parse_args ( sys . argv [ <NUM_LIT:1> : ] ) <EOL> compiler_args = { } <EOL> input_folder = os . path . realpath ( args . input_dir ) <EOL> if not args . output_dir : <EOL> output_folder = input_folder <EOL> else : <EOL> output_folder = os . path . realpath ( args . output_dir ) <EOL> if args . verbose : <EOL> Options . VERBOSE = True <EOL> print "<STR_LIT>" . format ( input_folder , args . refresh ) <EOL> if args . extension : <EOL> Options . OUTPUT_EXT = args . extension <EOL> if getattr ( args , '<STR_LIT>' , False ) : <EOL> hamlpynodes . TagNode . self_closing . update ( args . tags ) <EOL> if args . input_extension : <EOL> hamlpy . VALID_EXTENSIONS += args . input_extension <EOL> if args . attr_wrapper : <EOL> compiler_args [ '<STR_LIT>' ] = args . attr_wrapper <EOL> if args . jinja : <EOL> for k in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> del hamlpynodes . TagNode . self_closing [ k ] <EOL> hamlpynodes . TagNode . may_contain . pop ( k , None ) <EOL> hamlpynodes . TagNode . self_closing . update ( { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } ) <EOL> hamlpynodes . TagNode . may_contain [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> while True : <EOL> try : <EOL> _watch_folder ( input_folder , output_folder , compiler_args ) <EOL> time . sleep ( args . refresh ) <EOL> except KeyboardInterrupt : <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> def _watch_folder ( folder , destination , compiler_args ) : <EOL> """<STR_LIT>""" <EOL> for dirpath , dirnames , filenames in os . walk ( folder ) : <EOL> for filename in filenames : <EOL> if watched_extension ( filename ) and not filename . startswith ( '<STR_LIT>' ) : <EOL> fullpath = os . path . join ( dirpath , filename ) <EOL> subfolder = os . path . relpath ( dirpath , folder ) <EOL> mtime = os . stat ( fullpath ) . st_mtime <EOL> compiled_folder = os . path . join ( destination , subfolder ) <EOL> if not os . path . exists ( compiled_folder ) : <EOL> os . makedirs ( compiled_folder ) <EOL> compiled_path = _compiled_path ( compiled_folder , filename ) <EOL> if ( not fullpath in compiled or <EOL> compiled [ fullpath ] < mtime or <EOL> not os . path . isfile ( compiled_path ) ) : <EOL> compile_file ( fullpath , compiled_path , compiler_args ) <EOL> compiled [ fullpath ] = mtime <EOL> def _compiled_path ( destination , filename ) : <EOL> return os . path . join ( destination , filename [ : filename . rfind ( '<STR_LIT:.>' ) ] + Options . OUTPUT_EXT ) <EOL> def compile_file ( fullpath , outfile_name , compiler_args ) : <EOL> """<STR_LIT>""" <EOL> if Options . VERBOSE : <EOL> print '<STR_LIT>' % ( strftime ( "<STR_LIT>" ) , fullpath , outfile_name ) <EOL> try : <EOL> if Options . DEBUG : <EOL> print "<STR_LIT>" % ( fullpath , outfile_name ) <EOL> haml_lines = codecs . open ( fullpath , '<STR_LIT:r>' , encoding = '<STR_LIT:utf-8>' ) . read ( ) . splitlines ( ) <EOL> compiler = hamlpy . Compiler ( compiler_args ) <EOL> output = compiler . process_lines ( haml_lines ) <EOL> outfile = codecs . open ( outfile_name , '<STR_LIT:w>' , encoding = '<STR_LIT:utf-8>' ) <EOL> outfile . write ( output ) <EOL> except Exception , e : <EOL> print "<STR_LIT>" % ( fullpath , outfile_name , e ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> watch_folder ( ) </s>
