<s> import json <EOL> import threading <EOL> import sublime <EOL> import sublime_plugin <EOL> import analytics <EOL> import uuid <EOL> from elasticsearch import Elasticsearch <EOL> from elasticsearch_connections import CustomHeadersConnection <EOL> from abc import ABCMeta , abstractmethod <EOL> from . . panel import IndexListPanel <EOL> from . . panel import DocTypeListPanel <EOL> from . . panel import SwitchServerListPanel <EOL> from . . panel import AnalyzerListPanel <EOL> from . . panel import ScriptListPanel <EOL> from . . panel import SearchTemplateListPanel <EOL> from . . panel import AliasListPanel <EOL> from . . panel import IndexTemplateListPanel <EOL> from . . panel import WarmerListPanel <EOL> from . . panel import FieldListPanel <EOL> from . . panel import RepositoryListPanel <EOL> from . . panel import SnapshotListPanel <EOL> ANALYTICS_WRITE_KEY = "<STR_LIT>" <EOL> def track_command ( user_id , command_name ) : <EOL> analytics . write_key = ANALYTICS_WRITE_KEY <EOL> analytics . identify ( user_id ) <EOL> analytics . track ( user_id , "<STR_LIT>" , { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:label>" : command_name , <EOL> } ) <EOL> def track_activate ( user_id ) : <EOL> analytics . write_key = ANALYTICS_WRITE_KEY <EOL> analytics . identify ( user_id ) <EOL> analytics . track ( user_id , "<STR_LIT>" , { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:label>" : sublime . platform ( ) , <EOL> } ) <EOL> class Settings ( object ) : <EOL> SETTINGS_FILE = '<STR_LIT>' <EOL> def __init__ ( self ) : <EOL> self . settings = sublime . load_settings ( self . SETTINGS_FILE ) <EOL> @ property <EOL> def base_url ( self ) : <EOL> base_url = self . settings . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if base_url . endswith ( "<STR_LIT:/>" ) : <EOL> return base_url [ : - <NUM_LIT:1> ] <EOL> return base_url <EOL> @ property <EOL> def index ( self ) : <EOL> return self . settings . get ( "<STR_LIT:index>" , "<STR_LIT>" ) <EOL> @ property <EOL> def doc_type ( self ) : <EOL> return self . settings . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> @ property <EOL> def scroll_size ( self ) : <EOL> return self . settings . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> @ property <EOL> def headers ( self ) : <EOL> return self . settings . get ( "<STR_LIT>" , { } ) <EOL> @ property <EOL> def servers ( self ) : <EOL> def _normalize_servers ( servers ) : <EOL> items = [ ] <EOL> for name , server in servers . items ( ) : <EOL> server [ "<STR_LIT:name>" ] = name <EOL> items . append ( server ) <EOL> servers = sorted ( items , key = lambda k : k [ "<STR_LIT:name>" ] ) <EOL> return servers <EOL> servers = self . settings . get ( "<STR_LIT>" , [ ] ) <EOL> if isinstance ( servers , dict ) : <EOL> servers = _normalize_servers ( servers ) <EOL> return servers <EOL> @ property <EOL> def active_server ( self ) : <EOL> return dict ( <EOL> base_url = self . base_url , <EOL> index = self . index , <EOL> doc_type = self . doc_type , <EOL> scroll_size = self . scroll_size , <EOL> ) <EOL> @ property <EOL> def ab_command ( self ) : <EOL> return self . settings . get ( "<STR_LIT>" ) <EOL> @ property <EOL> def ab_requests ( self ) : <EOL> return str ( self . settings . get ( "<STR_LIT>" ) ) <EOL> @ property <EOL> def ab_concurrency ( self ) : <EOL> return str ( self . settings . get ( "<STR_LIT>" ) ) <EOL> @ property <EOL> def analytics ( self ) : <EOL> return self . settings . get ( "<STR_LIT>" , True ) <EOL> @ property <EOL> def user_id ( self ) : <EOL> return self . settings . get ( "<STR_LIT>" , None ) <EOL> @ property <EOL> def dump_file ( self ) : <EOL> return self . settings . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> @ property <EOL> def chunk_size ( self ) : <EOL> return self . settings . get ( "<STR_LIT>" , <NUM_LIT> ) <EOL> def set ( self , key , value ) : <EOL> self . settings . set ( key , value ) <EOL> def save ( self ) : <EOL> sublime . save_settings ( self . SETTINGS_FILE ) <EOL> class BaseCommand ( sublime_plugin . WindowCommand ) : <EOL> __metaclass__ = ABCMeta <EOL> command_name = None <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . settings = Settings ( ) <EOL> sublime_plugin . WindowCommand . __init__ ( self , * args , ** kwargs ) <EOL> @ property <EOL> def view ( self ) : <EOL> return self . window . active_view ( ) <EOL> def is_valid_json ( self ) : <EOL> try : <EOL> json . loads ( self . get_text ( ) ) <EOL> except ValueError : <EOL> return False <EOL> return True <EOL> def is_enabled ( self ) : <EOL> return self . is_valid_json ( ) <EOL> def get_text ( self ) : <EOL> return self . view . substr ( sublime . Region ( <NUM_LIT:0> , self . view . size ( ) ) ) <EOL> def init_client ( self ) : <EOL> self . _client = Elasticsearch ( <EOL> self . settings . base_url , <EOL> send_get_body_as = '<STR_LIT:POST>' , <EOL> connection_class = CustomHeadersConnection , <EOL> headers = self . settings . headers <EOL> ) <EOL> return self . _client <EOL> def save_settings ( self ) : <EOL> self . settings . save ( ) <EOL> self . init_client ( ) <EOL> @ property <EOL> def client ( self ) : <EOL> return self . init_client ( ) <EOL> def track_command ( self ) : <EOL> if self . settings . analytics : <EOL> user_id = self . settings . user_id <EOL> if not user_id : <EOL> user_id = str ( uuid . uuid4 ( ) ) <EOL> self . settings . set ( "<STR_LIT>" , user_id ) <EOL> self . settings . save ( ) <EOL> track_activate ( user_id ) <EOL> track_command ( user_id , self . command_name ) <EOL> def show_input_panel ( self , label , default , callback ) : <EOL> self . window . show_input_panel ( label , default , callback , None , None ) <EOL> def show_response ( self , response , title = "<STR_LIT>" ) : <EOL> title = title or self . command_name <EOL> text = json . dumps ( response , indent = <NUM_LIT:2> , ensure_ascii = False ) <EOL> self . window . run_command ( <EOL> "<STR_LIT>" , { "<STR_LIT:title>" : title , "<STR_LIT:text>" : text } ) <EOL> def show_index_list_panel ( self , callback ) : <EOL> list_panel = IndexListPanel ( <EOL> self . window , self . client , self . settings . index ) <EOL> list_panel . show ( callback ) <EOL> def show_doc_type_list_panel ( self , callback ) : <EOL> list_panel = DocTypeListPanel ( <EOL> self . window , self . client , self . settings . index ) <EOL> list_panel . show ( callback ) <EOL> def show_analyzer_list_panel ( self , callback ) : <EOL> list_panel = AnalyzerListPanel ( <EOL> self . window , self . client , self . settings . index ) <EOL> list_panel . show ( callback ) <EOL> def show_switch_server_list_panel ( self , callback ) : <EOL> list_panel = SwitchServerListPanel ( self . window , self . settings . servers ) <EOL> list_panel . show ( callback ) <EOL> def show_script_list_panel ( self , callback ) : <EOL> list_panel = ScriptListPanel ( self . window , self . client ) <EOL> list_panel . show ( callback ) <EOL> def show_search_template_list_panel ( self , callback ) : <EOL> list_panel = SearchTemplateListPanel ( self . window , self . client ) <EOL> list_panel . show ( callback ) <EOL> def show_alias_list_panel ( self , callback ) : <EOL> list_panel = AliasListPanel ( <EOL> self . window , self . client , self . settings . index ) <EOL> list_panel . show ( callback ) <EOL> def show_index_template_list_panel ( self , callback ) : <EOL> list_panel = IndexTemplateListPanel ( self . window , self . client ) <EOL> list_panel . show ( callback ) <EOL> def show_warmer_list_panel ( self , callback ) : <EOL> list_panel = WarmerListPanel ( <EOL> self . window , self . client , self . settings . index ) <EOL> list_panel . show ( callback ) <EOL> def show_field_list_panel ( self , callback ) : <EOL> list_panel = FieldListPanel ( <EOL> self . window , self . client , <EOL> self . settings . index , self . settings . doc_type ) <EOL> list_panel . show ( callback ) <EOL> def show_repository_list_panel ( self , callback ) : <EOL> list_panel = RepositoryListPanel ( self . window , self . client ) <EOL> list_panel . show ( callback ) <EOL> def show_snapshot_list_panel ( self , repository , callback ) : <EOL> list_panel = SnapshotListPanel ( self . window , self . client , repository ) <EOL> list_panel . show ( callback ) <EOL> def show_output_panel ( self , text , syntax = None ) : <EOL> self . window . run_command ( <EOL> "<STR_LIT>" , { "<STR_LIT:text>" : text , "<STR_LIT>" : syntax } ) <EOL> def show_object_output_panel ( self , obj ) : <EOL> options = dict ( <EOL> indent = <NUM_LIT:4> , <EOL> ensure_ascii = False <EOL> ) <EOL> self . show_output_panel ( <EOL> json . dumps ( obj , ** options ) , <EOL> syntax = "<STR_LIT>" ) <EOL> def show_active_server ( self ) : <EOL> self . window . run_command ( "<STR_LIT>" ) <EOL> @ abstractmethod <EOL> def run_request ( self , * args , ** kwargs ) : <EOL> raise NotImplementedError ( ) <EOL> def run_request_wrapper ( self , * args , ** kwargs ) : <EOL> try : <EOL> response = self . run_request ( * args , ** kwargs ) <EOL> except Exception as e : <EOL> sublime . error_message ( "<STR_LIT>" . format ( e ) ) <EOL> return <EOL> if response is not None : <EOL> self . show_response ( response ) <EOL> self . track_command ( ) <EOL> def request_thread ( self , * args , ** kwargs ) : <EOL> thread = threading . Thread ( <EOL> target = self . run_request_wrapper , args = args , kwargs = kwargs ) <EOL> thread . start ( ) <EOL> def run ( self , * args , ** kwargs ) : <EOL> self . request_thread ( * args , ** kwargs ) <EOL> class CreateBaseCommand ( BaseCommand ) : <EOL> def run_request_wrapper ( self , * args , ** kwargs ) : <EOL> try : <EOL> response = self . run_request ( * args , ** kwargs ) <EOL> except Exception as e : <EOL> sublime . error_message ( "<STR_LIT>" . format ( e ) ) <EOL> return <EOL> if response is not None : <EOL> self . show_object_output_panel ( response ) <EOL> self . track_command ( ) <EOL> class DeleteBaseCommand ( CreateBaseCommand ) : <EOL> pass <EOL> class CatBaseCommand ( CreateBaseCommand ) : <EOL> def is_enabled ( self ) : <EOL> return True <EOL> def run_request_wrapper ( self , * args , ** kwargs ) : <EOL> try : <EOL> response = self . run_request ( * args , ** kwargs ) <EOL> except Exception as e : <EOL> sublime . error_message ( "<STR_LIT>" . format ( e ) ) <EOL> return <EOL> if response is not None : <EOL> self . show_output_panel ( response ) <EOL> self . track_command ( ) <EOL> class SearchBaseCommand ( BaseCommand ) : <EOL> def extend_options ( self , options , search_type = None ) : <EOL> if search_type : <EOL> self . command_name = "<STR_LIT>" . format ( <EOL> base = self . command_name , <EOL> search_type = search_type . lower ( ) <EOL> ) <EOL> if search_type == "<STR_LIT>" : <EOL> options [ "<STR_LIT>" ] = dict ( <EOL> search_type = search_type , <EOL> scroll = self . settings . scroll_size <EOL> ) <EOL> elif search_type is not None : <EOL> options [ "<STR_LIT>" ] = dict ( <EOL> search_type = search_type <EOL> ) <EOL> return options <EOL> class SettingsBaseCommand ( BaseCommand ) : <EOL> def is_enabled ( self ) : <EOL> return True </s>
