<s> import sublime <EOL> import sublime_plugin <EOL> from os . path import join , exists , basename , normpath , dirname , isfile <EOL> from os import listdir , makedirs , chmod , unlink <EOL> from os import stat as osstat <EOL> import stat <EOL> import subprocess <EOL> ST3 = int ( sublime . version ( ) ) >= <NUM_LIT> <EOL> if ST3 : <EOL> from . lib . package_search import PackageSearch <EOL> from . lib . binary_manager import update_binary , check_version , get_binary_location <EOL> else : <EOL> from lib . package_search import PackageSearch <EOL> from lib . binary_manager import update_binary , check_version , get_binary_location <EOL> PLUGIN_NAME = "<STR_LIT>" <EOL> THEME_EDITOR = None <EOL> TEMP_FOLDER = "<STR_LIT>" <EOL> TEMP_PATH = "<STR_LIT>" % TEMP_FOLDER <EOL> PLUGIN_SETTINGS = '<STR_LIT>' <EOL> PREFERENCES = '<STR_LIT>' <EOL> SCHEME = "<STR_LIT>" <EOL> THEMES = "<STR_LIT>" <EOL> MSGS = { <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> def load_resource ( resource , binary = False ) : <EOL> bfr = None <EOL> if ST3 : <EOL> if not binary : <EOL> bfr = sublime . load_resource ( resource ) <EOL> else : <EOL> bfr = sublime . load_binary_resource ( resource ) <EOL> else : <EOL> resource = resource . replace ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:1> ) <EOL> if sublime . platform ( ) == "<STR_LIT>" : <EOL> resource = resource . replace ( "<STR_LIT:/>" , "<STR_LIT:\\>" ) <EOL> try : <EOL> mode = "<STR_LIT:rb>" if binary else "<STR_LIT:r>" <EOL> with open ( join ( sublime . packages_path ( ) , resource ) , mode ) as f : <EOL> bfr = f . read ( ) <EOL> except Exception as e : <EOL> print ( e ) <EOL> pass <EOL> return bfr <EOL> def nix_check_permissions ( bin ) : <EOL> st = osstat ( bin ) <EOL> if not bool ( st . st_mode & stat . S_IEXEC ) : <EOL> chmod ( bin , st . st_mode | stat . S_IEXEC ) <EOL> class ColorSchemeEditorCommand ( sublime_plugin . ApplicationCommand ) : <EOL> def init_settings ( self , action , select_theme ) : <EOL> init_okay = True <EOL> self . p_settings = sublime . load_settings ( PLUGIN_SETTINGS ) <EOL> self . settings = sublime . load_settings ( PREFERENCES ) <EOL> self . direct_edit = bool ( self . p_settings . get ( "<STR_LIT>" , False ) ) <EOL> self . scheme_file = self . settings . get ( SCHEME , None ) <EOL> self . actual_scheme_file = None <EOL> self . file_select = False <EOL> if action == "<STR_LIT>" : <EOL> if select_theme is not None : <EOL> self . scheme_file = select_theme <EOL> else : <EOL> init_okay = False <EOL> return init_okay <EOL> def check_binary ( self ) : <EOL> safe = True <EOL> if THEME_EDITOR is None or not exists ( THEME_EDITOR ) : <EOL> if sublime . ok_cancel_dialog ( MSGS [ "<STR_LIT>" ] ) : <EOL> update_binary ( init_plugin ) <EOL> safe = False <EOL> if safe : <EOL> if sublime . platform ( ) in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> nix_check_permissions ( THEME_EDITOR ) <EOL> return safe <EOL> def prepare_theme ( self , action ) : <EOL> if action != "<STR_LIT>" and self . scheme_file is not None and ( action == "<STR_LIT>" or action == "<STR_LIT>" ) : <EOL> self . actual_scheme_file = join ( dirname ( sublime . packages_path ( ) ) , normpath ( self . scheme_file ) ) <EOL> if ( <EOL> not exists ( self . actual_scheme_file ) or <EOL> ( not self . direct_edit and not self . scheme_file . startswith ( TEMP_PATH ) ) <EOL> ) : <EOL> zipped_themes = join ( sublime . packages_path ( ) , "<STR_LIT>" , TEMP_FOLDER ) <EOL> if not exists ( zipped_themes ) : <EOL> makedirs ( zipped_themes ) <EOL> text = load_resource ( self . scheme_file , binary = True ) <EOL> self . actual_scheme_file = join ( zipped_themes , basename ( self . scheme_file ) ) <EOL> try : <EOL> with open ( self . actual_scheme_file , "<STR_LIT:wb>" ) as f : <EOL> f . write ( text ) <EOL> except : <EOL> sublime . error_message ( MSGS [ "<STR_LIT>" ] ) <EOL> return <EOL> self . settings . set ( SCHEME , "<STR_LIT>" % ( TEMP_PATH , basename ( self . scheme_file ) ) ) <EOL> elif action == "<STR_LIT>" : <EOL> self . settings . set ( SCHEME , self . scheme_file ) <EOL> elif action != "<STR_LIT>" and action != "<STR_LIT>" : <EOL> self . file_select = True <EOL> def run ( self , action = None , select_theme = None , live_edit = None ) : <EOL> if not self . check_binary ( ) : <EOL> return <EOL> if not self . init_settings ( action , select_theme ) : <EOL> return <EOL> self . prepare_theme ( action ) <EOL> try : <EOL> subprocess . Popen ( <EOL> [ THEME_EDITOR ] + <EOL> ( [ "<STR_LIT>" ] if bool ( self . p_settings . get ( "<STR_LIT>" , False ) ) else [ ] ) + <EOL> ( [ "<STR_LIT>" ] if action == "<STR_LIT>" else [ ] ) + <EOL> ( [ "<STR_LIT>" ] if self . file_select else [ ] ) + <EOL> ( [ "<STR_LIT>" ] if ( live_edit is None and bool ( self . p_settings . get ( "<STR_LIT>" , True ) ) ) or ( live_edit is not None and live_edit ) else [ ] ) + <EOL> [ "<STR_LIT>" , join ( sublime . packages_path ( ) , "<STR_LIT>" ) ] + <EOL> ( [ self . actual_scheme_file ] if self . actual_scheme_file is not None and exists ( self . actual_scheme_file ) else [ ] ) <EOL> ) <EOL> except : <EOL> sublime . error_message ( MSGS [ "<STR_LIT>" ] ) <EOL> class GetColorSchemeFilesCommand ( sublime_plugin . WindowCommand , PackageSearch ) : <EOL> def on_select ( self , value , settings ) : <EOL> if value != - <NUM_LIT:1> : <EOL> preferences = sublime . load_settings ( PREFERENCES ) <EOL> preferences . set ( SCHEME , settings [ value ] ) <EOL> def process_file ( self , value , settings ) : <EOL> if value != - <NUM_LIT:1> : <EOL> if self . edit : <EOL> sublime . run_command ( <EOL> "<STR_LIT>" , <EOL> { "<STR_LIT:action>" : "<STR_LIT>" , "<STR_LIT>" : settings [ value ] } <EOL> ) <EOL> else : <EOL> preferences = sublime . load_settings ( PREFERENCES ) <EOL> preferences . set ( SCHEME , settings [ value ] ) <EOL> else : <EOL> if self . current_color_scheme is not None : <EOL> preferences = sublime . load_settings ( PREFERENCES ) <EOL> preferences . set ( SCHEME , self . current_color_scheme ) <EOL> def pre_process ( self , ** kwargs ) : <EOL> self . edit = kwargs . get ( "<STR_LIT>" , True ) <EOL> self . current_color_scheme = sublime . load_settings ( "<STR_LIT>" ) . get ( "<STR_LIT>" ) <EOL> return { "<STR_LIT>" : "<STR_LIT>" } <EOL> def run ( self , ** kwargs ) : <EOL> self . search ( ** kwargs ) <EOL> class ColorSchemeEditorLogCommand ( sublime_plugin . WindowCommand ) : <EOL> def run ( self ) : <EOL> log = join ( sublime . packages_path ( ) , "<STR_LIT>" , "<STR_LIT>" ) <EOL> if exists ( log ) : <EOL> self . window . open_file ( log ) <EOL> class ColorSchemeClearTempCommand ( sublime_plugin . ApplicationCommand ) : <EOL> def run ( self ) : <EOL> current_scheme = sublime . load_settings ( PREFERENCES ) . get ( SCHEME ) <EOL> using_temp = current_scheme . startswith ( TEMP_PATH ) <EOL> folder = join ( sublime . packages_path ( ) , "<STR_LIT>" , TEMP_FOLDER ) <EOL> for f in listdir ( folder ) : <EOL> pth = join ( folder , f ) <EOL> try : <EOL> if ( <EOL> isfile ( pth ) and <EOL> ( <EOL> not using_temp or ( <EOL> basename ( pth ) != basename ( current_scheme ) and <EOL> basename ( pth ) != basename ( current_scheme ) + "<STR_LIT>" <EOL> ) <EOL> ) <EOL> ) : <EOL> unlink ( pth ) <EOL> except : <EOL> print ( "<STR_LIT>" % pth ) <EOL> class ColorSchemeEditorUpgradeCommand ( sublime_plugin . ApplicationCommand ) : <EOL> def run ( self ) : <EOL> if THEME_EDITOR is None or not exists ( THEME_EDITOR ) : <EOL> if sublime . ok_cancel_dialog ( MSGS [ "<STR_LIT>" ] ) : <EOL> update_binary ( init_plugin ) <EOL> else : <EOL> if not check_version ( THEME_EDITOR , sublime . load_settings ( PLUGIN_SETTINGS ) , init_plugin ) : <EOL> sublime . message_dialog ( MSGS [ "<STR_LIT>" ] ) <EOL> def init_plugin ( ) : <EOL> global THEME_EDITOR <EOL> platform = sublime . platform ( ) <EOL> p_settings = sublime . load_settings ( PLUGIN_SETTINGS ) <EOL> p_settings . clear_on_change ( '<STR_LIT>' ) <EOL> THEME_EDITOR = get_binary_location ( ) <EOL> if THEME_EDITOR is None or not exists ( THEME_EDITOR ) : <EOL> THEME_EDITOR = None <EOL> elif platform in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> nix_check_permissions ( THEME_EDITOR ) <EOL> if THEME_EDITOR is not None : <EOL> check_version ( THEME_EDITOR , p_settings , init_plugin ) <EOL> else : <EOL> if sublime . ok_cancel_dialog ( MSGS [ "<STR_LIT>" ] ) : <EOL> update_binary ( init_plugin ) <EOL> p_settings . add_on_change ( '<STR_LIT>' , init_plugin ) <EOL> def plugin_loaded ( ) : <EOL> sublime . set_timeout ( init_plugin , <NUM_LIT> ) <EOL> if not ST3 : <EOL> plugin_loaded ( ) </s>
