<s> """<STR_LIT>""" <EOL> import argparse <EOL> import inspect <EOL> import logging <EOL> import json <EOL> import os <EOL> import struct <EOL> import sys <EOL> import time <EOL> import traceback <EOL> import threading <EOL> try : <EOL> import socketserver <EOL> PY33 = True <EOL> except ImportError : <EOL> import SocketServer as socketserver <EOL> PY33 = False <EOL> def _logger ( ) : <EOL> """<STR_LIT>""" <EOL> return logging . getLogger ( __name__ ) <EOL> HEARTBEAT_DELAY = <NUM_LIT> <EOL> def import_class ( klass ) : <EOL> """<STR_LIT>""" <EOL> path = klass . rfind ( "<STR_LIT:.>" ) <EOL> class_name = klass [ path + <NUM_LIT:1> : len ( klass ) ] <EOL> try : <EOL> module = __import__ ( klass [ <NUM_LIT:0> : path ] , globals ( ) , locals ( ) , [ class_name ] ) <EOL> klass = getattr ( module , class_name ) <EOL> except ImportError as e : <EOL> raise ImportError ( '<STR_LIT>' % ( klass , str ( e ) ) ) <EOL> except AttributeError : <EOL> raise ImportError ( klass ) <EOL> else : <EOL> return klass <EOL> class JsonServer ( socketserver . TCPServer ) : <EOL> """<STR_LIT>""" <EOL> class _Handler ( socketserver . BaseRequestHandler ) : <EOL> def read_bytes ( self , size ) : <EOL> """<STR_LIT>""" <EOL> if not PY33 : <EOL> data = '<STR_LIT>' <EOL> else : <EOL> data = bytes ( ) <EOL> while len ( data ) < size : <EOL> tmp = self . request . recv ( size - len ( data ) ) <EOL> data += tmp <EOL> if tmp == '<STR_LIT>' : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> return data <EOL> def get_msg_len ( self ) : <EOL> """<STR_LIT>""" <EOL> data = self . read_bytes ( <NUM_LIT:4> ) <EOL> payload = struct . unpack ( '<STR_LIT>' , data ) <EOL> return payload [ <NUM_LIT:0> ] <EOL> def read ( self ) : <EOL> """<STR_LIT>""" <EOL> size = self . get_msg_len ( ) <EOL> data = self . read_bytes ( size ) . decode ( '<STR_LIT:utf-8>' ) <EOL> return json . loads ( data ) <EOL> def send ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> msg = json . dumps ( obj ) . encode ( '<STR_LIT:utf-8>' ) <EOL> _logger ( ) . log ( <NUM_LIT:1> , '<STR_LIT>' , len ( msg ) ) <EOL> header = struct . pack ( '<STR_LIT>' , len ( msg ) ) <EOL> self . request . sendall ( header ) <EOL> self . request . sendall ( msg ) <EOL> def handle ( self ) : <EOL> """<STR_LIT>""" <EOL> self . srv . reset_heartbeat ( ) <EOL> self . srv . timeout = HEARTBEAT_DELAY * <NUM_LIT:10> <EOL> data = self . read ( ) <EOL> self . _handle ( data ) <EOL> self . srv . timeout = HEARTBEAT_DELAY <EOL> self . srv . reset_heartbeat ( ) <EOL> def _handle ( self , data ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> _logger ( ) . log ( <NUM_LIT:1> , '<STR_LIT>' , data ) <EOL> assert data [ '<STR_LIT>' ] <EOL> assert data [ '<STR_LIT>' ] <EOL> assert data [ '<STR_LIT:data>' ] is not None <EOL> response = { '<STR_LIT>' : data [ '<STR_LIT>' ] , '<STR_LIT>' : [ ] } <EOL> try : <EOL> worker = import_class ( data [ '<STR_LIT>' ] ) <EOL> except ImportError : <EOL> _logger ( ) . exception ( '<STR_LIT>' ) <EOL> else : <EOL> if inspect . isclass ( worker ) : <EOL> worker = worker ( ) <EOL> _logger ( ) . log ( <NUM_LIT:1> , '<STR_LIT>' , worker ) <EOL> _logger ( ) . log ( <NUM_LIT:1> , '<STR_LIT>' , data [ '<STR_LIT:data>' ] ) <EOL> try : <EOL> ret_val = worker ( data [ '<STR_LIT:data>' ] ) <EOL> except Exception : <EOL> _logger ( ) . exception ( <EOL> '<STR_LIT>' , <EOL> worker , data [ '<STR_LIT:data>' ] ) <EOL> ret_val = None <EOL> if ret_val is None : <EOL> ret_val = [ ] <EOL> response = { '<STR_LIT>' : data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : ret_val } <EOL> finally : <EOL> _logger ( ) . log ( <NUM_LIT:1> , '<STR_LIT>' , response ) <EOL> try : <EOL> self . send ( response ) <EOL> except ConnectionAbortedError : <EOL> pass <EOL> except : <EOL> _logger ( ) . warn ( '<STR_LIT>' , data ) <EOL> exc1 , exc2 , exc3 = sys . exc_info ( ) <EOL> traceback . print_exception ( exc1 , exc2 , exc3 , file = sys . stderr ) <EOL> def __init__ ( self , args = None ) : <EOL> """<STR_LIT>""" <EOL> self . reset_heartbeat ( ) <EOL> if not args : <EOL> args = default_parser ( ) . parse_args ( ) <EOL> self . port = args . port <EOL> self . timeout = HEARTBEAT_DELAY <EOL> self . _Handler . srv = self <EOL> socketserver . TCPServer . __init__ ( <EOL> self , ( '<STR_LIT:127.0.0.1>' , int ( args . port ) ) , self . _Handler ) <EOL> print ( '<STR_LIT>' % int ( args . port ) ) <EOL> print ( '<STR_LIT>' % ( sys . version_info [ : <NUM_LIT:3> ] ) ) <EOL> self . _heartbeat_thread = threading . Thread ( target = self . heartbeat ) <EOL> self . _heartbeat_thread . setDaemon ( True ) <EOL> self . _heartbeat_thread . start ( ) <EOL> def reset_heartbeat ( self ) : <EOL> self . last_time = time . time ( ) <EOL> self . elapsed_time = <NUM_LIT:0> <EOL> def heartbeat ( self ) : <EOL> while True : <EOL> elapsed_time = time . time ( ) - self . last_time <EOL> if elapsed_time > self . timeout : <EOL> self . shutdown ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> def default_parser ( ) : <EOL> """<STR_LIT>""" <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( "<STR_LIT:port>" , help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return parser <EOL> def serve_forever ( args = None ) : <EOL> """<STR_LIT>""" <EOL> class Unbuffered ( object ) : <EOL> def __init__ ( self , stream ) : <EOL> self . stream = stream <EOL> def write ( self , data ) : <EOL> self . stream . write ( data ) <EOL> self . stream . flush ( ) <EOL> def __getattr__ ( self , attr ) : <EOL> return getattr ( self . stream , attr ) <EOL> sys . stdout = Unbuffered ( sys . stdout ) <EOL> sys . stderr = Unbuffered ( sys . stderr ) <EOL> server = JsonServer ( args = args ) <EOL> server . serve_forever ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from pyqode . core import backend <EOL> backend . CodeCompletionWorker . providers . append ( <EOL> backend . DocumentWordsProvider ( ) ) <EOL> serve_forever ( ) </s>
