fix: clearer api exposure
This commit is contained in:
@@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
|
|||||||
[project]
|
[project]
|
||||||
name = "common"
|
name = "common"
|
||||||
description = "Reusable code stubs"
|
description = "Reusable code stubs"
|
||||||
version = "0.1.1"
|
version = "0.1.2"
|
||||||
requires-python = ">=3.13"
|
requires-python = ">=3.13"
|
||||||
authors = [
|
authors = [
|
||||||
{ name="BreakerBear", email="breakerbear@autistic.men" },
|
{ name="BreakerBear", email="breakerbear@autistic.men" },
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
from jsonrpc2 import Request, Response, Error, History, ConnectionContext, define, remove, run
|
||||||
@@ -3,8 +3,7 @@ import logging
|
|||||||
import trio
|
import trio
|
||||||
import trio_websocket as ws
|
import trio_websocket as ws
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import Self, Callable
|
from typing import Self, Callable, Any
|
||||||
from ipaddress import IPv4Address
|
|
||||||
from itertools import repeat
|
from itertools import repeat
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
|
|
||||||
@@ -142,7 +141,14 @@ async def backend(listen='127.0.0.1', port=0):
|
|||||||
await server.run()
|
await server.run()
|
||||||
t.interrupt_main()
|
t.interrupt_main()
|
||||||
|
|
||||||
def run(logger: logging.Logger) -> tuple[IPv4Address, int]:
|
def define(method: str, handler: Callable[..., Any]):
|
||||||
|
if method in handlers: raise KeyError()
|
||||||
|
handlers[method] = handler
|
||||||
|
|
||||||
|
def remove(method: str) -> Callable[..., Any]:
|
||||||
|
return handlers.pop(method)
|
||||||
|
|
||||||
|
def run(logger: logging.Logger) -> str:
|
||||||
history = History()
|
history = History()
|
||||||
fmt = logger.handlers[0].formatter
|
fmt = logger.handlers[0].formatter
|
||||||
logger.addHandler(history)
|
logger.addHandler(history)
|
||||||
@@ -152,4 +158,4 @@ def run(logger: logging.Logger) -> tuple[IPv4Address, int]:
|
|||||||
thread.start()
|
thread.start()
|
||||||
|
|
||||||
endpoint = server.listeners[0]
|
endpoint = server.listeners[0]
|
||||||
return (endpoint.address, endpoint.port)
|
return f'ws://{endpoint.address}:{endpoint.port}'
|
||||||
|
|||||||
Reference in New Issue
Block a user