<s> import json <EOL> import os <EOL> import sublime <EOL> from . import log <EOL> from . Common import Singleton <EOL> __file__ = os . path . normpath ( os . path . abspath ( __file__ ) ) <EOL> __path__ = os . path . dirname ( __file__ ) <EOL> class SettingEventSource ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _subscribers = { } <EOL> def notify ( self , evt_type , * evt_args , ** evt_kwargs ) : <EOL> if evt_type not in self . _subscribers : <EOL> return <EOL> for subscriber in self . _subscribers [ evt_type ] : <EOL> try : <EOL> subscriber ( * evt_args , ** evt_kwargs ) <EOL> except : <EOL> log . exception ( "<STR_LIT>" , str ( subscriber ) ) <EOL> def subscribe ( self , evt_type , subscriber ) : <EOL> if evt_type not in self . _subscribers : <EOL> self . _subscribers [ evt_type ] = set ( ) <EOL> self . _subscribers [ evt_type ] . add ( subscriber ) <EOL> def clear_subscribers ( self ) : <EOL> self . _subscribers . clear ( ) <EOL> @ Singleton <EOL> class Setting ( SettingEventSource ) : <EOL> def __init__ ( self ) : <EOL> SettingEventSource . __init__ ( self ) <EOL> @ staticmethod <EOL> def _read_default_settings ( ) : <EOL> default_settings_filename = os . path . join ( <EOL> __path__ , '<STR_LIT>' , '<STR_LIT>' ) <EOL> default_settings_filename = os . path . normpath ( default_settings_filename ) <EOL> with open ( default_settings_filename ) as f : <EOL> settings_obj = json . load ( f ) <EOL> return settings_obj <EOL> def _fix_setting_type ( self ) : <EOL> type_conversion_map = { <EOL> '<STR_LIT>' : set , <EOL> } <EOL> for attr , typ in type_conversion_map . items ( ) : <EOL> v = getattr ( self , attr ) <EOL> setattr ( self , attr , typ ( v ) ) <EOL> def load_setting ( self ) : <EOL> PLUGIN_NAME = '<STR_LIT>' <EOL> settings = sublime . load_settings ( PLUGIN_NAME + '<STR_LIT>' ) <EOL> settings . clear_on_change ( PLUGIN_NAME ) <EOL> settings . add_on_change ( PLUGIN_NAME , self . sublime_settings_on_change ) <EOL> self . _sublime_settings = settings <EOL> default_settings = self . _read_default_settings ( ) <EOL> for k , v in default_settings . items ( ) : <EOL> if isinstance ( v , dict ) : <EOL> v . update ( settings . get ( k , { } ) ) <EOL> else : <EOL> v = settings . get ( k , v ) <EOL> setattr ( self , k , v ) <EOL> self . _fix_setting_type ( ) <EOL> def get_setting ( self , k , default = None ) : <EOL> return getattr ( self , k , default ) <EOL> def init ( self ) : <EOL> self . clear_subscribers ( ) <EOL> self . load_setting ( ) <EOL> def sublime_settings_on_change ( self ) : <EOL> log . info ( '<STR_LIT>' ) <EOL> self . notify ( '<STR_LIT>' , setting = self ) <EOL> self . load_setting ( ) <EOL> self . notify ( '<STR_LIT>' , setting = self ) </s>
