<s> from __future__ import unicode_literals <EOL> import time <EOL> import subprocess <EOL> import sys <EOL> import re <EOL> import os <EOL> import os . path <EOL> import socket <EOL> TARGET_PORT = <NUM_LIT> <EOL> REPLACE_PATH = [ <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT:/>" , "<STR_LIT>" ] , <EOL> ] <EOL> openers = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:path>" : """<STR_LIT>""" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:path>" : """<STR_LIT>""" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:path>" : """<STR_LIT>""" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:path>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : '<STR_LIT>' , <EOL> "<STR_LIT>" : '<STR_LIT>' , <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:path>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : '<STR_LIT>' , <EOL> "<STR_LIT>" : '<STR_LIT>' , <EOL> } <EOL> } <EOL> open_associations = { <EOL> "<STR_LIT>" : openers [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : openers [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : openers [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : openers [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : openers [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : openers [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : openers [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : openers [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : openers [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : openers [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : openers [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : openers [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : openers [ "<STR_LIT>" ] , <EOL> "<STR_LIT:*>" : openers [ "<STR_LIT>" ] , <EOL> } <EOL> def main ( ) : <EOL> if len ( sys . argv ) == <NUM_LIT:1> : <EOL> open_url ( os . getcwd ( ) ) <EOL> return <EOL> if sys . argv [ <NUM_LIT:1> ] in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> usage ( ) <EOL> return <EOL> parse_user_defined_opener ( ) <EOL> if sys . argv [ <NUM_LIT:1> ] == "<STR_LIT>" and not os . path . exists ( "<STR_LIT>" ) : <EOL> del sys . argv [ <NUM_LIT:1> ] <EOL> if len ( sys . argv ) > <NUM_LIT:2> and sys . argv [ <NUM_LIT:2> ] == "<STR_LIT>" and not os . path . exists ( "<STR_LIT>" ) : <EOL> line_number = sys . argv [ <NUM_LIT:3> ] . replace ( "<STR_LIT:U+002C>" , "<STR_LIT>" ) <EOL> the_file = sys . argv [ <NUM_LIT:1> ] . replace ( "<STR_LIT:U+002C>" , "<STR_LIT>" ) <EOL> openfile ( the_file , line_number ) <EOL> return <EOL> first = True <EOL> for parameter in sys . argv [ <NUM_LIT:1> : ] : <EOL> if first : <EOL> first = False <EOL> else : <EOL> pass <EOL> open_url ( parameter ) <EOL> return <EOL> def parse_user_defined_opener ( ) : <EOL> global USER_DEFINED_OPENER <EOL> magic_string = "<STR_LIT>" <EOL> counter = <NUM_LIT:0> <EOL> for argv in sys . argv : <EOL> if argv . find ( magic_string ) == <NUM_LIT:0> : <EOL> del sys . argv [ counter ] <EOL> USER_DEFINED_OPENER = argv [ len ( magic_string ) : ] <EOL> return <EOL> counter += <NUM_LIT:1> <EOL> def open_url ( target_url ) : <EOL> if is_internet_address ( target_url ) : <EOL> send_socket_cmd ( target_url ) <EOL> return <EOL> if os . path . isdir ( target_url ) : <EOL> opendir ( target_url ) <EOL> return <EOL> if os . path . isfile ( target_url ) : <EOL> openfile ( target_url ) <EOL> return <EOL> cln = target_url . find ( "<STR_LIT::>" ) <EOL> if cln > <NUM_LIT:0> : <EOL> file_name = target_url [ <NUM_LIT:0> : cln ] <EOL> if os . path . isfile ( file_name ) : <EOL> line_number = target_url [ cln + <NUM_LIT:1> : ] <EOL> openfile ( file_name , line_number ) <EOL> return <EOL> target_url = sys . argv [ <NUM_LIT:1> ] [ <NUM_LIT:0> : - <NUM_LIT:1> ] <EOL> if os . path . isfile ( target_url ) : <EOL> line_number = sys . argv [ <NUM_LIT:3> ] . replace ( "<STR_LIT:U+002C>" , "<STR_LIT>" ) <EOL> openfile ( target_url , line_number ) <EOL> return <EOL> print "<STR_LIT>" % target_url <EOL> def is_internet_address ( target_url ) : <EOL> return target_url . find ( "<STR_LIT>" ) == <NUM_LIT:0> or target_url . find ( "<STR_LIT>" ) == <NUM_LIT:0> <EOL> def opendir ( the_path ) : <EOL> send_socket_cmd ( convert_path ( the_path ) ) <EOL> def convert_path ( the_path ) : <EOL> return path_replaced ( make_absolute_path_if_necessary ( the_path ) ) <EOL> def get_file_extension ( the_path ) : <EOL> last_dot = the_path . rfind ( "<STR_LIT:.>" ) <EOL> return the_path [ last_dot + <NUM_LIT:1> : ] . lower ( ) <EOL> def openfile ( the_path , line_number = None ) : <EOL> last_dot = the_path . rfind ( "<STR_LIT:.>" ) <EOL> extension = the_path [ last_dot : ] <EOL> if extension in open_associations : <EOL> opener = open_associations [ extension ] <EOL> else : <EOL> opener = open_associations [ "<STR_LIT:*>" ] <EOL> if line_number is None : <EOL> cmd = opener [ "<STR_LIT>" ] <EOL> else : <EOL> cmd = opener [ "<STR_LIT>" ] <EOL> the_path = make_absolute_path_if_necessary ( the_path ) <EOL> project_folder = path_replaced ( get_project_folder ( the_path ) ) <EOL> file_path = path_replaced ( the_path ) <EOL> the_cmd = cmd . format ( opener = opener [ "<STR_LIT:path>" ] , <EOL> file_path = file_path , <EOL> line_number = line_number , <EOL> project_folder = project_folder <EOL> ) <EOL> send_socket_cmd ( the_cmd ) <EOL> def get_project_folder ( file_path ) : <EOL> folder = os . path . dirname ( file_path ) <EOL> while True : <EOL> if os . path . isdir ( os . path . join ( folder , "<STR_LIT>" ) ) : <EOL> return folder <EOL> pos = folder . rfind ( "<STR_LIT:/>" ) <EOL> if pos < <NUM_LIT:1> : <EOL> break <EOL> folder = folder [ <NUM_LIT:0> : pos ] <EOL> return "<STR_LIT>" <EOL> def send_socket_cmd ( msg ) : <EOL> msg = msg . strip ( ) <EOL> sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> sock . connect ( ( server_ip ( ) , TARGET_PORT ) ) <EOL> totalsent = <NUM_LIT:0> <EOL> msglen = len ( msg ) <EOL> while totalsent < msglen : <EOL> sent = sock . send ( msg [ totalsent : ] ) <EOL> if sent == <NUM_LIT:0> : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> totalsent = totalsent + sent <EOL> def make_absolute_path_if_necessary ( the_path ) : <EOL> if the_path [ <NUM_LIT:0> ] == "<STR_LIT:/>" : <EOL> return the_path <EOL> return os . getcwd ( ) + "<STR_LIT:/>" + the_path <EOL> def path_replaced ( the_path ) : <EOL> old_path = the_path <EOL> for replace_pair in REPLACE_PATH : <EOL> if replace_pair [ <NUM_LIT:0> ] in the_path : <EOL> the_path = the_path . replace ( replace_pair [ <NUM_LIT:0> ] , replace_pair [ <NUM_LIT:1> ] , <NUM_LIT:1> ) <EOL> break <EOL> the_path = the_path . replace ( os . sep , "<STR_LIT:\\>" ) <EOL> print "<STR_LIT>" . format ( server_ip ( ) , old_path , the_path ) <EOL> return the_path <EOL> def is_microsoft_linux ( ) : <EOL> with file ( "<STR_LIT>" ) as f : <EOL> file_content = f . read ( ) <EOL> return "<STR_LIT>" in file_content <EOL> return False <EOL> def server_ip ( ) : <EOL> if is_microsoft_linux ( ) : <EOL> return "<STR_LIT:127.0.0.1>" <EOL> output = subprocess . check_output ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> the_ip = re . search ( "<STR_LIT>" , output ) <EOL> return the_ip . group ( <NUM_LIT:0> ) <EOL> def usage ( ) : <EOL> """<STR_LIT>""" <EOL> print """<STR_LIT>""" <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
