<s> import sublime , sublime_plugin <EOL> import subprocess <EOL> import webbrowser <EOL> from os import path <EOL> gte_st3 = int ( sublime . version ( ) ) >= <NUM_LIT> <EOL> if gte_st3 : <EOL> from . config import * <EOL> else : <EOL> from config import * <EOL> class HiveOpenCommand ( sublime_plugin . WindowCommand ) : <EOL> def run ( self , ** args ) : <EOL> self . init ( ** args ) <EOL> self . show_quick_panel ( ) <EOL> def init ( self , ** args ) : <EOL> options = sublime . load_settings ( OPTIONS_BASE_NAME ) <EOL> self . peek_file = gte_st3 and options . get ( '<STR_LIT>' , False ) <EOL> self . copy_url_on_open = options . get ( '<STR_LIT>' , False ) <EOL> self . binfile_open_in_subl = options . get ( '<STR_LIT>' , False ) <EOL> self . init_item_types ( args . get ( '<STR_LIT>' ) ) <EOL> self . init_item_data ( ) <EOL> if self . peek_file : self . save_view ( ) <EOL> def init_item_types ( self , item_types ) : <EOL> if not item_types : item_types = DEFAULT_ITEM_TYPES <EOL> self . item_types = list ( set ( item_types ) & set ( DEFAULT_ITEM_TYPES ) ) <EOL> def init_item_data ( self ) : <EOL> conf = sublime . load_settings ( CONFIG_BASE_NAME ) <EOL> self . items = [ ] <EOL> self . view_items = [ ] <EOL> for key in self . item_types : <EOL> if not conf . has ( key ) : continue <EOL> self . items . extend ( conf . get ( key ) ) <EOL> self . items . sort ( key = lambda x : x [ <NUM_LIT:1> ] . lower ( ) ) <EOL> for ( pathname , desc ) in self . items : <EOL> title = desc <EOL> if not title : <EOL> if self . isapp ( pathname ) or path . isfile ( pathname ) : <EOL> title = path . basename ( pathname ) or pathname <EOL> else : <EOL> title = pathname <EOL> subtitle = '<STR_LIT>' % ( self . get_item_type ( pathname ) , pathname ) <EOL> self . view_items . append ( [ title , subtitle ] ) <EOL> def get_item_type ( self , pathname ) : <EOL> if self . isurl ( pathname ) : return '<STR_LIT>' <EOL> if self . isapp ( pathname ) : return '<STR_LIT>' <EOL> if path . isfile ( pathname ) : <EOL> name , ext = path . splitext ( pathname ) <EOL> return ext [ <NUM_LIT:1> : ] . upper ( ) or '<STR_LIT>' <EOL> if path . isdir ( pathname ) : return '<STR_LIT>' <EOL> return '<STR_LIT>' <EOL> def show_quick_panel ( self ) : <EOL> if gte_st3 : <EOL> self . window . show_quick_panel ( self . view_items , self . on_done , on_highlight = self . on_highlight ) <EOL> else : <EOL> self . window . show_quick_panel ( self . view_items , self . on_done ) <EOL> def on_done ( self , index ) : <EOL> if self . peek_file : self . restore_view ( ) <EOL> if index == - <NUM_LIT:1> : return <EOL> item_name = self . get_name_by_index ( index ) <EOL> if self . isurl ( item_name ) : return self . open_url ( item_name ) <EOL> if self . isapp ( item_name ) : return self . open_app ( item_name ) <EOL> if path . isfile ( item_name ) : return self . open_file ( item_name ) <EOL> if path . isdir ( item_name ) : return self . open_dir ( item_name ) <EOL> sublime . status_message ( '<STR_LIT>' % item_name ) <EOL> def on_highlight ( self , index ) : <EOL> if not self . peek_file : return <EOL> item_name = self . get_name_by_index ( index ) <EOL> if path . isfile ( item_name ) : <EOL> self . open_file ( item_name , True ) <EOL> else : <EOL> self . restore_view ( ) <EOL> def open_url ( self , url ) : <EOL> if self . copy_url_on_open : <EOL> sublime . set_clipboard ( url ) <EOL> sublime . status_message ( '<STR_LIT>' % url ) <EOL> if url [ <NUM_LIT:0> : <NUM_LIT:3> ] == '<STR_LIT>' : url = '<STR_LIT>' + url <EOL> webbrowser . open_new_tab ( url ) <EOL> def open_app ( self , appname ) : <EOL> return subprocess . Popen ( [ '<STR_LIT>' , appname ] ) <EOL> def open_file ( self , filename , preview = False ) : <EOL> if preview : return self . window . open_file ( filename , sublime . TRANSIENT ) <EOL> name , ext = path . splitext ( filename ) <EOL> if self . binfile_open_in_subl or not self . is_binary_file ( filename ) : <EOL> self . open_file_inside_subl ( filename ) <EOL> else : <EOL> self . open_file_outside_subl ( filename ) <EOL> def open_file_inside_subl ( self , filename ) : <EOL> self . window . open_file ( filename ) <EOL> def open_file_outside_subl ( self , filename ) : <EOL> if SUBLIME_PLATFORM == '<STR_LIT>' : <EOL> subprocess . Popen ( [ '<STR_LIT>' , filename ] ) <EOL> if SUBLIME_PLATFORM == '<STR_LIT>' : <EOL> subprocess . Popen ( [ '<STR_LIT>' , filename ] ) <EOL> def open_dir ( self , dirname ) : <EOL> if SUBLIME_PLATFORM == '<STR_LIT>' : <EOL> subprocess . Popen ( [ '<STR_LIT>' , dirname ] ) <EOL> if SUBLIME_PLATFORM == '<STR_LIT>' : <EOL> self . window . run_command ( '<STR_LIT>' , { '<STR_LIT>' : dirname } ) <EOL> def is_binary_file ( self , filename ) : <EOL> textchars = bytearray ( [ <NUM_LIT:7> , <NUM_LIT:8> , <NUM_LIT:9> , <NUM_LIT:10> , <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT> ] ) + bytearray ( range ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> leadbytes = open ( filename , '<STR_LIT:rb>' ) . read ( <NUM_LIT> ) <EOL> return bool ( leadbytes . translate ( None , textchars ) ) <EOL> def isurl ( self , target ) : return bool ( REX_URL . match ( target ) ) <EOL> def isapp ( self , target ) : <EOL> if SUBLIME_PLATFORM != '<STR_LIT>' : return False <EOL> return path . splitext ( target ) [ <NUM_LIT:1> ] == '<STR_LIT>' <EOL> def get_name_by_index ( self , index ) : <EOL> return self . items [ index ] [ <NUM_LIT:0> ] <EOL> def save_view ( self ) : <EOL> self . saved_view = self . window . active_view ( ) <EOL> selection = self . saved_view . sel ( ) <EOL> self . saved_regions = [ ( region . a , region . b ) for region in selection ] <EOL> return None if not len ( selection ) else selection [ - <NUM_LIT:1> ] <EOL> def restore_view ( self ) : <EOL> saved_view = self . saved_view <EOL> if saved_view == None : return <EOL> sublime . Selection . clear ( saved_view ) <EOL> for region in self . saved_regions : <EOL> sublime . Selection . add ( saved_view , sublime . Region ( * region ) ) <EOL> self . window . focus_view ( saved_view ) </s>
