<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> import os <EOL> import sys <EOL> import struct <EOL> import argparse <EOL> import subprocess <EOL> import logging <EOL> log = logging . getLogger ( __name__ ) <EOL> log . addHandler ( logging . NullHandler ) <EOL> try : <EOL> import cPickle as pickle <EOL> except ImportError : <EOL> import pickle <EOL> jedi_path = os . path . join ( os . path . dirname ( os . path . dirname ( os . path . dirname ( __file__ ) ) ) , '<STR_LIT>' ) <EOL> _types = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:class>' : '<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>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:bool>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:list>' : '<STR_LIT>' , <EOL> '<STR_LIT:float>' : '<STR_LIT>' , <EOL> '<STR_LIT:int>' : '<STR_LIT>' , <EOL> '<STR_LIT:object>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:str>' : '<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> } <EOL> class StreamError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class StreamEmpty ( StreamError ) : <EOL> """<STR_LIT>""" <EOL> def stream_read ( pipe ) : <EOL> """<STR_LIT>""" <EOL> buffer = getattr ( pipe , '<STR_LIT>' , pipe ) <EOL> header = buffer . read ( <NUM_LIT:4> ) <EOL> if not header : <EOL> raise StreamEmpty <EOL> if len ( header ) < <NUM_LIT:4> : <EOL> raise StreamError ( '<STR_LIT>' ) <EOL> length = struct . unpack ( '<STR_LIT:I>' , header ) [ <NUM_LIT:0> ] <EOL> data = buffer . read ( length ) <EOL> if len ( data ) < length : <EOL> raise StreamError ( '<STR_LIT>' ) <EOL> return pickle . loads ( data ) <EOL> def stream_write ( pipe , obj ) : <EOL> """<STR_LIT>""" <EOL> data = pickle . dumps ( obj , <NUM_LIT:2> ) <EOL> header = struct . pack ( '<STR_LIT:I>' , len ( data ) ) <EOL> buffer = getattr ( pipe , '<STR_LIT>' , pipe ) <EOL> buffer . write ( header + data ) <EOL> pipe . flush ( ) <EOL> class Server ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , desc_len = <NUM_LIT:0> , short_types = False , show_docstring = False ) : <EOL> self . desc_len = desc_len <EOL> self . use_short_types = short_types <EOL> self . show_docstring = show_docstring <EOL> def _loop ( self ) : <EOL> import jedi <EOL> while True : <EOL> data = stream_read ( sys . stdin ) <EOL> if not isinstance ( data , tuple ) : <EOL> break <EOL> source , line , col , filename = data <EOL> log . debug ( '<STR_LIT>' , line , col , filename ) <EOL> completions = jedi . Script ( source , line , col , filename ) . completions ( ) <EOL> out = [ ] <EOL> tmp_filecache = { } <EOL> for c in completions : <EOL> name , type_ , desc , abbr = self . parse_completion ( c , tmp_filecache ) <EOL> kind = type_ if not self . use_short_types else _types . get ( type_ ) or type_ <EOL> out . append ( ( c . module_path , name , type_ , desc , abbr , kind ) ) <EOL> stream_write ( sys . stdout , out ) <EOL> def run ( self ) : <EOL> log . debug ( sys . path ) <EOL> try : <EOL> self . _loop ( ) <EOL> except StreamEmpty : <EOL> log . debug ( '<STR_LIT>' ) <EOL> except Exception : <EOL> log . exception ( '<STR_LIT>' ) <EOL> def call_signature ( self , comp ) : <EOL> """<STR_LIT>""" <EOL> params = [ ] <EOL> params_abbr = [ ] <EOL> try : <EOL> length = len ( comp . name ) <EOL> for i , p in enumerate ( comp . params ) : <EOL> desc = p . description . strip ( ) <EOL> if i == <NUM_LIT:0> and desc == '<STR_LIT>' : <EOL> continue <EOL> if '<STR_LIT>' in desc : <EOL> desc = desc . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> length += len ( desc ) + <NUM_LIT:2> <EOL> params . append ( desc ) <EOL> params_abbr = params [ : ] <EOL> if self . desc_len > <NUM_LIT:0> : <EOL> if length > self . desc_len : <EOL> params_abbr = [ x . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] for x in params_abbr ] <EOL> length = len ( comp . name ) + sum ( [ len ( x ) + <NUM_LIT:2> <EOL> for x in params_abbr ] ) <EOL> while length + <NUM_LIT:3> > self . desc_len and len ( params_abbr ) : <EOL> length -= len ( params_abbr [ - <NUM_LIT:1> ] ) - <NUM_LIT:3> <EOL> params_abbr = params_abbr [ : - <NUM_LIT:1> ] <EOL> if len ( params ) > len ( params_abbr ) : <EOL> params_abbr . append ( '<STR_LIT>' ) <EOL> except AttributeError : <EOL> pass <EOL> return ( '<STR_LIT>' . format ( comp . name , '<STR_LIT:U+002CU+0020>' . join ( params ) ) , <EOL> '<STR_LIT>' . format ( comp . name , '<STR_LIT:U+002CU+0020>' . join ( params_abbr ) ) ) <EOL> def parse_completion ( self , comp , cache ) : <EOL> """<STR_LIT>""" <EOL> name = comp . name <EOL> type_ , desc = [ x . strip ( ) for x in comp . description . split ( '<STR_LIT::>' , <NUM_LIT:1> ) ] <EOL> if type_ == '<STR_LIT>' and desc . startswith ( ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> builtin_type = desc . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] <EOL> if builtin_type in _types : <EOL> return ( name , builtin_type , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if type_ == '<STR_LIT:class>' and desc . startswith ( '<STR_LIT>' ) : <EOL> if self . show_docstring : <EOL> return ( name , <EOL> type_ , <EOL> comp . docstring ( ) , <EOL> self . call_signature ( comp ) [ <NUM_LIT:1> ] ) <EOL> else : <EOL> return ( name , type_ ) + self . call_signature ( comp ) <EOL> if type_ == '<STR_LIT>' : <EOL> if comp . module_path not in cache and comp . line and comp . line > <NUM_LIT:1> and os . path . exists ( comp . module_path ) : <EOL> with open ( comp . module_path , '<STR_LIT:r>' ) as fp : <EOL> cache [ comp . module_path ] = fp . readlines ( ) <EOL> lines = cache . get ( comp . module_path ) <EOL> if isinstance ( lines , list ) and len ( lines ) > <NUM_LIT:1> and comp . line < len ( lines ) and comp . line > <NUM_LIT:1> : <EOL> i = comp . line - <NUM_LIT:2> <EOL> while i >= <NUM_LIT:0> : <EOL> line = lines [ i ] . lstrip ( ) <EOL> if not line . startswith ( '<STR_LIT:@>' ) : <EOL> break <EOL> if line . startswith ( '<STR_LIT>' ) : <EOL> return ( name , '<STR_LIT>' , desc , '<STR_LIT>' ) <EOL> i -= <NUM_LIT:1> <EOL> if self . show_docstring : <EOL> return ( name , <EOL> type_ , <EOL> comp . docstring ( ) , <EOL> self . call_signature ( comp ) [ <NUM_LIT:1> ] ) <EOL> else : <EOL> return ( name , type_ ) + self . call_signature ( comp ) <EOL> return ( name , type_ , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class Client ( object ) : <EOL> """<STR_LIT>""" <EOL> max_completion_count = <NUM_LIT:50> <EOL> def __init__ ( self , desc_len = <NUM_LIT:0> , short_types = False , show_docstring = False , <EOL> debug = False ) : <EOL> self . _server = None <EOL> self . _count = <NUM_LIT:0> <EOL> self . env = os . environ . copy ( ) <EOL> self . env . update ( { <EOL> '<STR_LIT>' : jedi_path , <EOL> } ) <EOL> if '<STR_LIT>' in os . environ : <EOL> self . env [ '<STR_LIT>' ] = os . getenv ( '<STR_LIT>' ) <EOL> self . cmd = [ '<STR_LIT>' , '<STR_LIT>' , __file__ , '<STR_LIT>' , str ( desc_len ) ] <EOL> if short_types : <EOL> self . cmd . append ( '<STR_LIT>' ) <EOL> if show_docstring : <EOL> self . cmd . append ( '<STR_LIT>' ) <EOL> if debug : <EOL> self . cmd . append ( '<STR_LIT>' ) <EOL> self . restart ( ) <EOL> def shutdown ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _server is not None and self . _server . returncode is None : <EOL> self . _server . stdin . close ( ) <EOL> def restart ( self ) : <EOL> """<STR_LIT>""" <EOL> self . shutdown ( ) <EOL> self . _server = subprocess . Popen ( self . cmd , stdin = subprocess . PIPE , <EOL> stdout = subprocess . PIPE , env = self . env ) <EOL> def completions ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> if self . _count > self . max_completion_count : <EOL> self . _count = <NUM_LIT:0> <EOL> self . restart ( ) <EOL> self . _count += <NUM_LIT:1> <EOL> stream_write ( self . _server . stdin , args ) <EOL> return stream_read ( self . _server . stdout ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( '<STR_LIT>' , type = int ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> args = parser . parse_args ( ) <EOL> if args . debug : <EOL> log . removeHandler ( logging . NullHandler ) <EOL> handler = logging . FileHandler ( '<STR_LIT>' ) <EOL> handler . setLevel ( logging . DEBUG ) <EOL> log . addHandler ( handler ) <EOL> log . setLevel ( logging . DEBUG ) <EOL> s = Server ( args . desc_length , args . short_types , args . docstrings ) <EOL> s . run ( ) </s>
