mirror of
https://github.com/chatmail/core.git
synced 2026-05-08 17:36:29 +03:00
Turn start_rpc_server into a context manager
This commit is contained in:
@@ -7,46 +7,46 @@ import deltachat_rpc_client as dc
|
|||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
rpc = await dc.start_rpc_server()
|
async with dc.start_rpc_server() as rpc:
|
||||||
deltachat = dc.Deltachat(rpc)
|
deltachat = dc.Deltachat(rpc)
|
||||||
system_info = await deltachat.get_system_info()
|
system_info = await deltachat.get_system_info()
|
||||||
logging.info("Running deltachat core %s", system_info["deltachat_core_version"])
|
logging.info("Running deltachat core %s", system_info["deltachat_core_version"])
|
||||||
|
|
||||||
accounts = await deltachat.get_all_accounts()
|
accounts = await deltachat.get_all_accounts()
|
||||||
account = accounts[0] if accounts else await deltachat.add_account()
|
account = accounts[0] if accounts else await deltachat.add_account()
|
||||||
|
|
||||||
await account.set_config("bot", "1")
|
await account.set_config("bot", "1")
|
||||||
if not await account.is_configured():
|
if not await account.is_configured():
|
||||||
logging.info("Account is not configured, configuring")
|
logging.info("Account is not configured, configuring")
|
||||||
await account.set_config("addr", sys.argv[1])
|
await account.set_config("addr", sys.argv[1])
|
||||||
await account.set_config("mail_pw", sys.argv[2])
|
await account.set_config("mail_pw", sys.argv[2])
|
||||||
await account.configure()
|
await account.configure()
|
||||||
logging.info("Configured")
|
logging.info("Configured")
|
||||||
else:
|
else:
|
||||||
logging.info("Account is already configured")
|
logging.info("Account is already configured")
|
||||||
await deltachat.start_io()
|
await deltachat.start_io()
|
||||||
|
|
||||||
async def process_messages():
|
async def process_messages():
|
||||||
for message in await account.get_fresh_messages_in_arrival_order():
|
for message in await account.get_fresh_messages_in_arrival_order():
|
||||||
snapshot = await message.get_snapshot()
|
snapshot = await message.get_snapshot()
|
||||||
if not snapshot.is_info:
|
if not snapshot.is_info:
|
||||||
await snapshot.chat.send_text(snapshot.text)
|
await snapshot.chat.send_text(snapshot.text)
|
||||||
await snapshot.message.mark_seen()
|
await snapshot.message.mark_seen()
|
||||||
|
|
||||||
# Process old messages.
|
# Process old messages.
|
||||||
await process_messages()
|
await process_messages()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
event = await account.wait_for_event()
|
event = await account.wait_for_event()
|
||||||
if event["type"] == "Info":
|
if event["type"] == "Info":
|
||||||
logging.info("%s", event["msg"])
|
logging.info("%s", event["msg"])
|
||||||
elif event["type"] == "Warning":
|
elif event["type"] == "Warning":
|
||||||
logging.warning("%s", event["msg"])
|
logging.warning("%s", event["msg"])
|
||||||
elif event["type"] == "Error":
|
elif event["type"] == "Error":
|
||||||
logging.error("%s", event["msg"])
|
logging.error("%s", event["msg"])
|
||||||
elif event["type"] == "IncomingMsg":
|
elif event["type"] == "IncomingMsg":
|
||||||
logging.info("Got an incoming message")
|
logging.info("Got an incoming message")
|
||||||
await process_messages()
|
await process_messages()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import asyncio
|
|||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
|
||||||
@@ -73,6 +74,7 @@ class Rpc:
|
|||||||
return method
|
return method
|
||||||
|
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
async def start_rpc_server(*args, **kwargs):
|
async def start_rpc_server(*args, **kwargs):
|
||||||
proc = await asyncio.create_subprocess_exec(
|
proc = await asyncio.create_subprocess_exec(
|
||||||
"deltachat-rpc-server",
|
"deltachat-rpc-server",
|
||||||
@@ -82,7 +84,10 @@ async def start_rpc_server(*args, **kwargs):
|
|||||||
**kwargs
|
**kwargs
|
||||||
)
|
)
|
||||||
rpc = Rpc(proc)
|
rpc = Rpc(proc)
|
||||||
return rpc
|
try:
|
||||||
|
yield rpc
|
||||||
|
finally:
|
||||||
|
proc.terminate()
|
||||||
|
|
||||||
|
|
||||||
async def new_online_account():
|
async def new_online_account():
|
||||||
|
|||||||
@@ -10,9 +10,10 @@ from deltachat_rpc_client import Deltachat
|
|||||||
|
|
||||||
@pytest_asyncio.fixture
|
@pytest_asyncio.fixture
|
||||||
async def rpc(tmp_path):
|
async def rpc(tmp_path):
|
||||||
return await deltachat_rpc_client.start_rpc_server(
|
async with deltachat_rpc_client.start_rpc_server(
|
||||||
env={**os.environ, "DC_ACCOUNTS_PATH": str(tmp_path / "accounts")}
|
env={**os.environ, "DC_ACCOUNTS_PATH": str(tmp_path / "accounts")}
|
||||||
)
|
) as rpc:
|
||||||
|
yield rpc
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
|
|||||||
Reference in New Issue
Block a user