1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 11:34:18 +00:00

I think i improved some more things, but it's 3 am so i don't know

This commit is contained in:
Steffo 2020-08-05 03:02:50 +02:00
parent c581c8d08e
commit 10f3126d44
10 changed files with 32 additions and 16 deletions

View file

@ -19,6 +19,7 @@ import royalnet
# -- Project information -----------------------------------------------------
project = 'Royalnet'
# noinspection PyShadowingBuiltins
copyright = '2019, Stefano Pigozzi'
author = 'Stefano Pigozzi'
version = royalnet.__version__

View file

@ -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] = {}

View file

@ -8,6 +8,7 @@ from sqlalchemy.orm import *
import royalnet.utils as ru
# noinspection PyAttributeOutsideInit
class Token:
__tablename__ = "tokens"

View file

@ -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()

View file

@ -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.")

View file

@ -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

View file

@ -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`.

View file

@ -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")"

View file

@ -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)

View file

@ -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