8000 Refactor jsonrpc logic (#303) · llan-ml/python-lsp-server@3c99350 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3c99350

Browse files
authored
Refactor jsonrpc logic (#303)
1 parent 0f353bc commit 3c99350

17 files changed

+945
-694
lines changed

pyls/json_rpc_server.py

Lines changed: 0 additions & 143 deletions
This file was deleted.

pyls/jsonrpc/__init__.py

Whitespace-only changes.

pyls/jsonrpc/dispatchers.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2018 Palantir Technologies, Inc.
2+
import functools
3+
import re
4+
5+
_RE_FIRST_CAP = re.compile('(.)([A-Z][a-z]+)')
6+
_RE_ALL_CAP = re.compile('([a-z0-9])([A-Z])')
7+
8+
9+
class MethodDispatcher(object):
10+
"""JSON RPC dispatcher that calls methods on itself.
11+
12+
Method names are computed by converting camel case to snake case, slashes with double underscores, and removing
13+
dollar signs.
14+
"""
15+
16+
def __getitem__(self, item):
17+
method_name = 'm_{}'.format(_method_to_string(item))
18+
if hasattr(self, method_name):
19+
method = getattr(self, method_name)
20+
21+
@functools.wraps(method)
22+
def handler(params):
23+
return method(**(params or {}))
24+
25+
return handler
26+
raise KeyError()
27+
28+
29+
def _method_to_string(method):
30+
return _camel_to_underscore(method.replace("/", "__").replace("$", ""))
31+
32+
33+
def _camel_to_underscore(string):
34+
s1 = _RE_FIRST_CAP.sub(r'\1_\2', string)
35+
return _RE_ALL_CAP.sub(r'\1_\2', s1).lower()

0 commit comments

Comments
 (0)
0