passing only params to callbacks

This commit is contained in:
yeger 2019-01-11 17:55:02 -05:00
parent 07c6b40f8b
commit ae7305d84f
2 changed files with 41 additions and 39 deletions

View File

@ -54,7 +54,7 @@ class JsonRpcEndpoint(object):
def recv_response(self): def recv_response(self):
''' '''
Recives a message. Recives a message.
:return: a message :return: a message
''' '''
@ -62,7 +62,7 @@ class JsonRpcEndpoint(object):
line = self.stdout.readline() line = self.stdout.readline()
if not line: if not line:
return None return None
line = line.decode('utf-8') line = line.decode("utf-8")
# TODO: handle content type as well. # TODO: handle content type as well.
match = re.match(JSON_RPC_RES_REGEX, line) match = re.match(JSON_RPC_RES_REGEX, line)
if match is None or not match.groups(): if match is None or not match.groups():
@ -71,8 +71,8 @@ class JsonRpcEndpoint(object):
line = self.stdout.readline() line = self.stdout.readline()
if not line: if not line:
return None return None
line = line.decode('utf-8') line = line.decode("utf-8")
if line != "\r\n": if line != "\r\n":
raise RuntimeError("Bad header: missing newline") 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) return json.loads(jsonrpc_res)

View File

@ -4,26 +4,21 @@ import collections
from pylspclient import lsp_structs 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): 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) threading.Thread.__init__(self)
self.json_rpc_endpoint = json_rpc_endpoint self.json_rpc_endpoint = json_rpc_endpoint
self.notify_callbacks = collections.defaultdict(lambda : default_notify_callback, notify_callbacks) self.notify_callbacks = notify_callbacks
self.method_callbacks = collections.defaultdict(lambda : default_method_callback, method_callbacks) self.method_callbacks = method_callbacks
self.event_dict = {} self.event_dict = {}
self.response_dict = {} self.response_dict = {}
self.next_id = 0 self.next_id = 0
self.shutdown_flag = False self.shutdown_flag = False
def handle_result(self, jsonrpc_res): def handle_result(self, rpc_id, result, error):
self.response_dict[jsonrpc_res["id"]] = jsonrpc_res self.response_dict[rpc_id] = (result, error)
cond = self.event_dict[jsonrpc_res["id"]] cond = self.event_dict[rpc_id]
cond.acquire() cond.acquire()
cond.notify() cond.notify()
cond.release() cond.release()
@ -35,28 +30,36 @@ class LspEndpoint(threading.Thread):
def run(self): def run(self):
while not self.shutdown_flag: while not self.shutdown_flag:
jsonrpc_message = self.json_rpc_endpoint.recv_response() try:
jsonrpc_message = self.json_rpc_endpoint.recv_response()
if jsonrpc_message is None: method = jsonrpc_message.get("method")
print("server quit") result = jsonrpc_message.get("result")
break 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: if jsonrpc_message is None:
self.handle_result(jsonrpc_message) print("server quit")
elif "method" in jsonrpc_message: break
if "id" in jsonrpc_message:
# a call for method if method:
try: if rpc_id:
result = self.method_callbacks[jsonrpc_message["method"]](jsonrpc_message) # a call for method
self.send_response(jsonrpc_message["id"], result, None) if method not in self.method_callbacks:
except lsp_structs.ResponseError as e: raise lsp_structs.ResponseError("Method not found: {method}".format(method=method), lsp_structs.ErrorCodes.MethodNotFound)
self.send_response(jsonrpc_message["id"], None, e) 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: else:
# a call for notify self.handle_result(rpc_id, result, error)
self.notify_callbacks[jsonrpc_message["method"]](jsonrpc_message) except lsp_structs.ResponseError as e:
else: self.send_response(rpc_id, None, e)
print("unknown jsonrpc message")
def send_response(self, id, result, error): def send_response(self, id, result, error):
message_dict = {} message_dict = {}
@ -88,11 +91,10 @@ class LspEndpoint(threading.Thread):
self.send_message(method_name, kwargs, current_id) self.send_message(method_name, kwargs, current_id)
cond.wait() cond.wait()
cond.release() cond.release()
response = self.response_dict[current_id] result, error = self.response_dict[current_id]
if "error" in response: if error:
error = response["error"]
raise lsp_structs.ResponseError(error.get("code"), error.get("message"), error.get("data")) 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): def send_notification(self, method_name, **kwargs):