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 sys
|
|
|
|
import os
|
2013-09-23 17:33:14 -04:00
|
|
|
import atexit
|
2013-09-20 20:24:34 -04:00
|
|
|
|
|
|
|
# We want to have the YouCompleteMe/python directory on the Python PATH because
|
|
|
|
# all the code already assumes that it's there. This is a relic from before the
|
|
|
|
# client/server architecture.
|
|
|
|
# TODO: Fix things so that this is not needed anymore when we split ycmd into a
|
|
|
|
# separate repository.
|
|
|
|
sys.path.insert( 0, os.path.join(
|
|
|
|
os.path.dirname( os.path.abspath( __file__ ) ),
|
|
|
|
'../..' ) )
|
|
|
|
|
2013-10-01 19:21:17 -04:00
|
|
|
from ycm import utils
|
|
|
|
utils.AddThirdPartyFoldersToSysPath()
|
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
import logging
|
|
|
|
import json
|
|
|
|
import bottle
|
|
|
|
from bottle import run, request, response
|
|
|
|
import server_state
|
|
|
|
from ycm import user_options_store
|
2013-09-27 16:52:04 -04:00
|
|
|
from ycm.server.responses import BuildExceptionResponse
|
2013-09-20 20:24:34 -04:00
|
|
|
import argparse
|
2013-09-27 19:24:42 -04:00
|
|
|
import httplib
|
2013-09-20 20:24:34 -04:00
|
|
|
|
|
|
|
# num bytes for the request body buffer; request.json only works if the request
|
|
|
|
# size is less than this
|
|
|
|
bottle.Request.MEMFILE_MAX = 300 * 1024
|
|
|
|
|
2013-09-25 13:56:46 -04:00
|
|
|
SERVER_STATE = None
|
2013-09-25 14:07:25 -04:00
|
|
|
LOGGER = None
|
2013-09-20 20:24:34 -04:00
|
|
|
app = bottle.Bottle()
|
|
|
|
|
|
|
|
|
|
|
|
@app.post( '/event_notification' )
|
|
|
|
def EventNotification():
|
|
|
|
LOGGER.info( 'Received event notification')
|
|
|
|
request_data = request.json
|
|
|
|
event_name = request_data[ 'event_name' ]
|
|
|
|
LOGGER.debug( 'Event name: %s', event_name )
|
|
|
|
|
|
|
|
event_handler = 'On' + event_name
|
|
|
|
getattr( SERVER_STATE.GetGeneralCompleter(), event_handler )( request_data )
|
|
|
|
|
|
|
|
filetypes = request_data[ 'filetypes' ]
|
2013-10-03 16:15:43 -04:00
|
|
|
response_data = None
|
2013-09-20 20:24:34 -04:00
|
|
|
if SERVER_STATE.FiletypeCompletionUsable( filetypes ):
|
2013-10-03 16:15:43 -04:00
|
|
|
response_data = getattr( SERVER_STATE.GetFiletypeCompleter( filetypes ),
|
|
|
|
event_handler )( request_data )
|
|
|
|
|
|
|
|
if response_data:
|
|
|
|
return _JsonResponse( response_data )
|
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
@app.post( '/run_completer_command' )
|
|
|
|
def RunCompleterCommand():
|
|
|
|
LOGGER.info( 'Received command request')
|
|
|
|
request_data = request.json
|
2013-09-27 19:20:35 -04:00
|
|
|
completer = _GetCompleterForRequestData( request_data )
|
2013-09-20 20:24:34 -04:00
|
|
|
|
2013-09-27 16:52:04 -04:00
|
|
|
return _JsonResponse( completer.OnUserCommand(
|
|
|
|
request_data[ 'command_arguments' ],
|
|
|
|
request_data ) )
|
2013-09-20 20:24:34 -04:00
|
|
|
|
|
|
|
|
2013-10-02 15:23:26 -04:00
|
|
|
@app.post( '/completions' )
|
2013-09-20 20:24:34 -04:00
|
|
|
def GetCompletions():
|
|
|
|
LOGGER.info( 'Received completion request')
|
|
|
|
request_data = request.json
|
|
|
|
do_filetype_completion = SERVER_STATE.ShouldUseFiletypeCompleter(
|
|
|
|
request_data )
|
|
|
|
LOGGER.debug( 'Using filetype completion: %s', do_filetype_completion )
|
|
|
|
filetypes = request_data[ 'filetypes' ]
|
|
|
|
completer = ( SERVER_STATE.GetFiletypeCompleter( filetypes ) if
|
|
|
|
do_filetype_completion else
|
|
|
|
SERVER_STATE.GetGeneralCompleter() )
|
|
|
|
|
2013-09-30 16:40:25 -04:00
|
|
|
return _JsonResponse( completer.ComputeCandidates( request_data ) )
|
2013-09-20 20:24:34 -04:00
|
|
|
|
|
|
|
|
2013-09-24 15:53:44 -04:00
|
|
|
@app.get( '/user_options' )
|
|
|
|
def GetUserOptions():
|
|
|
|
LOGGER.info( 'Received user options GET request')
|
|
|
|
return _JsonResponse( dict( SERVER_STATE.user_options ) )
|
|
|
|
|
|
|
|
|
2013-10-03 13:14:31 -04:00
|
|
|
@app.get( '/healthy' )
|
|
|
|
def GetHealthy():
|
|
|
|
LOGGER.info( 'Received health request')
|
|
|
|
return _JsonResponse( True )
|
|
|
|
|
|
|
|
|
2013-09-24 15:53:44 -04:00
|
|
|
@app.post( '/user_options' )
|
|
|
|
def SetUserOptions():
|
|
|
|
LOGGER.info( 'Received user options POST request')
|
2013-09-24 17:00:27 -04:00
|
|
|
_SetUserOptions( request.json )
|
2013-09-20 20:24:34 -04:00
|
|
|
|
|
|
|
|
2013-10-03 13:14:31 -04:00
|
|
|
# TODO: Rename this to 'semantic_completion_available'
|
2013-09-20 20:24:34 -04:00
|
|
|
@app.post( '/filetype_completion_available')
|
|
|
|
def FiletypeCompletionAvailable():
|
|
|
|
LOGGER.info( 'Received filetype completion available request')
|
|
|
|
return _JsonResponse( SERVER_STATE.FiletypeCompletionAvailable(
|
|
|
|
request.json[ 'filetypes' ] ) )
|
|
|
|
|
|
|
|
|
2013-09-27 19:20:35 -04:00
|
|
|
@app.post( '/defined_subcommands')
|
|
|
|
def DefinedSubcommands():
|
|
|
|
LOGGER.info( 'Received defined subcommands request')
|
|
|
|
completer = _GetCompleterForRequestData( request.json )
|
|
|
|
|
|
|
|
return _JsonResponse( completer.DefinedSubcommands() )
|
|
|
|
|
|
|
|
|
2013-10-03 17:49:51 -04:00
|
|
|
@app.post( '/detailed_diagnostic')
|
|
|
|
def GetDetailedDiagnostic():
|
|
|
|
LOGGER.info( 'Received detailed diagnostic request')
|
|
|
|
request_data = request.json
|
|
|
|
completer = _GetCompleterForRequestData( request_data )
|
|
|
|
|
|
|
|
return _JsonResponse( completer.GetDetailedDiagnostic( request_data ) )
|
|
|
|
|
|
|
|
|
2013-09-27 20:13:43 -04:00
|
|
|
@app.post( '/debug_info')
|
|
|
|
def DebugInfo():
|
|
|
|
# This can't be at the top level because of possible extra conf preload
|
|
|
|
import ycm_core
|
|
|
|
LOGGER.info( 'Received debug info request')
|
|
|
|
|
|
|
|
output = []
|
|
|
|
has_clang_support = ycm_core.HasClangSupport()
|
|
|
|
output.append( 'Server has Clang support compiled in: {0}'.format(
|
|
|
|
has_clang_support ) )
|
|
|
|
|
|
|
|
if has_clang_support:
|
|
|
|
output.append( ycm_core.ClangVersion() )
|
|
|
|
|
|
|
|
request_data = request.json
|
2013-09-27 20:26:14 -04:00
|
|
|
try:
|
|
|
|
output.append(
|
|
|
|
_GetCompleterForRequestData( request_data ).DebugInfo( request_data) )
|
|
|
|
except:
|
|
|
|
pass
|
2013-09-27 20:13:43 -04:00
|
|
|
return _JsonResponse( '\n'.join( output ) )
|
|
|
|
|
|
|
|
|
2013-09-27 16:52:04 -04:00
|
|
|
# The type of the param is Bottle.HTTPError
|
2013-09-27 19:24:42 -04:00
|
|
|
@app.error( httplib.INTERNAL_SERVER_ERROR )
|
2013-09-27 16:52:04 -04:00
|
|
|
def ErrorHandler( httperror ):
|
|
|
|
return _JsonResponse( BuildExceptionResponse( str( httperror.exception ),
|
|
|
|
httperror.traceback ) )
|
|
|
|
|
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
def _JsonResponse( data ):
|
|
|
|
response.set_header( 'Content-Type', 'application/json' )
|
|
|
|
return json.dumps( data )
|
|
|
|
|
|
|
|
|
2013-09-27 19:20:35 -04:00
|
|
|
def _GetCompleterForRequestData( request_data ):
|
|
|
|
completer_target = request_data.get( 'completer_target', None )
|
|
|
|
|
|
|
|
if completer_target == 'identifier':
|
|
|
|
return SERVER_STATE.GetGeneralCompleter().GetIdentifierCompleter()
|
|
|
|
elif completer_target == 'filetype_default' or not completer_target:
|
|
|
|
return SERVER_STATE.GetFiletypeCompleter( request_data[ 'filetypes' ] )
|
|
|
|
else:
|
|
|
|
return SERVER_STATE.GetFiletypeCompleter( [ completer_target ] )
|
|
|
|
|
|
|
|
|
2013-09-23 17:33:14 -04:00
|
|
|
@atexit.register
|
|
|
|
def _ServerShutdown():
|
2013-09-25 13:56:46 -04:00
|
|
|
if SERVER_STATE:
|
|
|
|
SERVER_STATE.Shutdown()
|
2013-09-23 16:27:32 -04:00
|
|
|
|
|
|
|
|
2013-09-24 17:00:27 -04:00
|
|
|
def _SetUserOptions( options ):
|
|
|
|
global SERVER_STATE
|
|
|
|
|
|
|
|
user_options_store.SetAll( options )
|
2013-09-25 13:56:46 -04:00
|
|
|
SERVER_STATE = server_state.ServerState( options )
|
|
|
|
|
|
|
|
|
|
|
|
def SetServerStateToDefaults():
|
2013-09-25 14:07:25 -04:00
|
|
|
global SERVER_STATE, LOGGER
|
|
|
|
LOGGER = logging.getLogger( __name__ )
|
2013-09-25 13:56:46 -04:00
|
|
|
user_options_store.LoadDefaults()
|
|
|
|
SERVER_STATE = server_state.ServerState( user_options_store.GetAll() )
|
2013-09-24 17:00:27 -04:00
|
|
|
|
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
def Main():
|
|
|
|
global LOGGER
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument( '--host', type = str, default = 'localhost',
|
2013-09-25 14:12:34 -04:00
|
|
|
help = 'server hostname')
|
2013-09-20 20:24:34 -04:00
|
|
|
parser.add_argument( '--port', type = int, default = 6666,
|
2013-09-25 14:12:34 -04:00
|
|
|
help = 'server port')
|
2013-09-20 20:24:34 -04:00
|
|
|
parser.add_argument( '--log', type = str, default = 'info',
|
2013-09-25 14:12:34 -04:00
|
|
|
help = 'log level, one of '
|
|
|
|
'[debug|info|warning|error|critical]' )
|
2013-09-24 17:00:27 -04:00
|
|
|
parser.add_argument( '--options_file', type = str, default = '',
|
2013-09-25 14:12:34 -04:00
|
|
|
help = 'file with user options, in JSON format' )
|
2013-09-20 20:24:34 -04:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2013-09-24 17:00:27 -04:00
|
|
|
if args.options_file:
|
|
|
|
_SetUserOptions( json.load( open( args.options_file, 'r' ) ) )
|
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
numeric_level = getattr( logging, args.log.upper(), None )
|
|
|
|
if not isinstance( numeric_level, int ):
|
|
|
|
raise ValueError( 'Invalid log level: %s' % args.log )
|
|
|
|
|
|
|
|
logging.basicConfig( format = '%(asctime)s - %(levelname)s - %(message)s',
|
|
|
|
level = numeric_level )
|
|
|
|
|
|
|
|
LOGGER = logging.getLogger( __name__ )
|
2013-10-04 15:45:33 -04:00
|
|
|
run( app = app, host = args.host, port = args.port, server='waitress' )
|
2013-09-20 20:24:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
Main()
|
|
|
|
|