Skip to content

Commit 520a2ef

Browse files
change command handler strat to function decorator (#26)
* change command handler strat to function decorator * add handlers back to agent class
1 parent 0a6e975 commit 520a2ef

File tree

10 files changed

+279
-447
lines changed

10 files changed

+279
-447
lines changed

python/djls/agent.py

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,34 @@
11
from __future__ import annotations
22

3-
import logging
43
import struct
54
import sys
5+
from typing import Any
6+
from typing import cast
67

78
from google.protobuf.message import Message
89

9-
from .commands import COMMANDS
10-
from .commands import Command
10+
from .logging import configure_logging
1111
from .proto.v1 import messages_pb2
1212

13-
logger = logging.getLogger("djls")
14-
logger.setLevel(logging.DEBUG)
15-
16-
fh = logging.FileHandler("/tmp/djls_debug.log")
17-
fh.setLevel(logging.DEBUG)
18-
19-
ch = logging.StreamHandler(sys.stderr)
20-
ch.setLevel(logging.DEBUG)
21-
22-
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
23-
fh.setFormatter(formatter)
24-
ch.setFormatter(formatter)
25-
26-
logger.addHandler(fh)
27-
logger.addHandler(ch)
13+
logger = configure_logging()
2814

2915

3016
class LSPAgent:
3117
def __init__(self):
32-
self._commands: dict[str, Command] = {cmd.name: cmd() for cmd in COMMANDS}
18+
from .handlers import handlers
19+
20+
self._handlers = handlers
3321
logger.debug(
34-
"LSPAgent initialized with commands: %s", list(self._commands.keys())
22+
"LSPAgent initialized with handlers: %s", list(self._handlers.keys())
3523
)
3624

37-
def serve(self):
25+
async def serve(self):
3826
print("ready", flush=True)
3927

4028
try:
4129
import django
4230

4331
django.setup()
44-
4532
except Exception as e:
4633
error_response = self.create_error(messages_pb2.Error.DJANGO_ERROR, str(e))
4734
self.write_message(error_response)
@@ -52,7 +39,7 @@ def serve(self):
5239
if not data:
5340
break
5441

55-
response = self.handle_request(data)
42+
response = await self.handle_request(data)
5643
self.write_message(response)
5744

5845
except Exception as e:
@@ -71,23 +58,30 @@ def read_message(self) -> bytes | None:
7158
logger.debug("Read data bytes: %r", data)
7259
return data
7360

74-
def handle_request(self, request_data: bytes) -> Message:
61+
async def handle_request(self, request_data: bytes) -> Message:
7562
request = messages_pb2.Request()
7663
request.ParseFromString(request_data)
7764

7865
command_name = request.WhichOneof("command")
7966
logger.debug("Command name: %s", command_name)
80-
command = self._commands.get(command_name)
8167

82-
if not command:
68+
if not command_name:
69+
logger.error("No command specified")
70+
return self.create_error(
71+
messages_pb2.Error.INVALID_REQUEST, "No command specified"
72+
)
73+
74+
handler = self._handlers.get(command_name)
75+
if not handler:
8376
logger.error("Unknown command: %s", command_name)
8477
return self.create_error(
8578
messages_pb2.Error.INVALID_REQUEST, f"Unknown command: {command_name}"
8679
)
8780

8881
try:
89-
result = command.execute(getattr(request, command_name))
90-
return messages_pb2.Response(**{command_name: result})
82+
command_message = getattr(request, command_name)
83+
result = await handler(command_message)
84+
return messages_pb2.Response(**{command_name: cast(Any, result)})
9185
except Exception as e:
9286
logger.exception("Error executing command")
9387
return self.create_error(messages_pb2.Error.UNKNOWN, str(e))
@@ -110,14 +104,14 @@ def create_error(
110104
return response
111105

112106

113-
def main() -> None:
107+
async def main() -> None:
114108
logger.debug("Starting DJLS...")
115109

116110
try:
117111
logger.debug("Initializing LSPAgent...")
118112
agent = LSPAgent()
119113
logger.debug("Starting LSPAgent serve...")
120-
agent.serve()
114+
await agent.serve()
121115
except KeyboardInterrupt:
122116
logger.debug("Received KeyboardInterrupt")
123117
sys.exit(0)
@@ -128,4 +122,6 @@ def main() -> None:
128122

129123

130124
if __name__ == "__main__":
131-
main()
125+
import asyncio
126+
127+
asyncio.run(main())

python/djls/commands.py

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

0 commit comments

Comments
 (0)