<s> import sublime , os , re , importlib <EOL> from sublime import Region <EOL> from subprocess import Popen , PIPE <EOL> def is_file ( message , args , pipeline_data ) : <EOL> """<STR_LIT>""" <EOL> file = message . get ( "<STR_LIT:data>" , None ) <EOL> if file : <EOL> cwd = message [ "<STR_LIT>" ] <EOL> if os . path . isfile ( file ) : <EOL> message [ '<STR_LIT:data>' ] = file <EOL> return file <EOL> elif cwd : <EOL> relative_path = os . path . normpath ( cwd + '<STR_LIT:/>' + file ) <EOL> if os . path . isfile ( relative_path ) : <EOL> message [ '<STR_LIT:data>' ] = relative_path <EOL> return relative_path <EOL> return None <EOL> def is_dir ( message , args , pipeline_data ) : <EOL> """<STR_LIT>""" <EOL> file = message . get ( "<STR_LIT:data>" , None ) <EOL> if file : <EOL> cwd = message [ "<STR_LIT>" ] <EOL> if os . path . isdir ( file ) : <EOL> return file <EOL> message [ '<STR_LIT:data>' ] = file <EOL> elif cwd : <EOL> relative_path = os . path . normpath ( cwd + '<STR_LIT:/>' + file ) <EOL> if os . path . isfile ( relative_path ) : <EOL> message [ '<STR_LIT:data>' ] = relative_path <EOL> return relative_path <EOL> return None <EOL> def pattern ( message , args , pipeline_data ) : <EOL> """<STR_LIT>""" <EOL> text = message . get ( "<STR_LIT:data>" , None ) <EOL> pattern = args [ <NUM_LIT:0> ] <EOL> if text != None : <EOL> return re . search ( pattern , text ) <EOL> return None <EOL> def open_in_tab ( message , args , pipeline_data ) : <EOL> """<STR_LIT>""" <EOL> cwd = message [ '<STR_LIT>' ] <EOL> command = message [ '<STR_LIT:data>' ] <EOL> window = sublime . active_window ( ) <EOL> if os . path . isfile ( command ) : <EOL> tab = window . open_file ( command ) <EOL> return tab <EOL> else : <EOL> p = Popen ( [ command ] , shell = True , cwd = cwd , stdout = PIPE , stderr = PIPE ) <EOL> out , err = p . communicate ( ) <EOL> if out : <EOL> data = out . decode ( encoding = '<STR_LIT:utf-8>' ) <EOL> tab = window . new_file ( ) <EOL> tab . set_scratch ( True ) <EOL> edit_token = message [ '<STR_LIT>' ] <EOL> tab . insert ( edit_token , <NUM_LIT:0> , data ) <EOL> return tab <EOL> def open_in_external_program ( message , args , pipeline_data ) : <EOL> """<STR_LIT>""" <EOL> cwd = message [ '<STR_LIT>' ] <EOL> command = message [ '<STR_LIT:data>' ] <EOL> return Popen ( [ command ] , shell = True , cwd = cwd , close_fds = True ) <EOL> def prepare_command ( message , args , pipeline_data ) : <EOL> """<STR_LIT>""" <EOL> command = args [ <NUM_LIT:0> ] <EOL> if "<STR_LIT>" in pipeline_data : <EOL> match = pipeline_data [ "<STR_LIT>" ] <EOL> for idx , val in enumerate ( match . groups ( ) ) : <EOL> group = idx + <NUM_LIT:1> <EOL> command = command . replace ( "<STR_LIT:$>" + str ( group ) , val ) <EOL> for group , val in match . groupdict ( ) . items ( ) : <EOL> command = command . replace ( "<STR_LIT:$>" + group , val ) <EOL> command = command . replace ( "<STR_LIT>" , message [ '<STR_LIT:data>' ] ) <EOL> message [ '<STR_LIT:data>' ] = command <EOL> return command <EOL> def extract_jump ( message , args , pipeline_data ) : <EOL> """<STR_LIT>""" <EOL> text = message . get ( "<STR_LIT:data>" , None ) <EOL> if text != None : <EOL> match = re . search ( "<STR_LIT>" , text ) <EOL> if match : <EOL> message [ '<STR_LIT:data>' ] = text [ <NUM_LIT:0> : match . span ( <NUM_LIT:1> ) [ <NUM_LIT:0> ] ] <EOL> return ( match . group ( <NUM_LIT:1> ) , match . group ( <NUM_LIT:2> ) ) <EOL> return ( None , None ) <EOL> def jump ( message , args , pipeline_data ) : <EOL> """<STR_LIT>""" <EOL> jump_type , jump_location = pipeline_data . get ( "<STR_LIT>" ) <EOL> window = sublime . active_window ( ) <EOL> view = window . active_view ( ) <EOL> location = <NUM_LIT:0> <EOL> if jump_type == "<STR_LIT:@>" : <EOL> for result in window . lookup_symbol_in_open_files ( jump_location ) : <EOL> view_file = view . file_name ( ) <EOL> result_file = result [ <NUM_LIT:0> ] <EOL> if os . name == '<STR_LIT>' : <EOL> if not result_file . startswith ( "<STR_LIT>" ) : <EOL> result_file = result_file [ <NUM_LIT:1> : ] <EOL> result_file = result_file [ : <NUM_LIT:1> ] + "<STR_LIT>" + result_file [ <NUM_LIT:2> : ] <EOL> result_file = os . path . normpath ( result_file ) <EOL> if view_file == result_file : <EOL> location = view . text_point ( result [ <NUM_LIT:2> ] [ <NUM_LIT:0> ] - <NUM_LIT:1> , result [ <NUM_LIT:2> ] [ <NUM_LIT:1> ] - <NUM_LIT:1> ) <EOL> break <EOL> pass <EOL> elif jump_type == "<STR_LIT:#>" : <EOL> sel = view . sel ( ) <EOL> current_pos = view . sel ( ) [ <NUM_LIT:0> ] if len ( sel ) > <NUM_LIT:0> else sublime . Region ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> next_pos = view . find ( jump_location , current_pos . b + <NUM_LIT:1> ) <EOL> if next_pos . a == - <NUM_LIT:1> : <EOL> next_pos = view . find ( jump_location , <NUM_LIT:1> ) <EOL> if next_pos . a != - <NUM_LIT:1> : <EOL> location = next_pos . a <EOL> elif jump_type == "<STR_LIT::>" : <EOL> location = view . text_point ( int ( jump_location ) - <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> view . sel ( ) . clear ( ) <EOL> view . sel ( ) . add ( Region ( location ) ) <EOL> view . show ( location ) <EOL> return location <EOL> def extern ( message , args , pipeline_data ) : <EOL> """<STR_LIT>""" <EOL> module_name , function_name = args [ : <NUM_LIT:2> ] <EOL> module = importlib . import_module ( module_name ) <EOL> command = module . __dict__ . get ( function_name , None ) <EOL> if command : <EOL> return command ( message , args [ <NUM_LIT:2> : ] , pipeline_data ) <EOL> else : <EOL> return None <EOL> def print_pipeline ( message , args , pipeline_data ) : <EOL> """<STR_LIT>""" <EOL> print ( "<STR_LIT>" , message ) <EOL> print ( "<STR_LIT>" , pipeline_data ) <EOL> return True <EOL> def strip ( message , args , pipeline_data ) : <EOL> """<STR_LIT>""" <EOL> chars = args [ <NUM_LIT:0> ] <EOL> message [ '<STR_LIT:data>' ] = message [ '<STR_LIT:data>' ] . strip ( chars ) <EOL> return chars <EOL> def list_dir ( message , args , pipeline_data ) : <EOL> """<STR_LIT>""" <EOL> cwd = message [ '<STR_LIT:data>' ] <EOL> files = [ os . path . join ( cwd , f ) for f in os . listdir ( cwd ) ] <EOL> contents = "<STR_LIT:\n>" . join ( files ) <EOL> message [ '<STR_LIT:data>' ] = contents <EOL> return contents <EOL> def display_data_in_new_tab ( message , args , pipeline_data ) : <EOL> """<STR_LIT>""" <EOL> window = sublime . active_window ( ) <EOL> tab = window . new_file ( ) <EOL> tab . set_scratch ( True ) <EOL> edit_token = message [ '<STR_LIT>' ] <EOL> tab . insert ( edit_token , <NUM_LIT:0> , message [ '<STR_LIT:data>' ] ) <EOL> return tab </s>
