Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Pre Commit hook #68

Merged
merged 5 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.9
hooks:
- id: ruff
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.11.2
hooks:
- id: mypy
args: ["pylspclient"]
pass_filenames: false
additional_dependencies: [pydantic~=2.5.2]
- repo: local
hooks:
- id: poetry-install
name: Poetry install
entry: poetry install --no-interaction --no-root --with tests
language: system
types: [python]
pass_filenames: false
- id: poetry-pytest
name: Run Tests with Poetry and Pytest
entry: poetry run pytest
language: system
pass_filenames: false
types: [python]
7 changes: 3 additions & 4 deletions pylspclient/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Any
__all__: list[Any] = []

from pylspclient.json_rpc_endpoint import JsonRpcEndpoint
from pylspclient.lsp_client import LspClient
from pylspclient.lsp_endpoint import LspEndpoint
from pylspclient import lsp_errors
from .json_rpc_endpoint import JsonRpcEndpoint
from .lsp_client import LspClient
from .lsp_endpoint import LspEndpoint
58 changes: 33 additions & 25 deletions pylspclient/json_rpc_endpoint.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import print_function
import json
from pylspclient.lsp_errors import ErrorCodes, ResponseError
import threading
from typing import Any, IO
from .lsp_errors import ErrorCodes, ResponseError

JSON_RPC_REQ_FORMAT = "Content-Length: {json_string_len}\r\n\r\n{json_string}"
LEN_HEADER = "Content-Length: "
Expand All @@ -12,81 +12,89 @@
# TODO: add content-type


class MyEncoder(json.JSONEncoder):
class MyEncoder(json.JSONEncoder):
"""
Encodes an object in JSON
"""
def default(self, o: Any): # pylint: disable=E0202
return o.__dict__

def default(self, o: Any): # pylint: disable=E0202
return o.__dict__


class JsonRpcEndpoint(object):
'''
"""
Thread safe JSON RPC endpoint implementation. Responsible to recieve and send JSON RPC messages, as described in the
protocol. More information can be found: https://www.jsonrpc.org/
'''
"""

def __init__(self, stdin: IO, stdout: IO):
self.stdin = stdin
self.stdout = stdout
self.read_lock = threading.Lock()
self.write_lock = threading.Lock()
self.read_lock = threading.Lock()
self.write_lock = threading.Lock()

@staticmethod
def __add_header(json_string: str) -> str:
'''
"""
Adds a header for the given json string

:param str json_string: The string
:return: the string with the header
'''
return JSON_RPC_REQ_FORMAT.format(json_string_len=len(json_string), json_string=json_string)

"""
return JSON_RPC_REQ_FORMAT.format(
json_string_len=len(json_string), json_string=json_string
)

def send_request(self, message: Any) -> None:
'''
"""
Sends the given message.

:param dict message: The message to send.
'''
:param dict message: The message to send.
"""
json_string = json.dumps(message, cls=MyEncoder)
jsonrpc_req = self.__add_header(json_string)
with self.write_lock:
self.stdin.write(jsonrpc_req.encode())
self.stdin.flush()


def recv_response(self) -> Any:
'''
"""
Recives a message.

:return: a message
'''
"""
with self.read_lock:
message_size = None
while True:
#read header
# read header
line = self.stdout.readline()
if not line:
# server quit
return None
line = line.decode("utf-8")
if not line.endswith("\r\n"):
raise ResponseError(ErrorCodes.ParseError, "Bad header: missing newline")
#remove the "\r\n"
raise ResponseError(
ErrorCodes.ParseError, "Bad header: missing newline"
)
# remove the "\r\n"
line = line[:-2]
if line == "":
# done with the headers
break
elif line.startswith(LEN_HEADER):
line = line[len(LEN_HEADER):]
line = line[len(LEN_HEADER) :]
if not line.isdigit():
raise ResponseError(ErrorCodes.ParseError, "Bad header: size is not int")
raise ResponseError(
ErrorCodes.ParseError, "Bad header: size is not int"
)
message_size = int(line)
elif line.startswith(TYPE_HEADER):
# nothing todo with type for now.
pass
else:
raise ResponseError(ErrorCodes.ParseError, "Bad header: unkown header")
raise ResponseError(
ErrorCodes.ParseError, "Bad header: unkown header"
)
if not message_size:
raise ResponseError(ErrorCodes.ParseError, "Bad header: missing size")

Expand Down
Loading
Loading