From fe73d3d897e6bc34d1095c4a3906c78b5e112058 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 28 Nov 2019 02:30:08 +0100 Subject: [PATCH] Fix a few bugs encountered while developing royalpack --- .idea/royalnet.iml | 2 +- royalnet/backpack/events/__init__.py | 6 +----- royalnet/commands/commandinterface.py | 7 +++++-- royalnet/serf/discord/discordserf.py | 8 +++++--- royalnet/serf/serf.py | 9 ++++----- royalnet/serf/telegram/telegramserf.py | 4 +++- 6 files changed, 19 insertions(+), 17 deletions(-) diff --git a/.idea/royalnet.iml b/.idea/royalnet.iml index 3dcd20ac..e2972f70 100644 --- a/.idea/royalnet.iml +++ b/.idea/royalnet.iml @@ -9,7 +9,7 @@ - + diff --git a/royalnet/backpack/events/__init__.py b/royalnet/backpack/events/__init__.py index eac5e669..5cad9296 100644 --- a/royalnet/backpack/events/__init__.py +++ b/royalnet/backpack/events/__init__.py @@ -3,12 +3,8 @@ from .exception import ExceptionEvent # Enter the commands of your Pack here! available_events = [ - + ExceptionEvent, ] -# noinspection PyUnreachableCode -if __debug__: - available_events.append(ExceptionEvent) - # Don't change this, it should automatically generate __all__ __all__ = [command.__name__ for command in available_events] diff --git a/royalnet/commands/commandinterface.py b/royalnet/commands/commandinterface.py index d8c18db3..65b22e5e 100644 --- a/royalnet/commands/commandinterface.py +++ b/royalnet/commands/commandinterface.py @@ -34,7 +34,6 @@ class CommandInterface: Example: A reference to a :class:`~royalnet.constellation.Constellation`.""" - @property def alchemy(self) -> "Alchemy": """A shortcut for :attr:`.serf.alchemy`.""" @@ -43,7 +42,11 @@ class CommandInterface: @property def loop(self) -> AbstractEventLoop: """A shortcut for :attr:`.serf.loop`.""" - return self.serf.loop + if self.serf: + return self.serf.loop + else: + # TODO + raise Exception("TODO") def __init__(self, cfg: Dict[str, Any]): self.cfg: Dict[str, Any] = cfg diff --git a/royalnet/serf/discord/discordserf.py b/royalnet/serf/discord/discordserf.py index 52e889c5..c3b06ec2 100644 --- a/royalnet/serf/discord/discordserf.py +++ b/royalnet/serf/discord/discordserf.py @@ -1,4 +1,4 @@ -import asyncio +import asyncio as aio import logging import warnings from typing import * @@ -36,6 +36,7 @@ class DiscordSerf(Serf): _identity_column = "discord_id" def __init__(self, + loop: aio.AbstractEventLoop, alchemy_cfg: Dict[str, Any], herald_cfg: Dict[str, Any], sentry_cfg: Dict[str, Any], @@ -45,7 +46,8 @@ class DiscordSerf(Serf): if discord is None: raise ImportError("'discord' extra is not installed") - super().__init__(alchemy_cfg=alchemy_cfg, + super().__init__(loop=loop, + alchemy_cfg=alchemy_cfg, herald_cfg=herald_cfg, sentry_cfg=sentry_cfg, packs_cfg=packs_cfg, @@ -80,7 +82,7 @@ class DiscordSerf(Serf): def __init__(data, interface: CommandInterface, session, - loop: asyncio.AbstractEventLoop, + loop: aio.AbstractEventLoop, message: "discord.Message"): super().__init__(interface=interface, session=session, loop=loop) data.message = message diff --git a/royalnet/serf/serf.py b/royalnet/serf/serf.py index b6f4cbb2..5b1721ad 100644 --- a/royalnet/serf/serf.py +++ b/royalnet/serf/serf.py @@ -40,10 +40,13 @@ class Serf: _identity_column: str = NotImplemented def __init__(self, + loop: aio.AbstractEventLoop, alchemy_cfg: Dict[str, Any], herald_cfg: Dict[str, Any], packs_cfg: Dict[str, Any], **_): + self.loop: Optional[aio.AbstractEventLoop] = loop + """The event loop this Serf is running on.""" # Import packs pack_names = packs_cfg["active"] @@ -131,9 +134,6 @@ class Serf: self.init_herald(herald_cfg) log.info(f"Herald: enabled") - self.loop: Optional[aio.AbstractEventLoop] = None - """The event loop this Serf is running on.""" - def init_alchemy(self, alchemy_cfg: Dict[str, Any], tables: Set[type]) -> None: """Create and initialize the :class:`Alchemy` with the required tables, and find the link between the master table and the identity table.""" @@ -320,9 +320,8 @@ class Serf: except ImportError: log.info("Sentry: not installed") - serf = cls(**kwargs) + serf = cls(loop=aio.get_event_loop(), **kwargs) - serf.loop = aio.get_event_loop() try: serf.loop.run_until_complete(serf.run()) except Exception as e: diff --git a/royalnet/serf/telegram/telegramserf.py b/royalnet/serf/telegram/telegramserf.py index 72111d55..6a39df5a 100644 --- a/royalnet/serf/telegram/telegramserf.py +++ b/royalnet/serf/telegram/telegramserf.py @@ -32,6 +32,7 @@ class TelegramSerf(Serf): _identity_column = "tg_id" def __init__(self, + loop: aio.AbstractEventLoop, alchemy_cfg: Dict[str, Any], herald_cfg: Dict[str, Any], sentry_cfg: Dict[str, Any], @@ -41,7 +42,8 @@ class TelegramSerf(Serf): if telegram is None: raise ImportError("'telegram' extra is not installed") - super().__init__(alchemy_cfg=alchemy_cfg, + super().__init__(loop=loop, + alchemy_cfg=alchemy_cfg, herald_cfg=herald_cfg, sentry_cfg=sentry_cfg, packs_cfg=packs_cfg,