Skip to content

Commit

Permalink
Clean up repo (#13)
Browse files Browse the repository at this point in the history
* Remove unnecessary code

* Update types

* Add example for editorconfig

* Update classifiers

* Reformat

* Update readme
  • Loading branch information
Moosems authored Jun 14, 2024
1 parent 63bf8ef commit 7fc6659
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 24 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ The `COMMANDS` list contains all valid commands to request server output from. I
- "highlight"
- "editorconfig"

### `Token` type alias

The `Token` type alias helps your type checker when you work with `Token`'s from salve.

### `generic_tokens`

The `generic_tokens` list is a list of strings that define all of the generic token types returned by the server which can be mapped to colors for syntax highlighting.
Expand Down
20 changes: 20 additions & 0 deletions examples/simple_editorconfig_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from time import sleep

from salve_ipc import IPC, Response


def main():
context = IPC()

context.update_file("test", "")

context.request("editorconfig", file="test", file_path=__file__)

sleep(1)
output: Response | None = context.get_response("editorconfig")
print(output["result"]) # type: ignore
context.kill_IPC()


if __name__ == "__main__":
main()
21 changes: 7 additions & 14 deletions salve_ipc/ipc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pathlib import Path
from random import randint

from .misc import COMMANDS, Message, Notification, Request, Response
from .misc import COMMANDS, Notification, Request, Response
from .server import Server


Expand All @@ -28,7 +28,7 @@ def __init__(self, id_max: int = 15_000) -> None:
self.files: dict[str, str] = {}

self.response_queue: Queue[Response] = Queue()
self.requests_queue: Queue[Request] = Queue()
self.requests_queue: Queue[Request | Notification] = Queue()
self.client_end: Connection
self.main_server: Process
self.create_server()
Expand All @@ -49,23 +49,16 @@ def create_server(self) -> None:
for filename, data in files_copy.items():
self.update_file(filename, data)

def check_server(self) -> None:
"""Checks that the main_server is alive - internal API"""
if not self.main_server.is_alive():
self.create_server()

def send_message(self, message: Message) -> None:
"""Sends a Message to the main_server as provided by the argument message - internal API"""

self.requests_queue.put(message) # type: ignore

def create_message(self, type: str, **kwargs) -> None:
"""Creates a Message based on the args and kwawrgs provided. Highly flexible. - internal API"""
id = randint(1, self.id_max) # 0 is reserved for the empty case
while id in self.all_ids:
id = randint(1, self.id_max)
self.all_ids.append(id)

if not self.main_server.is_alive():
self.create_server()

match type:
case "request":
command = kwargs.get("command", "")
Expand All @@ -81,7 +74,7 @@ def create_message(self, type: str, **kwargs) -> None:
"text_range": kwargs.get("text_range", (1, -1)),
"file_path": kwargs.get("file_path", __file__),
}
self.send_message(request)
self.requests_queue.put(request)
case "notification":
notification: Notification = {
"id": id,
Expand All @@ -90,7 +83,7 @@ def create_message(self, type: str, **kwargs) -> None:
"file": kwargs.get("filename", ""),
"contents": kwargs.get("contents", ""),
}
self.send_message(notification)
self.requests_queue.put(notification)

def request(
self,
Expand Down
6 changes: 4 additions & 2 deletions salve_ipc/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Request(Message):
expected_keywords: NotRequired[list[str]] # autocomplete, replacements
current_word: NotRequired[str] # autocomplete, replacements
language: NotRequired[str] # highlight
text_range: NotRequired[tuple[int, int]]
text_range: NotRequired[tuple[int, int]] # highlight
file_path: NotRequired[Path | str] # editorconfig


Expand All @@ -41,4 +41,6 @@ class Response(Message):

cancelled: bool
command: NotRequired[str]
result: NotRequired[list[str | tuple[tuple[int, int], int, str]]]
result: NotRequired[
list[str | tuple[tuple[int, int], int, str]] | dict[str, str]
]
13 changes: 5 additions & 8 deletions salve_ipc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from pyeditorconfig import get_config

from .misc import COMMANDS, Message, Request, Response
from .misc import COMMANDS, Notification, Request, Response
from .server_functions import (
Token,
find_autocompletions,
Expand Down Expand Up @@ -38,18 +38,15 @@ def __init__(
self.run_tasks()
sleep(0.0025)

def write_message(self, message: Message) -> None:
self.response_queue.put(message) # type: ignore

def simple_id_response(self, id: int, cancelled: bool = True) -> None:
response: Response = {
"id": id,
"type": "response",
"cancelled": cancelled,
}
self.write_message(response)
self.response_queue.put(response)

def parse_line(self, message: Message) -> None:
def parse_line(self, message: Request | Notification) -> None:
id: int = message["id"]
match message["type"]:
case "notification":
Expand Down Expand Up @@ -118,9 +115,9 @@ def handle_request(self, request: Request) -> None:
"type": "response",
"cancelled": cancelled,
"command": command,
"result": result, # type: ignore
"result": result,
}
self.write_message(response)
self.response_queue.put(response)
self.newest_ids[command] = 0

def run_tasks(self) -> None:
Expand Down
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
python_requires=">=3.9",
license="MIT license",
classifiers=[
"Development Status :: 3 - Alpha",
"Operating System :: OS Independent", # I believe it can be classified as such, cannot test Windows
"Programming Language :: Python :: Implementation",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Typing :: Typed",
Expand Down

0 comments on commit 7fc6659

Please # to comment.