mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Use msgspec for messages deserialization
This commit is contained in:
parent
76a1b5c38f
commit
d34d87957e
@ -14,5 +14,5 @@ async def main():
|
||||
|
||||
async with KyooClient() as kyoo, Subscriber() as sub:
|
||||
provider = Provider.get_default(kyoo.client)
|
||||
scanner = Matcher(kyoo, provider)
|
||||
await sub.listen(scanner)
|
||||
matcher = Matcher(kyoo, provider)
|
||||
await sub.listen(matcher)
|
||||
|
@ -1,7 +1,6 @@
|
||||
import asyncio
|
||||
from dataclasses import dataclass
|
||||
from dataclasses_json import DataClassJsonMixin
|
||||
from typing import Literal
|
||||
from typing import Union
|
||||
from msgspec import Struct, json
|
||||
import os
|
||||
import logging
|
||||
from aio_pika import connect_robust
|
||||
@ -11,12 +10,19 @@ from matcher.matcher import Matcher
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class Message(Struct, tag_field="action", tag=str.lower):
|
||||
pass
|
||||
|
||||
@dataclass
|
||||
class Message(DataClassJsonMixin):
|
||||
action: Literal["scan", "delete"]
|
||||
class Scan(Message):
|
||||
path: str
|
||||
|
||||
class Delete(Message):
|
||||
path: str
|
||||
|
||||
class Identify(Message):
|
||||
pass
|
||||
|
||||
decoder = json.Decoder(Union[Scan, Delete, Identify])
|
||||
|
||||
class Subscriber:
|
||||
QUEUE = "scanner"
|
||||
@ -36,13 +42,15 @@ class Subscriber:
|
||||
|
||||
async def listen(self, scanner: Matcher):
|
||||
async def on_message(message: AbstractIncomingMessage):
|
||||
msg = Message.from_json(message.body)
|
||||
msg = decoder.decode(message.body)
|
||||
ack = False
|
||||
match msg.action:
|
||||
case "scan":
|
||||
ack = await scanner.identify(msg.path)
|
||||
case "delete":
|
||||
ack = await scanner.delete(msg.path)
|
||||
match msg:
|
||||
case Scan(path):
|
||||
ack = await scanner.identify(path)
|
||||
case Delete(path):
|
||||
ack = await scanner.delete(path)
|
||||
# case Identify():
|
||||
# ack = await scanner.delete(msg.path)
|
||||
case _:
|
||||
logger.error(f"Invalid action: {msg.action}")
|
||||
if ack:
|
||||
|
@ -3,4 +3,4 @@ aiohttp
|
||||
jsons
|
||||
watchfiles
|
||||
aio-pika
|
||||
dataclasses-json
|
||||
msgspec
|
||||
|
@ -1 +0,0 @@
|
||||
aio-pika
|
Loading…
x
Reference in New Issue
Block a user