mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-23 19:44:20 +00:00
I think i improved some more things, but it's 3 am so i don't know
This commit is contained in:
parent
c581c8d08e
commit
10f3126d44
10 changed files with 32 additions and 16 deletions
|
@ -19,6 +19,7 @@ import royalnet
|
||||||
# -- Project information -----------------------------------------------------
|
# -- Project information -----------------------------------------------------
|
||||||
|
|
||||||
project = 'Royalnet'
|
project = 'Royalnet'
|
||||||
|
# noinspection PyShadowingBuiltins
|
||||||
copyright = '2019, Stefano Pigozzi'
|
copyright = '2019, Stefano Pigozzi'
|
||||||
author = 'Stefano Pigozzi'
|
author = 'Stefano Pigozzi'
|
||||||
version = royalnet.__version__
|
version = royalnet.__version__
|
||||||
|
|
|
@ -2,7 +2,6 @@ from contextlib import contextmanager, asynccontextmanager
|
||||||
from typing import *
|
from typing import *
|
||||||
|
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
from sqlalchemy.engine import Engine
|
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
from sqlalchemy.ext.declarative.api import DeclarativeMeta
|
from sqlalchemy.ext.declarative.api import DeclarativeMeta
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
@ -12,10 +11,14 @@ from sqlalchemy.schema import Table
|
||||||
from royalnet.alchemy.errors import TableNotFoundError
|
from royalnet.alchemy.errors import TableNotFoundError
|
||||||
from royalnet.utils import asyncify
|
from royalnet.utils import asyncify
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
# noinspection PyProtectedMember
|
||||||
|
from sqlalchemy.engine import Engine
|
||||||
|
|
||||||
|
|
||||||
class Alchemy:
|
class Alchemy:
|
||||||
"""A wrapper around :mod:`sqlalchemy.orm` that allows the instantiation of multiple engines at once while maintaining
|
"""A wrapper around :mod:`sqlalchemy.orm` that allows the instantiation of multiple engines at once while
|
||||||
a single declarative class for all of them."""
|
maintaining a single declarative class for all of them."""
|
||||||
|
|
||||||
def __init__(self, database_uri: str, tables: Set):
|
def __init__(self, database_uri: str, tables: Set):
|
||||||
"""Create a new :class:`.Alchemy` object.
|
"""Create a new :class:`.Alchemy` object.
|
||||||
|
@ -28,7 +31,7 @@ class Alchemy:
|
||||||
if database_uri.startswith("sqlite"):
|
if database_uri.startswith("sqlite"):
|
||||||
raise NotImplementedError("sqlite databases aren't supported, as they can't be used in multithreaded"
|
raise NotImplementedError("sqlite databases aren't supported, as they can't be used in multithreaded"
|
||||||
" applications")
|
" applications")
|
||||||
self._engine: Engine = create_engine(database_uri)
|
self._engine: "Engine" = create_engine(database_uri)
|
||||||
self._Base = declarative_base(bind=self._engine)
|
self._Base = declarative_base(bind=self._engine)
|
||||||
self.Session: sessionmaker = sessionmaker(bind=self._engine)
|
self.Session: sessionmaker = sessionmaker(bind=self._engine)
|
||||||
self._tables: Dict[str, Table] = {}
|
self._tables: Dict[str, Table] = {}
|
||||||
|
|
|
@ -8,6 +8,7 @@ from sqlalchemy.orm import *
|
||||||
import royalnet.utils as ru
|
import royalnet.utils as ru
|
||||||
|
|
||||||
|
|
||||||
|
# noinspection PyAttributeOutsideInit
|
||||||
class Token:
|
class Token:
|
||||||
__tablename__ = "tokens"
|
__tablename__ = "tokens"
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
|
from typing import TYPE_CHECKING, Union
|
||||||
import asyncio as aio
|
import asyncio as aio
|
||||||
from typing import TYPE_CHECKING
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from ..serf import Serf
|
from ..serf import Serf
|
||||||
|
from ..constellation import Constellation
|
||||||
|
|
||||||
|
|
||||||
class HeraldEvent:
|
class HeraldEvent:
|
||||||
|
@ -11,19 +12,19 @@ class HeraldEvent:
|
||||||
name = NotImplemented
|
name = NotImplemented
|
||||||
"""The event_name that will trigger this event."""
|
"""The event_name that will trigger this event."""
|
||||||
|
|
||||||
def __init__(self, serf: "Serf", config):
|
def __init__(self, parent: Union["Serf", "Constellation"], config):
|
||||||
self.serf: "Serf" = serf
|
self.parent: Union["Serf", "Constellation"] = parent
|
||||||
self.config = config
|
self.config = config
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def alchemy(self):
|
def alchemy(self):
|
||||||
"""A shortcut for :attr:`.interface.alchemy`."""
|
"""A shortcut for :attr:`.parent.alchemy`."""
|
||||||
return self.serf.alchemy
|
return self.parent.alchemy
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def loop(self) -> aio.AbstractEventLoop:
|
def loop(self) -> aio.AbstractEventLoop:
|
||||||
"""A shortcut for :attr:`.interface.loop`."""
|
"""A shortcut for :attr:`.parent.loop`."""
|
||||||
return self.serf.loop
|
return self.parent.loop
|
||||||
|
|
||||||
async def run(self, **kwargs):
|
async def run(self, **kwargs):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
|
@ -220,7 +220,7 @@ class Constellation:
|
||||||
for SelectedEvent in events:
|
for SelectedEvent in events:
|
||||||
# Initialize the event
|
# Initialize the event
|
||||||
try:
|
try:
|
||||||
event = SelectedEvent(serf=self, config=pack_cfg)
|
event = SelectedEvent(constellation=self, config=pack_cfg)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.error(f"Skipping: "
|
log.error(f"Skipping: "
|
||||||
f"{SelectedEvent.__qualname__} - {e.__class__.__qualname__} in the initialization.")
|
f"{SelectedEvent.__qualname__} - {e.__class__.__qualname__} in the initialization.")
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
|
from abc import ABCMeta
|
||||||
|
|
||||||
from .star import Star
|
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``).
|
"""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
|
To create a new website route you should create a new class inheriting from this class with a function overriding
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
from typing import *
|
from typing import *
|
||||||
|
|
||||||
|
import abc
|
||||||
from starlette.requests import Request
|
from starlette.requests import Request
|
||||||
from starlette.responses import Response
|
from starlette.responses import Response
|
||||||
|
|
||||||
|
@ -10,7 +11,7 @@ if TYPE_CHECKING:
|
||||||
import sqlalchemy.orm.session
|
import sqlalchemy.orm.session
|
||||||
|
|
||||||
|
|
||||||
class Star:
|
class Star(metaclass=abc.ABCMeta):
|
||||||
"""A Star is a class representing a part of the website.
|
"""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!"""
|
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.constellation: "Constellation" = constellation
|
||||||
self.config: "ConfigDict" = config
|
self.config: "ConfigDict" = config
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
async def page(self, request: Request) -> Response:
|
async def page(self, request: Request) -> Response:
|
||||||
"""The function generating the :class:`~starlette.Response` to a web :class:`~starlette.Request`.
|
"""The function generating the :class:`~starlette.Response` to a web :class:`~starlette.Request`.
|
||||||
|
|
||||||
|
|
|
@ -47,4 +47,8 @@ class ResponseFailure(Response):
|
||||||
self.extra_info: Optional[dict] = extra_info
|
self.extra_info: Optional[dict] = extra_info
|
||||||
|
|
||||||
def __repr__(self):
|
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")"
|
||||||
|
|
|
@ -285,7 +285,8 @@ class Serf(abc.ABC):
|
||||||
finally:
|
finally:
|
||||||
await data.session_close()
|
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)}")
|
log.info(f"Calling key_callback: {repr(key)}")
|
||||||
try:
|
try:
|
||||||
await key.press(data)
|
await key.press(data)
|
||||||
|
|
|
@ -40,6 +40,7 @@ def sentry_exc(exc: Exception,
|
||||||
with sentry_sdk.configure_scope() as scope:
|
with sentry_sdk.configure_scope() as scope:
|
||||||
scope.set_level(level.lower())
|
scope.set_level(level.lower())
|
||||||
sentry_sdk.capture_exception(exc)
|
sentry_sdk.capture_exception(exc)
|
||||||
|
# noinspection PyUnresolvedReferences,PyProtectedMember
|
||||||
level_int: int = logging._nameToLevel[level.upper()]
|
level_int: int = logging._nameToLevel[level.upper()]
|
||||||
log.log(level_int, f"Captured {level.capitalize()}: {exc}")
|
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
|
# If started in debug mode (without -O), raise the exception, allowing you to see its source
|
||||||
|
|
Loading…
Reference in a new issue