From 10f3126d440bce2fb151d0c3ff506e5d3defc732 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 5 Aug 2020 03:02:50 +0200 Subject: [PATCH] I think i improved some more things, but it's 3 am so i don't know --- docs_source/conf.py | 1 + royalnet/alchemy/alchemy.py | 11 +++++++---- royalnet/backpack/tables/tokens.py | 1 + royalnet/commands/heraldevent.py | 15 ++++++++------- royalnet/constellation/constellation.py | 2 +- royalnet/constellation/pagestar.py | 4 +++- royalnet/constellation/star.py | 4 +++- royalnet/herald/response.py | 6 +++++- royalnet/serf/serf.py | 3 ++- royalnet/utils/sentry.py | 1 + 10 files changed, 32 insertions(+), 16 deletions(-) diff --git a/docs_source/conf.py b/docs_source/conf.py index 77a8619c..84027802 100644 --- a/docs_source/conf.py +++ b/docs_source/conf.py @@ -19,6 +19,7 @@ import royalnet # -- Project information ----------------------------------------------------- project = 'Royalnet' +# noinspection PyShadowingBuiltins copyright = '2019, Stefano Pigozzi' author = 'Stefano Pigozzi' version = royalnet.__version__ diff --git a/royalnet/alchemy/alchemy.py b/royalnet/alchemy/alchemy.py index 34964c6a..34b3abd7 100644 --- a/royalnet/alchemy/alchemy.py +++ b/royalnet/alchemy/alchemy.py @@ -2,7 +2,6 @@ from contextlib import contextmanager, asynccontextmanager from typing import * from sqlalchemy import create_engine -from sqlalchemy.engine import Engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative.api import DeclarativeMeta from sqlalchemy.orm import sessionmaker @@ -12,10 +11,14 @@ from sqlalchemy.schema import Table from royalnet.alchemy.errors import TableNotFoundError from royalnet.utils import asyncify +if TYPE_CHECKING: + # noinspection PyProtectedMember + from sqlalchemy.engine import Engine + class Alchemy: - """A wrapper around :mod:`sqlalchemy.orm` that allows the instantiation of multiple engines at once while maintaining - a single declarative class for all of them.""" + """A wrapper around :mod:`sqlalchemy.orm` that allows the instantiation of multiple engines at once while + maintaining a single declarative class for all of them.""" def __init__(self, database_uri: str, tables: Set): """Create a new :class:`.Alchemy` object. @@ -28,7 +31,7 @@ class Alchemy: if database_uri.startswith("sqlite"): raise NotImplementedError("sqlite databases aren't supported, as they can't be used in multithreaded" " applications") - self._engine: Engine = create_engine(database_uri) + self._engine: "Engine" = create_engine(database_uri) self._Base = declarative_base(bind=self._engine) self.Session: sessionmaker = sessionmaker(bind=self._engine) self._tables: Dict[str, Table] = {} diff --git a/royalnet/backpack/tables/tokens.py b/royalnet/backpack/tables/tokens.py index 16b82238..fa74a879 100644 --- a/royalnet/backpack/tables/tokens.py +++ b/royalnet/backpack/tables/tokens.py @@ -8,6 +8,7 @@ from sqlalchemy.orm import * import royalnet.utils as ru +# noinspection PyAttributeOutsideInit class Token: __tablename__ = "tokens" diff --git a/royalnet/commands/heraldevent.py b/royalnet/commands/heraldevent.py index 504f3568..2883686a 100644 --- a/royalnet/commands/heraldevent.py +++ b/royalnet/commands/heraldevent.py @@ -1,8 +1,9 @@ +from typing import TYPE_CHECKING, Union import asyncio as aio -from typing import TYPE_CHECKING if TYPE_CHECKING: from ..serf import Serf + from ..constellation import Constellation class HeraldEvent: @@ -11,19 +12,19 @@ class HeraldEvent: name = NotImplemented """The event_name that will trigger this event.""" - def __init__(self, serf: "Serf", config): - self.serf: "Serf" = serf + def __init__(self, parent: Union["Serf", "Constellation"], config): + self.parent: Union["Serf", "Constellation"] = parent self.config = config @property def alchemy(self): - """A shortcut for :attr:`.interface.alchemy`.""" - return self.serf.alchemy + """A shortcut for :attr:`.parent.alchemy`.""" + return self.parent.alchemy @property def loop(self) -> aio.AbstractEventLoop: - """A shortcut for :attr:`.interface.loop`.""" - return self.serf.loop + """A shortcut for :attr:`.parent.loop`.""" + return self.parent.loop async def run(self, **kwargs): raise NotImplementedError() diff --git a/royalnet/constellation/constellation.py b/royalnet/constellation/constellation.py index 576a43b9..ce712733 100644 --- a/royalnet/constellation/constellation.py +++ b/royalnet/constellation/constellation.py @@ -220,7 +220,7 @@ class Constellation: for SelectedEvent in events: # Initialize the event try: - event = SelectedEvent(serf=self, config=pack_cfg) + event = SelectedEvent(constellation=self, config=pack_cfg) except Exception as e: log.error(f"Skipping: " f"{SelectedEvent.__qualname__} - {e.__class__.__qualname__} in the initialization.") diff --git a/royalnet/constellation/pagestar.py b/royalnet/constellation/pagestar.py index e6a0b1ba..3efd8bde 100644 --- a/royalnet/constellation/pagestar.py +++ b/royalnet/constellation/pagestar.py @@ -1,7 +1,9 @@ +from abc import ABCMeta + from .star import Star -class PageStar(Star): +class PageStar(Star, metaclass=ABCMeta): """A PageStar is a class representing a single route of the website (for example, ``/api/user/get``). To create a new website route you should create a new class inheriting from this class with a function overriding diff --git a/royalnet/constellation/star.py b/royalnet/constellation/star.py index 0de8e6ca..53ca57f5 100644 --- a/royalnet/constellation/star.py +++ b/royalnet/constellation/star.py @@ -1,5 +1,6 @@ from typing import * +import abc from starlette.requests import Request from starlette.responses import Response @@ -10,7 +11,7 @@ if TYPE_CHECKING: import sqlalchemy.orm.session -class Star: +class Star(metaclass=abc.ABCMeta): """A Star is a class representing a part of the website. It shouldn't be used directly: please use :class:`PageStar` and :class:`ExceptionStar` instead!""" @@ -19,6 +20,7 @@ class Star: self.constellation: "Constellation" = constellation self.config: "ConfigDict" = config + @abc.abstractmethod async def page(self, request: Request) -> Response: """The function generating the :class:`~starlette.Response` to a web :class:`~starlette.Request`. diff --git a/royalnet/herald/response.py b/royalnet/herald/response.py index 460fefc8..2b09d403 100644 --- a/royalnet/herald/response.py +++ b/royalnet/herald/response.py @@ -47,4 +47,8 @@ class ResponseFailure(Response): self.extra_info: Optional[dict] = extra_info def __repr__(self): - return f"{self.__class__.__qualname__}(name={self.name}, description={self.description}, extra_info={self.extra_info})" + return f"{self.__class__.__qualname__}(" \ + f"name={self.name}, " \ + f"description={self.description}, " \ + f"extra_info={self.extra_info}" \ + f")" diff --git a/royalnet/serf/serf.py b/royalnet/serf/serf.py index 9852ec99..57fa3766 100644 --- a/royalnet/serf/serf.py +++ b/royalnet/serf/serf.py @@ -285,7 +285,8 @@ class Serf(abc.ABC): finally: await data.session_close() - async def press(self, key: rc.KeyboardKey, data: rc.CommandData): + @staticmethod + async def press(key: rc.KeyboardKey, data: rc.CommandData): log.info(f"Calling key_callback: {repr(key)}") try: await key.press(data) diff --git a/royalnet/utils/sentry.py b/royalnet/utils/sentry.py index 4534f465..ac8f393e 100644 --- a/royalnet/utils/sentry.py +++ b/royalnet/utils/sentry.py @@ -40,6 +40,7 @@ def sentry_exc(exc: Exception, with sentry_sdk.configure_scope() as scope: scope.set_level(level.lower()) sentry_sdk.capture_exception(exc) + # noinspection PyUnresolvedReferences,PyProtectedMember level_int: int = logging._nameToLevel[level.upper()] log.log(level_int, f"Captured {level.capitalize()}: {exc}") # If started in debug mode (without -O), raise the exception, allowing you to see its source