<s> import pytz <EOL> import sublime <EOL> import sublime_plugin <EOL> try : <EOL> from . format_date import FormatDate , UnknownTimeZoneError <EOL> except ValueError : <EOL> from format_date import FormatDate , UnknownTimeZoneError <EOL> ST2 = int ( sublime . version ( ) ) < <NUM_LIT> <EOL> if not ST2 : <EOL> basestring = str <EOL> fdate = FormatDate ( ) <EOL> s = None <EOL> DEBUG = False <EOL> def status ( msg , e = None ) : <EOL> msg = "<STR_LIT>" + msg <EOL> sublime . status_message ( msg ) <EOL> if e is not None : <EOL> msg += "<STR_LIT>" % ( type ( e ) . __name__ , e ) <EOL> print ( msg ) <EOL> if e and DEBUG : <EOL> import traceback <EOL> traceback . print_exc ( ) <EOL> def show_timezone_quickpanel ( callback , selected_item ) : <EOL> global s <EOL> show_quick_panel = sublime . active_window ( ) . show_quick_panel <EOL> if ST2 : <EOL> show_quick_panel ( pytz . all_timezones , callback ) <EOL> else : <EOL> try : <EOL> selected_index = pytz . all_timezones . index ( selected_item ) <EOL> except ValueError : <EOL> selected_index = <NUM_LIT:0> <EOL> show_quick_panel ( pytz . all_timezones , callback , <EOL> selected_index = selected_index ) <EOL> class Settings ( object ) : <EOL> """<STR_LIT>""" <EOL> _sobj = None <EOL> _settings = None <EOL> _callback = None <EOL> _auto_update = False <EOL> def __init__ ( self , settings_obj , settings , callback = None , auto_update = True ) : <EOL> self . _sobj = settings_obj <EOL> for k , v in settings . items ( ) : <EOL> if v is None : <EOL> settings [ k ] = ( k , None ) <EOL> if isinstance ( v , basestring ) : <EOL> settings [ k ] = ( v , None ) <EOL> self . _settings = settings <EOL> self . update ( ) <EOL> self . set_callback ( callback , auto_update ) <EOL> def update ( self ) : <EOL> for attr , ( name , def_value ) in self . _settings . items ( ) : <EOL> setattr ( self , attr , self . _sobj . get ( name , def_value ) ) <EOL> def _on_change ( self ) : <EOL> if self . has_changed ( ) : <EOL> self . update ( ) <EOL> if self . _callback : <EOL> self . _callback ( ) <EOL> def _register ( self , callback ) : <EOL> for name , _ in self . _settings . values ( ) : <EOL> self . _sobj . add_on_change ( name , callback ) <EOL> def _unregister ( self ) : <EOL> for name , _ in self . _settings . values ( ) : <EOL> self . _sobj . clear_on_change ( name ) <EOL> def has_changed ( self ) : <EOL> return self . get_state ( ) != self . get_real_state ( ) <EOL> def get_state ( self ) : <EOL> return dict ( ( name , getattr ( self , attr ) ) <EOL> for attr , ( name , _ ) in self . _settings . items ( ) ) <EOL> def get_real_state ( self ) : <EOL> return dict ( ( name , self . _sobj . get ( name , def_value ) ) <EOL> for name , def_value in self . _settings . values ( ) ) <EOL> def set_callback ( self , callback , auto_update = True ) : <EOL> if callback is not None and not callable ( callback ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> cb = self . _callback <EOL> self . _callback = callback <EOL> if ( not cb and not self . _auto_update ) and ( callback or auto_update ) : <EOL> self . _register ( self . _on_change ) <EOL> self . _auto_update = auto_update <EOL> return cb <EOL> def clear_callback ( self , clear_auto_update = False ) : <EOL> cb = self . _callback <EOL> if cb or not self . _auto_update or clear_auto_update : <EOL> self . _unregister ( ) <EOL> self . _callback = None <EOL> return cb <EOL> class InsertDateCommand ( sublime_plugin . TextCommand ) : <EOL> """<STR_LIT>""" <EOL> def run ( self , edit , format = None , tz_in = None , tz_out = None ) : <EOL> if format is not None : <EOL> if format == '<STR_LIT>' or not isinstance ( format , basestring ) or format . isspace ( ) : <EOL> return <EOL> try : <EOL> text = fdate . parse ( format , tz_in , tz_out ) <EOL> except UnknownTimeZoneError as e : <EOL> status ( str ( e ) . strip ( '<STR_LIT:">' ) , e ) <EOL> return <EOL> except Exception as e : <EOL> status ( "<STR_LIT>" % format , e ) <EOL> return <EOL> if not text or text . isspace ( ) : <EOL> return <EOL> for r in self . view . sel ( ) : <EOL> if r . empty ( ) : <EOL> self . view . insert ( edit , r . a , text ) <EOL> else : <EOL> self . view . replace ( edit , r , text ) <EOL> class InsertDatePromptCommand ( sublime_plugin . TextCommand ) : <EOL> """<STR_LIT>""" <EOL> def run ( self , edit , format = None , tz_in = None , tz_out = None ) : <EOL> self . tz_in = tz_in <EOL> self . tz_out = tz_out <EOL> self . view . settings ( ) . set ( '<STR_LIT>' , False ) <EOL> i_panel = self . view . window ( ) . show_input_panel ( <EOL> "<STR_LIT>" , <EOL> format or fdate . default [ '<STR_LIT>' ] , <EOL> self . on_format , <EOL> None , <EOL> lambda : self . view . settings ( ) . erase ( '<STR_LIT>' ) <EOL> ) <EOL> i_panel . sel ( ) . clear ( ) <EOL> i_panel . sel ( ) . add ( sublime . Region ( <NUM_LIT:0> , i_panel . size ( ) ) ) <EOL> def on_format ( self , fmt ) : <EOL> global s <EOL> if fmt : <EOL> self . format = fmt <EOL> if self . tz_out : <EOL> self . run_for_real ( ) <EOL> else : <EOL> sublime . status_message ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> show_timezone_quickpanel ( self . on_tz_out , self . tz_in or s . tz_in ) <EOL> def on_tz_out ( self , index ) : <EOL> if index != - <NUM_LIT:1> : <EOL> self . tz_out = pytz . all_timezones [ index ] <EOL> self . run_for_real ( ) <EOL> def run_for_real ( self ) : <EOL> self . view . settings ( ) . erase ( '<STR_LIT>' ) <EOL> self . view . run_command ( <EOL> '<STR_LIT>' , <EOL> { '<STR_LIT>' : self . format , '<STR_LIT>' : self . tz_in , '<STR_LIT>' : self . tz_out } <EOL> ) <EOL> class InsertDatePanelCommand ( sublime_plugin . TextCommand ) : <EOL> """<STR_LIT>""" <EOL> panel_cache = [ ] <EOL> config_map = { } <EOL> def run ( self , edit , tz_in = None , tz_out = None ) : <EOL> self . panel_cache = [ ] <EOL> self . config_map = { } <EOL> if not isinstance ( s . prompt_config , list ) : <EOL> status ( "<STR_LIT>" ) <EOL> return <EOL> configs = s . prompt_config <EOL> if not isinstance ( s . user_prompt_config , list ) : <EOL> status ( "<STR_LIT>" ) <EOL> else : <EOL> configs = configs + s . user_prompt_config <EOL> if not configs : <EOL> status ( "<STR_LIT>" ) <EOL> return <EOL> for conf in configs : <EOL> c = dict ( ) <EOL> c [ '<STR_LIT>' ] = tz_in if tz_in else conf . get ( '<STR_LIT>' ) <EOL> c [ '<STR_LIT>' ] = tz_out if tz_out else conf . get ( '<STR_LIT>' ) <EOL> c [ '<STR_LIT>' ] = conf . get ( '<STR_LIT>' ) <EOL> if isinstance ( c [ '<STR_LIT>' ] , basestring ) : <EOL> c [ '<STR_LIT>' ] = c [ '<STR_LIT>' ] . replace ( "<STR_LIT>" , fdate . default [ '<STR_LIT>' ] ) <EOL> try : <EOL> text = fdate . parse ( ** c ) <EOL> except UnknownTimeZoneError as e : <EOL> status ( str ( e ) . strip ( '<STR_LIT:">' ) , e ) <EOL> return <EOL> except Exception as e : <EOL> status ( '<STR_LIT>' % format , e ) <EOL> return <EOL> self . panel_cache . append ( [ conf [ '<STR_LIT:name>' ] , text ] ) <EOL> self . config_map [ conf [ '<STR_LIT:name>' ] ] = c <EOL> self . view . settings ( ) . set ( '<STR_LIT>' , False ) <EOL> self . view . window ( ) . show_quick_panel ( self . panel_cache , self . on_done ) <EOL> def on_done ( self , index ) : <EOL> self . view . settings ( ) . erase ( '<STR_LIT>' ) <EOL> if index == - <NUM_LIT:1> : <EOL> return <EOL> name = self . panel_cache [ index ] [ <NUM_LIT:0> ] <EOL> self . view . run_command ( '<STR_LIT>' , self . config_map [ name ] ) <EOL> class InsertDateSelectTimezone ( sublime_plugin . ApplicationCommand ) : <EOL> @ staticmethod <EOL> def on_select ( index ) : <EOL> global s <EOL> if index == - <NUM_LIT:1> : <EOL> return <EOL> timezone = pytz . all_timezones [ index ] <EOL> s . _sobj . set ( '<STR_LIT>' , timezone ) <EOL> s . _sobj . erase ( '<STR_LIT>' ) <EOL> sublime . save_settings ( '<STR_LIT>' ) <EOL> def run ( self ) : <EOL> global s <EOL> show_timezone_quickpanel ( self . on_select , s . tz_in ) <EOL> def plugin_loaded ( ) : <EOL> global s <EOL> s = Settings ( <EOL> sublime . load_settings ( '<STR_LIT>' ) , <EOL> settings = dict ( <EOL> format = ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> tz_in = ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> prompt_config = ( '<STR_LIT>' , [ ] ) , <EOL> user_prompt_config = ( '<STR_LIT>' , [ ] ) , <EOL> silence_timezone_request = None <EOL> ) <EOL> ) <EOL> def on_settings_changed ( initial = False ) : <EOL> global fdate <EOL> if not initial : <EOL> status ( "<STR_LIT>" ) <EOL> fdate . set_default ( s . get_state ( ) ) <EOL> on_settings_changed ( True ) <EOL> s . set_callback ( on_settings_changed ) <EOL> if s . tz_in == '<STR_LIT>' and not s . silence_timezone_request : <EOL> def request_timezone ( ) : <EOL> if sublime . ok_cancel_dialog ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> '<STR_LIT>' , <EOL> "<STR_LIT>" <EOL> ) : <EOL> sublime . run_command ( '<STR_LIT>' ) <EOL> else : <EOL> s . _sobj . set ( '<STR_LIT>' , True ) <EOL> sublime . save_settings ( '<STR_LIT>' ) <EOL> sublime . set_timeout ( request_timezone , <NUM_LIT> ) <EOL> def plugin_unloaded ( ) : <EOL> global s <EOL> if s : <EOL> s . clear_callback ( True ) <EOL> unload_handler = plugin_unloaded if ST2 else lambda : None <EOL> if ST2 : <EOL> plugin_loaded ( ) </s>
