2013-09-20 20:24:34 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright (C) 2013 Strahinja Val Markovic <val@markovic.io>
|
|
|
|
#
|
|
|
|
# This file is part of YouCompleteMe.
|
|
|
|
#
|
|
|
|
# YouCompleteMe is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# YouCompleteMe is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import vim
|
|
|
|
import json
|
|
|
|
import requests
|
2013-10-08 23:49:00 -04:00
|
|
|
import urlparse
|
2013-10-02 20:09:25 -04:00
|
|
|
from retries import retries
|
2013-10-01 19:21:17 -04:00
|
|
|
from requests_futures.sessions import FuturesSession
|
2013-10-30 21:08:35 -04:00
|
|
|
from ycm.unsafe_thread_pool_executor import UnsafeThreadPoolExecutor
|
2013-09-20 20:24:34 -04:00
|
|
|
from ycm import vimsupport
|
2013-10-08 19:21:43 -04:00
|
|
|
from ycm.server.responses import ServerError, UnknownExtraConf
|
2013-09-20 20:24:34 -04:00
|
|
|
|
|
|
|
HEADERS = {'content-type': 'application/json'}
|
2013-10-30 21:08:35 -04:00
|
|
|
EXECUTOR = UnsafeThreadPoolExecutor( max_workers = 10 )
|
2013-10-17 17:21:37 -04:00
|
|
|
# Setting this to None seems to screw up the Requests/urllib3 libs.
|
|
|
|
DEFAULT_TIMEOUT_SEC = 30
|
2013-09-20 20:24:34 -04:00
|
|
|
|
|
|
|
class BaseRequest( object ):
|
|
|
|
def __init__( self ):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def Start( self ):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def Done( self ):
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def Response( self ):
|
|
|
|
return {}
|
|
|
|
|
2013-11-20 15:33:57 -05:00
|
|
|
# This method blocks
|
|
|
|
# |timeout| is num seconds to tolerate no response from server before giving
|
|
|
|
# up; see Requests docs for details (we just pass the param along).
|
|
|
|
@staticmethod
|
|
|
|
def GetDataFromHandler( handler, timeout = DEFAULT_TIMEOUT_SEC ):
|
|
|
|
return JsonFromFuture( BaseRequest._TalkToHandlerAsync( '',
|
|
|
|
handler,
|
|
|
|
'GET',
|
|
|
|
timeout ) )
|
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
|
2013-10-01 19:21:17 -04:00
|
|
|
# This is the blocking version of the method. See below for async.
|
2013-10-14 14:08:15 -04:00
|
|
|
# |timeout| is num seconds to tolerate no response from server before giving
|
|
|
|
# up; see Requests docs for details (we just pass the param along).
|
2013-09-24 15:53:44 -04:00
|
|
|
@staticmethod
|
2013-10-17 17:21:37 -04:00
|
|
|
def PostDataToHandler( data, handler, timeout = DEFAULT_TIMEOUT_SEC ):
|
2013-10-01 19:21:17 -04:00
|
|
|
return JsonFromFuture( BaseRequest.PostDataToHandlerAsync( data,
|
2013-10-14 14:08:15 -04:00
|
|
|
handler,
|
|
|
|
timeout ) )
|
2013-09-23 17:34:26 -04:00
|
|
|
|
2013-09-27 16:52:04 -04:00
|
|
|
|
2013-10-01 19:21:17 -04:00
|
|
|
# This returns a future! Use JsonFromFuture to get the value.
|
2013-10-14 14:08:15 -04:00
|
|
|
# |timeout| is num seconds to tolerate no response from server before giving
|
|
|
|
# up; see Requests docs for details (we just pass the param along).
|
2013-10-01 19:21:17 -04:00
|
|
|
@staticmethod
|
2013-10-17 17:21:37 -04:00
|
|
|
def PostDataToHandlerAsync( data, handler, timeout = DEFAULT_TIMEOUT_SEC ):
|
2013-11-20 15:33:57 -05:00
|
|
|
return BaseRequest._TalkToHandlerAsync( data, handler, 'POST', timeout )
|
|
|
|
|
|
|
|
|
|
|
|
# This returns a future! Use JsonFromFuture to get the value.
|
|
|
|
# |method| is either 'POST' or 'GET'.
|
|
|
|
# |timeout| is num seconds to tolerate no response from server before giving
|
|
|
|
# up; see Requests docs for details (we just pass the param along).
|
|
|
|
@staticmethod
|
|
|
|
def _TalkToHandlerAsync( data,
|
|
|
|
handler,
|
|
|
|
method,
|
|
|
|
timeout = DEFAULT_TIMEOUT_SEC ):
|
|
|
|
def SendRequest( data, handler, method, timeout ):
|
|
|
|
if method == 'POST':
|
|
|
|
return BaseRequest.session.post( _BuildUri( handler ),
|
|
|
|
data = json.dumps( data ),
|
|
|
|
headers = HEADERS,
|
|
|
|
timeout = timeout )
|
|
|
|
if method == 'GET':
|
|
|
|
return BaseRequest.session.get( _BuildUri( handler ),
|
|
|
|
headers = HEADERS,
|
|
|
|
timeout = timeout )
|
2013-10-01 19:21:17 -04:00
|
|
|
|
2013-10-17 17:45:16 -04:00
|
|
|
@retries( 5, delay = 0.5, backoff = 1.5 )
|
2013-11-20 15:33:57 -05:00
|
|
|
def DelayedSendRequest( data, handler, method ):
|
|
|
|
if method == 'POST':
|
|
|
|
return requests.post( _BuildUri( handler ),
|
|
|
|
data = json.dumps( data ),
|
|
|
|
headers = HEADERS )
|
|
|
|
if method == 'GET':
|
|
|
|
return requests.get( _BuildUri( handler ),
|
|
|
|
headers = HEADERS )
|
2013-09-20 20:24:34 -04:00
|
|
|
|
2013-10-02 20:09:25 -04:00
|
|
|
if not _CheckServerIsHealthyWithCache():
|
2013-11-20 15:33:57 -05:00
|
|
|
return EXECUTOR.submit( DelayedSendRequest, data, handler, method )
|
2013-10-02 20:09:25 -04:00
|
|
|
|
2013-11-20 15:33:57 -05:00
|
|
|
return SendRequest( data, handler, method, timeout )
|
2013-10-02 20:09:25 -04:00
|
|
|
|
|
|
|
|
|
|
|
session = FuturesSession( executor = EXECUTOR )
|
2013-09-20 20:24:34 -04:00
|
|
|
server_location = 'http://localhost:6666'
|
|
|
|
|
|
|
|
|
2013-10-14 16:29:28 -04:00
|
|
|
def BuildRequestData( start_column = None,
|
|
|
|
query = None,
|
|
|
|
include_buffer_data = True ):
|
2013-09-20 20:24:34 -04:00
|
|
|
line, column = vimsupport.CurrentLineAndColumn()
|
2013-10-06 22:45:47 -04:00
|
|
|
filepath = vimsupport.GetCurrentBufferFilepath()
|
2013-09-20 20:24:34 -04:00
|
|
|
request_data = {
|
|
|
|
'filetypes': vimsupport.CurrentFiletypes(),
|
|
|
|
'line_num': line,
|
|
|
|
'column_num': column,
|
|
|
|
'start_column': start_column,
|
|
|
|
'line_value': vim.current.line,
|
2013-10-14 16:29:28 -04:00
|
|
|
'filepath': filepath
|
2013-09-20 20:24:34 -04:00
|
|
|
}
|
|
|
|
|
2013-10-14 16:29:28 -04:00
|
|
|
if include_buffer_data:
|
|
|
|
request_data[ 'file_data' ] = vimsupport.GetUnsavedAndCurrentBufferData()
|
2013-09-20 20:24:34 -04:00
|
|
|
if query:
|
|
|
|
request_data[ 'query' ] = query
|
|
|
|
|
|
|
|
return request_data
|
|
|
|
|
|
|
|
|
2013-10-01 19:21:17 -04:00
|
|
|
def JsonFromFuture( future ):
|
|
|
|
response = future.result()
|
|
|
|
if response.status_code == requests.codes.server_error:
|
2013-10-08 19:21:43 -04:00
|
|
|
_RaiseExceptionForData( response.json() )
|
2013-10-01 19:21:17 -04:00
|
|
|
|
|
|
|
# We let Requests handle the other status types, we only handle the 500
|
|
|
|
# error code.
|
|
|
|
response.raise_for_status()
|
|
|
|
|
|
|
|
if response.text:
|
|
|
|
return response.json()
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
def _BuildUri( handler ):
|
2013-10-08 23:49:00 -04:00
|
|
|
return urlparse.urljoin( BaseRequest.server_location, handler )
|
2013-09-20 20:24:34 -04:00
|
|
|
|
|
|
|
|
2013-10-02 20:09:25 -04:00
|
|
|
SERVER_HEALTHY = False
|
|
|
|
|
|
|
|
def _CheckServerIsHealthyWithCache():
|
|
|
|
global SERVER_HEALTHY
|
|
|
|
|
|
|
|
def _ServerIsHealthy():
|
|
|
|
response = requests.get( _BuildUri( 'healthy' ) )
|
|
|
|
response.raise_for_status()
|
|
|
|
return response.json()
|
|
|
|
|
|
|
|
if SERVER_HEALTHY:
|
|
|
|
return True
|
|
|
|
|
|
|
|
try:
|
|
|
|
SERVER_HEALTHY = _ServerIsHealthy()
|
|
|
|
return SERVER_HEALTHY
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
2013-10-08 19:21:43 -04:00
|
|
|
|
|
|
|
def _RaiseExceptionForData( data ):
|
|
|
|
if data[ 'exception' ][ 'TYPE' ] == UnknownExtraConf.__name__:
|
|
|
|
raise UnknownExtraConf( data[ 'exception' ][ 'extra_conf_file' ] )
|
|
|
|
|
|
|
|
raise ServerError( '{0}: {1}'.format( data[ 'exception' ][ 'TYPE' ],
|
|
|
|
data[ 'message' ] ) )
|