<s> import sublime <EOL> import re <EOL> import os <EOL> import plistlib <EOL> VERSION = int ( sublime . version ( ) ) <EOL> from . package_resources import * <EOL> class ColorSchemeManager ( ) : <EOL> update_preferences = True <EOL> def __init__ ( self , new_color_scheme_path ) : <EOL> self . new_color_scheme_path = new_color_scheme_path <EOL> def _add_colors_to_scheme ( self , color_scheme_plist , colors ) : <EOL> settings = color_scheme_plist . settings <EOL> scope_exist = False <EOL> updates_made = False <EOL> for color in colors : <EOL> if re . match ( r"<STR_LIT>" , color ) is None : <EOL> print ( "<STR_LIT>" + <EOL> color ) <EOL> print ( "<STR_LIT>" ) <EOL> continue <EOL> scope = "<STR_LIT>" + color . upper ( ) <EOL> for setting in settings : <EOL> if "<STR_LIT>" in setting and setting [ "<STR_LIT>" ] == scope : <EOL> scope_exist = True <EOL> break <EOL> if not scope_exist : <EOL> updates_made = True <EOL> entry = { } <EOL> entry [ "<STR_LIT:name>" ] = "<STR_LIT>" + color <EOL> entry [ "<STR_LIT>" ] = scope <EOL> entry [ "<STR_LIT>" ] = { "<STR_LIT>" : "<STR_LIT:#>" + color } <EOL> settings . append ( entry ) <EOL> color_scheme_plist . settings = settings <EOL> return updates_made , color_scheme_plist <EOL> def _create_custom_color_scheme_directory ( self ) : <EOL> package_path = sublime . packages_path ( ) <EOL> path = os . path . join ( package_path , self . new_color_scheme_path ) <EOL> if not os . path . exists ( path ) : <EOL> os . makedirs ( path ) <EOL> return path <EOL> def create_user_custom_theme ( self , colors ) : <EOL> if len ( colors ) == <NUM_LIT:0> : <EOL> return <EOL> preferences = sublime . load_settings ( "<STR_LIT>" ) <EOL> preferences_cs = preferences . get ( '<STR_LIT>' ) <EOL> cs_base = os . path . basename ( preferences_cs ) <EOL> if cs_base [ <NUM_LIT:0> : <NUM_LIT:15> ] != "<STR_LIT>" : <EOL> new_cs_base = "<STR_LIT>" + cs_base <EOL> else : <EOL> new_cs_base = cs_base <EOL> custom_color_base = self . _create_custom_color_scheme_directory ( ) <EOL> new_cs_absolute = os . path . join ( custom_color_base , new_cs_base ) <EOL> new_cs = "<STR_LIT>" + self . new_color_scheme_path + "<STR_LIT:/>" + new_cs_base <EOL> try : <EOL> package , resource = get_package_and_resource_name ( preferences_cs ) <EOL> if VERSION > <NUM_LIT> : <EOL> binary_data = get_binary_resource ( package , resource ) <EOL> cs_plist = plistlib . readPlistFromBytes ( binary_data ) <EOL> else : <EOL> preferences_cs_absolute = os . path . join ( sublime . packages_path ( ) , <EOL> "<STR_LIT:..>" , <EOL> preferences_cs ) <EOL> cs_plist = plistlib . readPlist ( preferences_cs_absolute ) <EOL> except : <EOL> sublime . error_message ( "<STR_LIT>" + <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> raise <EOL> updates_made , color_scheme = self . _add_colors_to_scheme ( cs_plist , <EOL> colors ) <EOL> if updates_made or preferences_cs != new_cs : <EOL> plistlib . writePlist ( color_scheme , new_cs_absolute ) <EOL> if preferences_cs != new_cs : <EOL> if ColorSchemeManager . update_preferences : <EOL> okay = sublime . ok_cancel_dialog ( "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> new_cs + "<STR_LIT>" + "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> if okay : <EOL> preferences . set ( "<STR_LIT>" , new_cs ) <EOL> sublime . save_settings ( "<STR_LIT>" ) <EOL> else : <EOL> ColorSchemeManager . update_preferences = False </s>
