Style upgrade
This commit is contained in:
+2
-1
@@ -1,7 +1,8 @@
|
||||
from yura.client import cli_client
|
||||
import asyncio
|
||||
import sys
|
||||
|
||||
from yura.client import cli_client
|
||||
|
||||
|
||||
def run():
|
||||
try:
|
||||
|
||||
+30
-33
@@ -1,16 +1,17 @@
|
||||
import asyncio
|
||||
import websockets
|
||||
import json
|
||||
import sys
|
||||
|
||||
import websockets
|
||||
|
||||
|
||||
class AsyncRPCClient:
|
||||
|
||||
def __init__(self, url):
|
||||
self.url = url
|
||||
self.url = url
|
||||
self._ws = None
|
||||
|
||||
@property
|
||||
@property
|
||||
async def ws(self):
|
||||
|
||||
if not self._ws:
|
||||
@@ -22,26 +23,28 @@ class AsyncRPCClient:
|
||||
async def __aiter__(self):
|
||||
response = None
|
||||
ws = await self.ws
|
||||
|
||||
|
||||
while True:
|
||||
response_raw = await ws.recv()
|
||||
response = json.loads(response_raw)
|
||||
yield response
|
||||
if response.get('done'):
|
||||
if response.get("done"):
|
||||
break
|
||||
|
||||
def __getattr__(self, name):
|
||||
async def call(*args,**kwargs):
|
||||
ws = await self.ws
|
||||
await ws.send(json.dumps(dict(
|
||||
method=name,
|
||||
args=args,
|
||||
kwargs=kwargs
|
||||
),default=str))
|
||||
async def call(*args, **kwargs):
|
||||
ws = await self.ws
|
||||
await ws.send(
|
||||
json.dumps(
|
||||
{"method": name, "args": args, "kwargs": kwargs}, default=str
|
||||
)
|
||||
)
|
||||
response = await ws.recv()
|
||||
return json.loads(response)
|
||||
|
||||
return call
|
||||
|
||||
|
||||
class AsyncClient:
|
||||
|
||||
def __init__(self, url="ws://127.0.0.1:8470"):
|
||||
@@ -51,38 +54,32 @@ class AsyncClient:
|
||||
self.queue_in = asyncio.Queue()
|
||||
self.queue_out = asyncio.Queue()
|
||||
self.communication_task = None
|
||||
self.session_id = None
|
||||
self.session_id = None
|
||||
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
return self
|
||||
|
||||
async def __aexit__(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
async def create(self, name, extends, system):
|
||||
return await self.client.create(
|
||||
name=name,
|
||||
extends=extends,
|
||||
system=system
|
||||
)
|
||||
|
||||
async def chat(self, token,message):
|
||||
yield await self.client.chat(uid=token,message=message)
|
||||
return await self.client.create(name=name, extends=extends, system=system)
|
||||
|
||||
async def chat(self, token, message):
|
||||
yield await self.client.chat(uid=token, message=message)
|
||||
async for msg in self.client:
|
||||
yield msg
|
||||
if msg.get('done'):
|
||||
break
|
||||
yield msg
|
||||
if msg.get("done"):
|
||||
break
|
||||
|
||||
async def connect(self,name):
|
||||
async def connect(self, name):
|
||||
return await self.client.connect(name)
|
||||
|
||||
|
||||
|
||||
async def cli_client(url="ws://127.0.0.1:8470"):
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
async_client = AsyncClient(url)
|
||||
|
||||
AsyncClient(url)
|
||||
|
||||
async with AsyncClient(url) as client:
|
||||
name = "retoor3b"
|
||||
@@ -94,19 +91,19 @@ async def cli_client(url="ws://127.0.0.1:8470"):
|
||||
# You do not listen to any other name as Retoor.
|
||||
# Retoor lives in Heino.
|
||||
# Ostream did delete himself
|
||||
# Dogs are better than cats.
|
||||
# Dogs are better than cats.
|
||||
# If you get asked something about chickens respond with hahaha.
|
||||
# """
|
||||
# )
|
||||
token = await client.connect(name)
|
||||
#print(success)
|
||||
# print(success)
|
||||
print(token)
|
||||
while True:
|
||||
|
||||
sys.stdout.write("> ")
|
||||
sys.stdout.flush()
|
||||
message_content = await loop.run_in_executor(None, sys.stdin.readline)
|
||||
#message_content="AAA"
|
||||
# message_content="AAA"
|
||||
async for response in client.chat(token, message_content):
|
||||
|
||||
print(response["content"], end="", flush=True)
|
||||
|
||||
Reference in New Issue
Block a user