make a Server interface, interface.IServer

This commit is contained in:
jesopo 2020-04-01 23:25:44 +01:00
parent fd934b1101
commit be31fe1936
3 changed files with 61 additions and 32 deletions

52
ircrobots/interface.py Normal file
View file

@ -0,0 +1,52 @@
from typing import Optional
from enum import IntEnum
from dataclasses import dataclass
from ircstates import Server
from irctokens import Line
from .ircv3 import Capability
@dataclass
class ConnectionParams(object):
nickname: str
host: str
port: int
ssl: bool
username: Optional[str] = None
realname: Optional[str] = None
bindhost: Optional[str] = None
class SendPriority(IntEnum):
HIGH = 0
MEDIUM = 10
LOW = 20
DEFAULT = MEDIUM
class PriorityLine(object):
def __init__(self, priority: int, line: Line):
self.priority = priority
self.line = line
def __lt__(self, other: "PriorityLine") -> bool:
return self.priority < other.priority
class IServer(Server):
params: ConnectionParams
async def send_raw(self, line: str, priority=SendPriority.DEFAULT):
pass
async def send(self, line: Line, priority=SendPriority.DEFAULT):
pass
def set_throttle(self, rate: int, time: float):
pass
async def connect(self, params: ConnectionParams):
pass
async def queue_capability(self, cap: Capability):
pass
async def line_written(self, line: Line):
pass

View file

@ -6,41 +6,18 @@ from enum import IntEnum
from dataclasses import dataclass from dataclasses import dataclass
from asyncio_throttle import Throttler from asyncio_throttle import Throttler
from ircstates import Server as BaseServer
from ircstates import Emit from ircstates import Emit
from irctokens import build, Line, tokenise from irctokens import build, Line, tokenise
from .ircv3 import Capability, CAPS from .ircv3 import Capability, CAPS
from .interface import ConnectionParams, IServer, PriorityLine, SendPriority
sc = ssl.create_default_context(ssl.Purpose.SERVER_AUTH) sc = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
THROTTLE_RATE = 4 # lines THROTTLE_RATE = 4 # lines
THROTTLE_TIME = 2 # seconds THROTTLE_TIME = 2 # seconds
@dataclass class Server(IServer):
class ConnectionParams(object):
nickname: str
host: str
port: int
ssl: bool
username: Optional[str] = None
realname: Optional[str] = None
bindhost: Optional[str] = None
class SendPriority(IntEnum):
HIGH = 0
MEDIUM = 10
LOW = 20
DEFAULT = MEDIUM
class PriorityLine(object):
def __init__(self, priority: int, line: Line):
self.priority = priority
self.line = line
def __lt__(self, other: "PriorityLine") -> bool:
return self.priority < other.priority
class Server(BaseServer):
_reader: asyncio.StreamReader _reader: asyncio.StreamReader
_writer: asyncio.StreamWriter _writer: asyncio.StreamWriter
params: ConnectionParams params: ConnectionParams
@ -93,9 +70,8 @@ class Server(BaseServer):
if matches: if matches:
self._requested_caps = matches self._requested_caps = matches
await self.send(build("CAP", ["REQ", " ".join(matches)])) await self.send(build("CAP", ["REQ", " ".join(matches)]))
async def _cap_ack(self, line: Line): async def _cap_ack(self, emit: Emit):
caps = line.params[2].split(" ") for cap in (emit.tokens or []):
for cap in caps:
if cap in self._requested_caps: if cap in self._requested_caps:
self._requested_caps.remove(cap) self._requested_caps.remove(cap)
if not self._requested_caps: if not self._requested_caps:
@ -106,7 +82,7 @@ class Server(BaseServer):
if emit.subcommand == "LS" and emit.finished: if emit.subcommand == "LS" and emit.finished:
await self._cap_ls_done() await self._cap_ls_done()
elif emit.subcommand in ["ACK", "NAK"]: elif emit.subcommand in ["ACK", "NAK"]:
await self._cap_ack(line) await self._cap_ack(emit)
async def _on_read_line(self, line: Line): async def _on_read_line(self, line: Line):
pass pass

View file

@ -1,2 +1,3 @@
ircstates ==0.8.0 ircstates ==0.8.1
asyncio-throttle ==1.0.1 asyncio-throttle ==1.0.1
dataclasses ==0.6