passing only params to callbacks
This commit is contained in:
parent
07c6b40f8b
commit
ae7305d84f
@ -54,7 +54,7 @@ class JsonRpcEndpoint(object):
|
||||
|
||||
def recv_response(self):
|
||||
'''
|
||||
Recives a message.
|
||||
Recives a message.
|
||||
|
||||
:return: a message
|
||||
'''
|
||||
@ -62,7 +62,7 @@ class JsonRpcEndpoint(object):
|
||||
line = self.stdout.readline()
|
||||
if not line:
|
||||
return None
|
||||
line = line.decode('utf-8')
|
||||
line = line.decode("utf-8")
|
||||
# TODO: handle content type as well.
|
||||
match = re.match(JSON_RPC_RES_REGEX, line)
|
||||
if match is None or not match.groups():
|
||||
@ -71,8 +71,8 @@ class JsonRpcEndpoint(object):
|
||||
line = self.stdout.readline()
|
||||
if not line:
|
||||
return None
|
||||
line = line.decode('utf-8')
|
||||
line = line.decode("utf-8")
|
||||
if line != "\r\n":
|
||||
raise RuntimeError("Bad header: missing newline")
|
||||
jsonrpc_res = self.stdout.read(size).decode('utf-8')
|
||||
jsonrpc_res = self.stdout.read(size).decode("utf-8")
|
||||
return json.loads(jsonrpc_res)
|
||||
|
@ -4,26 +4,21 @@ import collections
|
||||
from pylspclient import lsp_structs
|
||||
|
||||
|
||||
class MethodNotFound(object):
|
||||
def __call__(self, jsonrpc_message):
|
||||
raise lsp_structs.ResponseError("Method not found: {method}".format(method=jsonrpc_message["method"]), lsp_structs.ErrorCodes.MethodNotFound)
|
||||
|
||||
|
||||
class LspEndpoint(threading.Thread):
|
||||
def __init__(self, json_rpc_endpoint, default_method_callback=MethodNotFound(), method_callbacks={}, default_notify_callback=print, notify_callbacks={}):
|
||||
def __init__(self, json_rpc_endpoint, method_callbacks={}, notify_callbacks={}):
|
||||
threading.Thread.__init__(self)
|
||||
self.json_rpc_endpoint = json_rpc_endpoint
|
||||
self.notify_callbacks = collections.defaultdict(lambda : default_notify_callback, notify_callbacks)
|
||||
self.method_callbacks = collections.defaultdict(lambda : default_method_callback, method_callbacks)
|
||||
self.notify_callbacks = notify_callbacks
|
||||
self.method_callbacks = method_callbacks
|
||||
self.event_dict = {}
|
||||
self.response_dict = {}
|
||||
self.next_id = 0
|
||||
self.shutdown_flag = False
|
||||
|
||||
|
||||
def handle_result(self, jsonrpc_res):
|
||||
self.response_dict[jsonrpc_res["id"]] = jsonrpc_res
|
||||
cond = self.event_dict[jsonrpc_res["id"]]
|
||||
def handle_result(self, rpc_id, result, error):
|
||||
self.response_dict[rpc_id] = (result, error)
|
||||
cond = self.event_dict[rpc_id]
|
||||
cond.acquire()
|
||||
cond.notify()
|
||||
cond.release()
|
||||
@ -35,28 +30,36 @@ class LspEndpoint(threading.Thread):
|
||||
|
||||
def run(self):
|
||||
while not self.shutdown_flag:
|
||||
jsonrpc_message = self.json_rpc_endpoint.recv_response()
|
||||
|
||||
if jsonrpc_message is None:
|
||||
print("server quit")
|
||||
break
|
||||
try:
|
||||
jsonrpc_message = self.json_rpc_endpoint.recv_response()
|
||||
method = jsonrpc_message.get("method")
|
||||
result = jsonrpc_message.get("result")
|
||||
error = jsonrpc_message.get("error")
|
||||
rpc_id = jsonrpc_message.get("id")
|
||||
params = jsonrpc_message.get("params")
|
||||
|
||||
if "result" in jsonrpc_message or "error" in jsonrpc_message:
|
||||
self.handle_result(jsonrpc_message)
|
||||
elif "method" in jsonrpc_message:
|
||||
if "id" in jsonrpc_message:
|
||||
# a call for method
|
||||
try:
|
||||
result = self.method_callbacks[jsonrpc_message["method"]](jsonrpc_message)
|
||||
self.send_response(jsonrpc_message["id"], result, None)
|
||||
except lsp_structs.ResponseError as e:
|
||||
self.send_response(jsonrpc_message["id"], None, e)
|
||||
if jsonrpc_message is None:
|
||||
print("server quit")
|
||||
break
|
||||
|
||||
if method:
|
||||
if rpc_id:
|
||||
# a call for method
|
||||
if method not in self.method_callbacks:
|
||||
raise lsp_structs.ResponseError("Method not found: {method}".format(method=method), lsp_structs.ErrorCodes.MethodNotFound)
|
||||
result = self.method_callbacks[method](params)
|
||||
self.send_response(rpc_id, result, None)
|
||||
else:
|
||||
# a call for notify
|
||||
if method not in self.notify_callbacks:
|
||||
raise lsp_structs.ResponseError("Method not found: {method}".format(method=method), lsp_structs.ErrorCodes.MethodNotFound)
|
||||
|
||||
self.notify_callbacks[method](params)
|
||||
else:
|
||||
# a call for notify
|
||||
self.notify_callbacks[jsonrpc_message["method"]](jsonrpc_message)
|
||||
else:
|
||||
print("unknown jsonrpc message")
|
||||
|
||||
self.handle_result(rpc_id, result, error)
|
||||
except lsp_structs.ResponseError as e:
|
||||
self.send_response(rpc_id, None, e)
|
||||
|
||||
|
||||
def send_response(self, id, result, error):
|
||||
message_dict = {}
|
||||
@ -88,11 +91,10 @@ class LspEndpoint(threading.Thread):
|
||||
self.send_message(method_name, kwargs, current_id)
|
||||
cond.wait()
|
||||
cond.release()
|
||||
response = self.response_dict[current_id]
|
||||
if "error" in response:
|
||||
error = response["error"]
|
||||
result, error = self.response_dict[current_id]
|
||||
if error:
|
||||
raise lsp_structs.ResponseError(error.get("code"), error.get("message"), error.get("data"))
|
||||
return response["result"]
|
||||
return result
|
||||
|
||||
|
||||
def send_notification(self, method_name, **kwargs):
|
||||
|
Loading…
Reference in New Issue
Block a user