<s> import sublime <EOL> import sublime_plugin <EOL> import urllib . request <EOL> import threading <EOL> import json <EOL> import os <EOL> import sys <EOL> import subprocess <EOL> import locale <EOL> import re <EOL> import shlex <EOL> if os . name == '<STR_LIT>' : <EOL> plat = '<STR_LIT>' <EOL> elif sys . platform == '<STR_LIT>' : <EOL> plat = '<STR_LIT>' <EOL> else : <EOL> plat = '<STR_LIT>' <EOL> if plat == '<STR_LIT>' : <EOL> try : <EOL> import _winreg <EOL> except ( ImportError ) : <EOL> import winreg as _winreg <EOL> from ctypes import windll , create_unicode_buffer <EOL> class NotFoundError ( Exception ) : <EOL> pass <EOL> if sys . version_info >= ( <NUM_LIT:3> , ) : <EOL> installed_dir , _ = __name__ . split ( '<STR_LIT:.>' ) <EOL> else : <EOL> installed_dir = os . path . basename ( os . getcwd ( ) ) <EOL> class KTerminalSelector ( ) : <EOL> default = None <EOL> @ staticmethod <EOL> def get ( ) : <EOL> package_dir = os . path . join ( sublime . packages_path ( ) , installed_dir ) <EOL> default = None <EOL> if plat == '<STR_LIT>' : <EOL> if os . path . exists ( os . environ [ '<STR_LIT>' ] + <EOL> '<STR_LIT>' ) : <EOL> key_string = '<STR_LIT>' + '<STR_LIT>' <EOL> try : <EOL> key = _winreg . OpenKey ( _winreg . HKEY_CURRENT_USER , <EOL> key_string ) <EOL> except ( WindowsError ) : <EOL> key = _winreg . CreateKey ( _winreg . HKEY_CURRENT_USER , <EOL> key_string ) <EOL> _winreg . SetValueEx ( key , '<STR_LIT>' , <NUM_LIT:0> , <EOL> _winreg . REG_DWORD , <NUM_LIT> ) <EOL> _winreg . SetValueEx ( key , '<STR_LIT>' , <NUM_LIT:0> , <EOL> _winreg . REG_DWORD , <NUM_LIT> ) <EOL> default = os . path . join ( package_dir , '<STR_LIT>' ) <EOL> sublime_terminal_path = os . path . join ( sublime . packages_path ( ) , installed_dir ) <EOL> buf = create_unicode_buffer ( <NUM_LIT> ) <EOL> if windll . kernel32 . GetShortPathNameW ( sublime_terminal_path , buf , len ( buf ) ) : <EOL> sublime_terminal_path = buf . value <EOL> os . putenv ( '<STR_LIT>' , sublime_terminal_path . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) ) <EOL> else : <EOL> default = os . environ [ '<STR_LIT>' ] + '<STR_LIT>' <EOL> elif plat == '<STR_LIT>' : <EOL> default = os . path . join ( package_dir , '<STR_LIT>' ) <EOL> if not os . access ( default , os . X_OK ) : <EOL> os . chmod ( default , <NUM_LIT> ) <EOL> else : <EOL> ps = '<STR_LIT>' + '<STR_LIT>' <EOL> wm = [ x . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) for x in os . popen ( ps ) ] <EOL> if wm : <EOL> if wm [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> default = '<STR_LIT>' <EOL> elif wm [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> default = '<STR_LIT>' <EOL> elif wm [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> default = '<STR_LIT>' <EOL> if not default : <EOL> default = '<STR_LIT>' <EOL> KTerminalSelector . default = default <EOL> return default <EOL> class KTerminalCommand ( ) : <EOL> def get_path ( self , paths ) : <EOL> if paths : <EOL> return paths [ <NUM_LIT:0> ] <EOL> elif self . window . active_view ( ) : <EOL> return self . window . active_view ( ) . file_name ( ) <EOL> elif self . window . folders ( ) : <EOL> return self . window . folders ( ) [ <NUM_LIT:0> ] <EOL> else : <EOL> sublime . error_message ( '<STR_LIT>' ) <EOL> return False <EOL> def run_terminal ( self , dir_ , parameters ) : <EOL> try : <EOL> if not dir_ : <EOL> raise NotFoundError ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) <EOL> pathToProjJson = self . findProjectJsonFile ( ) <EOL> print ( '<STR_LIT>' + pathToProjJson ) <EOL> dir_ = os . path . dirname ( self . findProjectJsonFile ( ) ) <EOL> for k , v in enumerate ( parameters ) : <EOL> parameters [ k ] = v . replace ( '<STR_LIT>' , dir_ ) <EOL> args = [ KTerminalSelector . get ( ) ] <EOL> args . extend ( parameters ) <EOL> if args [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> args = shlex . split ( args [ <NUM_LIT:0> ] + '<STR_LIT>' + args [ <NUM_LIT:1> ] + '<STR_LIT:">' ) <EOL> elif args [ <NUM_LIT:0> ] in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> args = shlex . split ( args [ <NUM_LIT:0> ] + '<STR_LIT>' + args [ <NUM_LIT:1> ] + '<STR_LIT:">' ) <EOL> elif args [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> args = shlex . split ( args [ <NUM_LIT:0> ] + '<STR_LIT>' + dir_ + '<STR_LIT>' + args [ <NUM_LIT:1> ] ) <EOL> encoding = locale . getpreferredencoding ( do_setlocale = True ) <EOL> if sys . version_info >= ( <NUM_LIT:3> , ) : <EOL> cwd = dir_ <EOL> else : <EOL> cwd = dir_ . encode ( encoding ) <EOL> print ( args ) <EOL> if plat == '<STR_LIT>' : <EOL> startupinfo = subprocess . STARTUPINFO ( ) <EOL> startupinfo . dwFlags |= subprocess . STARTF_USESHOWWINDOW <EOL> subprocess . Popen ( args , cwd = cwd , startupinfo = startupinfo ) <EOL> elif plat == '<STR_LIT>' : <EOL> subprocess . Popen ( args , cwd = cwd ) <EOL> else : <EOL> subprocess . Popen ( args , cwd = cwd ) <EOL> except ( OSError ) as exception : <EOL> print ( str ( exception ) ) <EOL> sublime . error_message ( '<STR_LIT>' + <EOL> KTerminalSelector . get ( ) + '<STR_LIT>' ) <EOL> except ( Exception ) as exception : <EOL> sublime . error_message ( '<STR_LIT>' + str ( exception ) ) <EOL> def findProjectJsonFile ( self ) : <EOL> currentFile = self . window . active_view ( ) . file_name ( ) <EOL> if not currentFile : <EOL> msg = '<STR_LIT>' <EOL> sublime . error_message ( msg ) <EOL> raise NotFoundError ( msg ) <EOL> sublimeProjectFile = self . window . active_view ( ) . window ( ) . project_file_name ( ) <EOL> if ( sublimeProjectFile ) : <EOL> sublimeProjFileDir = os . path . dirname ( sublimeProjectFile ) <EOL> expectedPathToProjeJsonFromProj = os . path . join ( sublimeProjFileDir , '<STR_LIT>' ) <EOL> if ( os . path . isfile ( expectedPathToProjeJsonFromProj ) ) : <EOL> msg = '<STR_LIT>' + expectedPathToProjeJsonFromProj + '<STR_LIT:]>' <EOL> print ( msg ) <EOL> sublime . status_message ( msg ) <EOL> return expectedPathToProjeJsonFromProj <EOL> msg = '<STR_LIT>' <EOL> print ( msg ) <EOL> sublime . status_message ( msg ) <EOL> currentDir = os . path . dirname ( currentFile ) <EOL> pathToCheck = os . path . join ( currentDir , '<STR_LIT>' ) <EOL> previousPath = '<STR_LIT>' <EOL> counter = <NUM_LIT:0> <EOL> while counter < <NUM_LIT:100> : <EOL> counter += <NUM_LIT:1> <EOL> if ( previousPath == pathToCheck ) : <EOL> break <EOL> print ( '<STR_LIT>' + pathToCheck + '<STR_LIT:]>' ) <EOL> if os . path . isfile ( pathToCheck ) : <EOL> print ( '<STR_LIT>' + pathToCheck + '<STR_LIT:]>' ) <EOL> return pathToCheck <EOL> previousPath = pathToCheck <EOL> parentDir = os . path . abspath ( os . path . join ( os . path . dirname ( pathToCheck ) , os . pardir ) ) <EOL> pathToCheck = os . path . join ( parentDir , '<STR_LIT>' ) <EOL> sublime . error_message ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> return <EOL> class KOpenTerminalCommand ( sublime_plugin . WindowCommand , KTerminalCommand ) : <EOL> def run ( self , paths = [ ] , parameters = None ) : <EOL> path = self . get_path ( paths ) <EOL> if not path : <EOL> return <EOL> if parameters == None : <EOL> settings = sublime . load_settings ( '<STR_LIT>' ) <EOL> parameters = settings . get ( '<STR_LIT>' ) <EOL> if not parameters : <EOL> parameters = [ ] <EOL> if os . path . isfile ( path ) : <EOL> path = os . path . dirname ( path ) <EOL> self . run_terminal ( path , parameters ) <EOL> class KRunCommand ( sublime_plugin . WindowCommand ) : <EOL> def run ( self ) : <EOL> file_name = self . findProjectJsonFile ( ) <EOL> if file_name is None : <EOL> print ( '<STR_LIT>' + self . window . active_view ( ) . file_name ( ) + '<STR_LIT:]>' ) <EOL> return <EOL> try : <EOL> json_file = json . load ( open ( file_name ) ) <EOL> except UnicodeDecodeError : <EOL> msg = '<STR_LIT>' + file_name + '<STR_LIT>' <EOL> print ( msg ) <EOL> sublime . error_message ( msg ) <EOL> retrun <EOL> except IOError : <EOL> print ( '<STR_LIT>' ) <EOL> return <EOL> self . commands = [ ] <EOL> try : <EOL> json_commands = json_file [ '<STR_LIT>' ] <EOL> for command in json_commands : <EOL> args = json_commands [ command ] <EOL> if command . lower ( ) == '<STR_LIT>' or command . lower ( ) == '<STR_LIT>' : <EOL> url_regex = r'<STR_LIT>' <EOL> match = re . search ( url_regex , args ) <EOL> try : <EOL> url = match . group ( <NUM_LIT:1> ) <EOL> except AttributeError as e : <EOL> url = '<STR_LIT>' <EOL> self . commands . append ( [ '<STR_LIT>' + command , '<STR_LIT>' + url ] ) <EOL> else : <EOL> self . commands . append ( [ '<STR_LIT>' + command , args ] ) <EOL> except LookupError : <EOL> pass <EOL> self . commands . append ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . commands . append ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . commands . append ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . window . show_quick_panel ( self . commands , self . commandlist ) <EOL> def commandlist ( self , position ) : <EOL> if ( position > - <NUM_LIT:1> ) : <EOL> self . window . run_command ( '<STR_LIT>' , { '<STR_LIT>' : [ self . commands [ position ] [ <NUM_LIT:0> ] ] } ) <EOL> i = <NUM_LIT:0> <EOL> else : <EOL> return <EOL> def findProjectJsonFile ( self ) : <EOL> currentFile = self . window . active_view ( ) . file_name ( ) <EOL> if not currentFile : <EOL> msg = '<STR_LIT>' <EOL> sublime . error_message ( msg ) <EOL> raise NotFoundError ( msg ) <EOL> sublimeProjectFile = self . window . active_view ( ) . window ( ) . project_file_name ( ) <EOL> if ( sublimeProjectFile ) : <EOL> sublimeProjFileDir = os . path . dirname ( sublimeProjectFile ) <EOL> expectedPathToProjeJsonFromProj = os . path . join ( sublimeProjFileDir , '<STR_LIT>' ) <EOL> if ( os . path . isfile ( expectedPathToProjeJsonFromProj ) ) : <EOL> msg = '<STR_LIT>' + expectedPathToProjeJsonFromProj + '<STR_LIT:]>' <EOL> print ( msg ) <EOL> sublime . status_message ( msg ) <EOL> return expectedPathToProjeJsonFromProj <EOL> msg = '<STR_LIT>' <EOL> print ( msg ) <EOL> sublime . status_message ( msg ) <EOL> currentDir = os . path . dirname ( currentFile ) <EOL> pathToCheck = os . path . join ( currentDir , '<STR_LIT>' ) <EOL> previousPath = '<STR_LIT>' <EOL> counter = <NUM_LIT:0> <EOL> while counter < <NUM_LIT:100> : <EOL> counter += <NUM_LIT:1> <EOL> if ( previousPath == pathToCheck ) : <EOL> break <EOL> print ( '<STR_LIT>' + pathToCheck + '<STR_LIT:]>' ) <EOL> if os . path . isfile ( pathToCheck ) : <EOL> print ( '<STR_LIT>' + pathToCheck + '<STR_LIT:]>' ) <EOL> return pathToCheck <EOL> previousPath = pathToCheck <EOL> parentDir = os . path . abspath ( os . path . join ( os . path . dirname ( pathToCheck ) , os . pardir ) ) <EOL> pathToCheck = os . path . join ( parentDir , '<STR_LIT>' ) <EOL> sublime . error_message ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> return <EOL> class RetrievePackageNames ( threading . Thread ) : <EOL> def __init__ ( self , timeout ) : <EOL> self . timeout = timeout <EOL> self . result = None <EOL> threading . Thread . __init__ ( self ) <EOL> def run ( self ) : <EOL> try : <EOL> request = urllib . request . Request ( '<STR_LIT>' , <EOL> headers = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> http_file = urllib . request . urlopen ( request , timeout = self . timeout ) <EOL> self . result = [ ] <EOL> response = json . loads ( http_file . read ( ) . decode ( '<STR_LIT:utf-8>' ) ) [ '<STR_LIT:d>' ] <EOL> for package in response : <EOL> self . result . append ( package [ '<STR_LIT>' ] ) <EOL> print ( self . result ) <EOL> return <EOL> except ( urllib . request . HTTPError ) as e : <EOL> self . message = '<STR_LIT>' % ( __name__ , str ( e . code ) ) <EOL> except ( urllib . request . URLError ) as e : <EOL> self . message = '<STR_LIT>' % ( __name__ , str ( e . reason ) ) <EOL> self . result = False <EOL> return </s>
