<s> import sublime , sublime_plugin <EOL> import json , os , re , time , urllib . request <EOL> from functools import partial <EOL> from . . import utils <EOL> SYNTAX_EXT = "<STR_LIT>" if int ( sublime . version ( ) ) >= <NUM_LIT> else "<STR_LIT>" <EOL> RESULTS_TEMPLATES = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } <EOL> def plugin_loaded ( ) : <EOL> sublime . set_timeout_async ( load ) <EOL> def load ( ) : <EOL> global RESULTS_TEMPLATES <EOL> for template in RESULTS_TEMPLATES : <EOL> RESULTS_TEMPLATES [ template ] = sublime . load_resource ( "<STR_LIT>" + utils . get_plugin_name ( ) + "<STR_LIT>" + template + "<STR_LIT>" ) . replace ( "<STR_LIT:\r>" , "<STR_LIT>" ) <EOL> class TestboxCommand ( sublime_plugin . WindowCommand ) : <EOL> def run ( self , cmd = None , test_type = "<STR_LIT>" ) : <EOL> if not len ( self . get_setting ( "<STR_LIT>" ) ) : <EOL> sublime . message_dialog ( "<STR_LIT>" ) <EOL> return <EOL> testbox_tests_root = self . normalize_directory ( self . get_setting ( "<STR_LIT>" ) ) <EOL> directory , filename , ext = self . get_path_parts ( self . window . active_view ( ) . file_name ( ) ) <EOL> full_url = self . get_setting ( "<STR_LIT>" ) <EOL> test_bundle = "<STR_LIT>" <EOL> if test_type in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> if not directory . startswith ( testbox_tests_root ) : <EOL> print ( testbox_tests_root , directory ) <EOL> sublime . message_dialog ( "<STR_LIT>" ) <EOL> return <EOL> full_url += self . get_dotted_directory ( testbox_tests_root , directory ) <EOL> else : <EOL> full_url += self . get_setting ( "<STR_LIT>" ) <EOL> if test_type == "<STR_LIT>" : <EOL> if ext != "<STR_LIT>" : <EOL> sublime . message_dialog ( "<STR_LIT>" ) <EOL> return <EOL> test_bundle = self . get_dotted_directory ( testbox_tests_root , directory ) + "<STR_LIT:.>" + filename <EOL> full_url += "<STR_LIT>" + test_bundle <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> self . output_view = self . window . create_output_panel ( "<STR_LIT>" ) <EOL> testbox_results = { k : self . normalize_directory ( v ) for k , v in self . get_setting ( "<STR_LIT>" ) . items ( ) } <EOL> self . output_view . settings ( ) . set ( "<STR_LIT>" , testbox_results [ "<STR_LIT>" ] + "<STR_LIT>" ) <EOL> self . output_view . settings ( ) . set ( "<STR_LIT>" , testbox_results [ "<STR_LIT>" ] ) <EOL> self . output_view . settings ( ) . set ( "<STR_LIT>" , True ) <EOL> self . output_view . settings ( ) . set ( "<STR_LIT>" , False ) <EOL> self . output_view . settings ( ) . set ( "<STR_LIT>" , False ) <EOL> self . output_view . settings ( ) . set ( "<STR_LIT>" , False ) <EOL> self . output_view . settings ( ) . set ( "<STR_LIT>" , "<STR_LIT>" + utils . get_plugin_name ( ) + "<STR_LIT>" ) <EOL> self . output_view . assign_syntax ( "<STR_LIT>" + utils . get_plugin_name ( ) + "<STR_LIT>" + SYNTAX_EXT ) <EOL> self . window . create_output_panel ( "<STR_LIT>" ) <EOL> self . window . run_command ( "<STR_LIT>" , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> self . output_view . run_command ( "<STR_LIT>" , { "<STR_LIT>" : RESULTS_TEMPLATES [ "<STR_LIT>" ] , "<STR_LIT>" : True , "<STR_LIT>" : True } ) <EOL> sublime . set_timeout_async ( partial ( self . run_tests , full_url , test_bundle ) ) <EOL> def run_tests ( self , full_url , test_bundle ) : <EOL> sublime . status_message ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" + full_url ) <EOL> try : <EOL> json_string = urllib . request . urlopen ( full_url ) . read ( ) . decode ( "<STR_LIT:utf-8>" ) <EOL> result_string = render_result ( json . loads ( json_string ) , test_bundle ) <EOL> except : <EOL> result_string = "<STR_LIT>" + full_url <EOL> result_string += "<STR_LIT>" <EOL> self . output_view . run_command ( "<STR_LIT>" , { "<STR_LIT>" : result_string , "<STR_LIT>" : True , "<STR_LIT>" : False } ) <EOL> def get_setting ( self , setting_key ) : <EOL> if setting_key in self . window . project_data ( ) : <EOL> return self . window . project_data ( ) [ setting_key ] <EOL> package_settings = sublime . load_settings ( "<STR_LIT>" ) <EOL> return package_settings . get ( setting_key , "<STR_LIT>" ) <EOL> def get_path_parts ( self , file_name ) : <EOL> normalized_file_name = file_name . replace ( "<STR_LIT:\\>" , "<STR_LIT:/>" ) <EOL> file_parts = normalized_file_name . split ( "<STR_LIT:/>" ) <EOL> directory = "<STR_LIT:/>" . join ( file_parts [ : - <NUM_LIT:1> ] ) + "<STR_LIT:/>" <EOL> file_and_ext = file_parts [ - <NUM_LIT:1> ] . split ( "<STR_LIT:.>" ) <EOL> return directory , file_and_ext [ <NUM_LIT:0> ] , file_and_ext [ <NUM_LIT:1> ] <EOL> def get_dotted_directory ( self , root , directory ) : <EOL> return directory . replace ( root , "<STR_LIT>" ) . replace ( "<STR_LIT:/>" , "<STR_LIT:.>" ) [ : - <NUM_LIT:1> ] <EOL> def normalize_directory ( self , directory ) : <EOL> if len ( directory ) == <NUM_LIT:0> : <EOL> return "<STR_LIT>" <EOL> normalized_directory = directory . replace ( "<STR_LIT:\\>" , "<STR_LIT:/>" ) <EOL> if normalized_directory [ - <NUM_LIT:1> ] != "<STR_LIT:/>" : <EOL> normalized_directory += "<STR_LIT:/>" <EOL> return normalized_directory <EOL> def render_result ( result_data , test_bundle ) : <EOL> result_data = lcase_keys ( result_data ) <EOL> result_string = sublime . expand_variables ( RESULTS_TEMPLATES [ "<STR_LIT>" ] , filter_stats_dict ( result_data ) ) + "<STR_LIT:\n>" <EOL> for bundle in result_data [ "<STR_LIT>" ] : <EOL> if len ( test_bundle ) and bundle [ "<STR_LIT:path>" ] != test_bundle : <EOL> continue <EOL> result_string += "<STR_LIT:\n>" + sublime . expand_variables ( RESULTS_TEMPLATES [ "<STR_LIT>" ] , filter_stats_dict ( bundle ) ) + "<STR_LIT:\n>" <EOL> if isinstance ( bundle [ "<STR_LIT>" ] , dict ) : <EOL> result_string += "<STR_LIT:\n>" + sublime . expand_variables ( RESULTS_TEMPLATES [ "<STR_LIT>" ] , filter_exception_dict ( bundle [ "<STR_LIT>" ] ) ) + "<STR_LIT:\n>" <EOL> for suite in bundle [ "<STR_LIT>" ] : <EOL> result_string += "<STR_LIT:\n>" + gen_suite_report ( suite ) <EOL> result_string += "<STR_LIT:\n>" + RESULTS_TEMPLATES [ "<STR_LIT>" ] <EOL> return result_string <EOL> def lcase_keys ( source ) : <EOL> if isinstance ( source , dict ) : <EOL> return { k . lower ( ) : lcase_keys ( source [ k ] ) for k in source } <EOL> if isinstance ( source , list ) : <EOL> return [ lcase_keys ( item ) for item in source ] <EOL> return source <EOL> def filter_stats_dict ( source ) : <EOL> stat_keys = [ "<STR_LIT:path>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> stat_keys . extend ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> return { key : str ( source [ key ] ) for key in stat_keys if key in source } <EOL> def filter_exception_dict ( source ) : <EOL> exception_keys = [ "<STR_LIT:type>" , "<STR_LIT:message>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> return { key : str ( source [ key ] ) for key in exception_keys if key in source } <EOL> def get_status_bit ( status ) : <EOL> status_dict = { "<STR_LIT>" : "<STR_LIT:!>" , "<STR_LIT>" : "<STR_LIT:X>" , "<STR_LIT>" : "<STR_LIT:->" } <EOL> if status in status_dict : <EOL> return status_dict [ status ] <EOL> return "<STR_LIT:+>" <EOL> def build_stacktrace ( stack , tabs ) : <EOL> stacktrace = "<STR_LIT>" <EOL> for row in stack : <EOL> stacktrace += tabs + "<STR_LIT:U+0020>" + row [ "<STR_LIT>" ] + "<STR_LIT::>" + str ( row [ "<STR_LIT>" ] ) + "<STR_LIT:\n>" <EOL> return stacktrace <EOL> def gen_suite_report ( suiteStats , level = <NUM_LIT:0> ) : <EOL> tabs = "<STR_LIT:U+0020>" * level <EOL> report = tabs + "<STR_LIT:(>" + get_status_bit ( suiteStats [ "<STR_LIT:status>" ] ) + "<STR_LIT:)>" + suiteStats [ "<STR_LIT:name>" ] + "<STR_LIT:\n>" <EOL> level += <NUM_LIT:1> <EOL> for spec in suiteStats [ "<STR_LIT>" ] : <EOL> tabs = "<STR_LIT:U+0020>" * level <EOL> report += tabs + "<STR_LIT:(>" + get_status_bit ( spec [ "<STR_LIT:status>" ] ) + "<STR_LIT:)>" + spec [ "<STR_LIT:name>" ] + "<STR_LIT>" + str ( spec [ "<STR_LIT>" ] ) + "<STR_LIT>" + "<STR_LIT:\n>" <EOL> if spec [ "<STR_LIT:status>" ] == "<STR_LIT>" : <EOL> report += tabs + "<STR_LIT>" + spec [ "<STR_LIT>" ] + "<STR_LIT:\n>" <EOL> report += build_stacktrace ( spec [ "<STR_LIT>" ] , tabs ) <EOL> if spec [ "<STR_LIT:status>" ] == "<STR_LIT>" : <EOL> report += tabs + "<STR_LIT>" + spec [ "<STR_LIT:error>" ] [ "<STR_LIT:message>" ] + "<STR_LIT:\n>" <EOL> if "<STR_LIT>" in spec [ "<STR_LIT:error>" ] : <EOL> report += build_stacktrace ( spec [ "<STR_LIT:error>" ] [ "<STR_LIT>" ] , tabs ) <EOL> for nestedSuite in suiteStats [ "<STR_LIT>" ] : <EOL> report += gen_suite_report ( nestedSuite , level + <NUM_LIT:1> ) + "<STR_LIT:\n>" <EOL> return report </s>
