From 7beb2fb193e92cf57432ae49e67eff55201333df Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 1 Apr 2019 15:38:59 +0200 Subject: [PATCH] Realize everything you were doing was a mistake and rollback --- royalnet/commands/sync.py | 26 ++++++++++++++++++++++++++ royalnet/utils/call.py | 9 +++++---- 2 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 royalnet/commands/sync.py diff --git a/royalnet/commands/sync.py b/royalnet/commands/sync.py new file mode 100644 index 00000000..741ee9f4 --- /dev/null +++ b/royalnet/commands/sync.py @@ -0,0 +1,26 @@ +import typing +from telegram import Update, User +from ..utils import Command, CommandArgs, Call +from ..database.tables import Royal + + +class SyncCommand(Command): + + command_name = "sync" + command_title = "Connect your current account to Royalnet" + + require_alchemy_tables = [Royal] + + async def common(self, call: Call, args: CommandArgs): + raise NotImplementedError() + + async def telegram(self, call: Call, args: CommandArgs): + update: Update = args.kwargs["update"] + # Find the user + user: typing.Optional[User] = update.effective_user + if user is None: + raise ValueError("Trying to sync a None user.") + # Find the Royal + royal = call.session.query(call.interface_alchemy.Royal).filter_by(username=args[0]).one_or_none() + if royal is None: + await call.reply("⚠️ Non esiste alcun account Royalnet con quel nome.") diff --git a/royalnet/utils/call.py b/royalnet/utils/call.py index 5e3512d3..2fe4ddf2 100644 --- a/royalnet/utils/call.py +++ b/royalnet/utils/call.py @@ -2,7 +2,8 @@ import typing import asyncio from ..network.messages import Message from .command import Command, CommandArgs -from ..database import Alchemy +if typing.TYPE_CHECKING: + from ..database import Alchemy loop = asyncio.get_event_loop() @@ -14,7 +15,7 @@ class Call: # These parameters / methods should be overridden interface_name = NotImplemented interface_obj = NotImplemented - interface_alchemy: Alchemy = NotImplemented + interface_alchemy: "Alchemy" = NotImplemented async def reply(self, text: str): """Send a text message to the channel the call was made.""" @@ -36,7 +37,7 @@ class Call: async def session_init(self): if not self.command.require_alchemy_tables: return - self.session = await loop.run_in_executor(self.interface_alchemy.Session) + self.session = await loop.run_in_executor(None, self.interface_alchemy.Session) async def session_end(self): if not self.session: @@ -52,5 +53,5 @@ class Call: try: result = await coroutine(self.command, self, CommandArgs(*self.args, **self.kwargs)) finally: - self.session.close() + await self.session_end() return result