<s> import sublime <EOL> import sublime_plugin <EOL> import threading <EOL> import urllib . request , urllib . parse <EOL> import json <EOL> import re <EOL> SETTINGS_FILE = '<STR_LIT>' <EOL> AC_OPTS = sublime . INHIBIT_WORD_COMPLETIONS | sublime . INHIBIT_EXPLICIT_COMPLETIONS <EOL> class KKultureComplete ( sublime_plugin . TextCommand ) : <EOL> def run ( self , edit , action = None , ** args ) : <EOL> print ( "<STR_LIT>" ) <EOL> self . view . insert ( edit , self . view . sel ( ) [ <NUM_LIT:0> ] . begin ( ) , args [ '<STR_LIT:key>' ] ) <EOL> self . view . run_command ( '<STR_LIT>' ) <EOL> return True <EOL> class KKultureCompletion ( sublime_plugin . EventListener ) : <EOL> def __init__ ( self ) : <EOL> self . result = { } <EOL> self . cache = [ ] <EOL> self . thread = RetrievePackageNames ( <NUM_LIT:5> ) <EOL> self . thread . start ( ) <EOL> while ( self . thread . is_alive ( ) ) : <EOL> pass <EOL> response = self . thread . response <EOL> for package in response : <EOL> t = ( package [ '<STR_LIT>' ] , package [ '<STR_LIT>' ] ) <EOL> regex = r'<STR_LIT>' <EOL> match = re . search ( regex , package [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> try : <EOL> if ( match . group ( <NUM_LIT:1> ) , match . group ( <NUM_LIT:1> ) ) not in self . cache : <EOL> self . cache . append ( ( match . group ( <NUM_LIT:1> ) , match . group ( <NUM_LIT:1> ) ) ) <EOL> try : <EOL> self . result [ match . group ( <NUM_LIT:1> ) ] . append ( ( match . group ( <NUM_LIT:2> ) , match . group ( <NUM_LIT:2> ) ) ) <EOL> except KeyError as e : <EOL> self . result [ match . group ( <NUM_LIT:1> ) ] = [ ] <EOL> self . result [ match . group ( <NUM_LIT:1> ) ] . append ( ( match . group ( <NUM_LIT:2> ) , match . group ( <NUM_LIT:2> ) ) ) <EOL> except AttributeError as e : <EOL> pass <EOL> def on_query_completions ( self , view , prefix , locations ) : <EOL> pos = locations [ <NUM_LIT:0> ] <EOL> scopes = view . scope_name ( pos ) . split ( ) <EOL> if "<STR_LIT>" not in scopes : <EOL> return [ ] <EOL> else : <EOL> doc = view . substr ( sublime . Region ( <NUM_LIT:0> , view . size ( ) ) ) <EOL> whitespaces = <NUM_LIT:0> ; <EOL> for index , char in enumerate ( doc [ <NUM_LIT:0> : pos ] ) : <EOL> if char == '<STR_LIT:\n>' or char == '<STR_LIT:\r>' or char == '<STR_LIT:U+0020>' or char == '<STR_LIT:\t>' : <EOL> whitespaces += <NUM_LIT:1> <EOL> doc = doc . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) . replace ( '<STR_LIT:\r>' , '<STR_LIT>' ) . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) . replace ( '<STR_LIT:\t>' , '<STR_LIT>' ) <EOL> pos = pos - whitespaces <EOL> depth = - <NUM_LIT:1> <EOL> tokens = [ ] <EOL> token_regex = re . compile ( r'<STR_LIT>' ) <EOL> for index , char in enumerate ( doc ) : <EOL> if char == '<STR_LIT:{>' : <EOL> depth += <NUM_LIT:1> <EOL> token_regex = r'<STR_LIT>' <EOL> match = re . search ( token_regex , doc [ <NUM_LIT:0> : index + <NUM_LIT:1> ] ) <EOL> try : <EOL> tokens . append ( match . group ( <NUM_LIT:1> ) ) <EOL> except AttributeError as e : <EOL> pass <EOL> if char == '<STR_LIT:}>' : <EOL> depth -= <NUM_LIT:1> <EOL> try : <EOL> tokens . pop ( ) <EOL> except IndexError as e : <EOL> pass <EOL> if index == pos : <EOL> if ( depth == <NUM_LIT:1> and tokens [ <NUM_LIT:0> ] == '<STR_LIT>' ) : <EOL> version_regex = r'<STR_LIT>' <EOL> try : <EOL> package_name = re . search ( version_regex , doc [ <NUM_LIT:0> : index ] ) . group ( <NUM_LIT:1> ) <EOL> return ( self . result [ package_name ] , AC_OPTS ) <EOL> except AttributeError as e : <EOL> pass <EOL> except KeyError as e : <EOL> return AC_OPTS <EOL> return ( self . cache , AC_OPTS ) <EOL> elif depth == <NUM_LIT:0> : <EOL> return ( [ ( '<STR_LIT:version>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:description>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:code>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ] , AC_OPTS ) <EOL> else : <EOL> break <EOL> return AC_OPTS <EOL> class RetrievePackageNames ( threading . Thread ) : <EOL> def __init__ ( self , timeout ) : <EOL> self . timeout = timeout <EOL> self . response = None <EOL> threading . Thread . __init__ ( self ) <EOL> def run ( self ) : <EOL> try : <EOL> request = urllib . request . Request ( "<STR_LIT>" <EOL> + "<STR_LIT>" <EOL> + "<STR_LIT>" <EOL> + "<STR_LIT>" <EOL> + "<STR_LIT>" , <EOL> headers = { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> http_file = urllib . request . urlopen ( request , timeout = self . timeout ) <EOL> self . response = json . loads ( http_file . read ( ) . decode ( '<STR_LIT:utf-8>' ) ) [ '<STR_LIT:d>' ] <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 . response = False <EOL> return </s>
