Skip to content

Commit

Permalink
Merge branch 'release/0.15.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
ri-gilfanov committed Jun 16, 2021
2 parents 46566c2 + 5111db6 commit b89a452
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 58 deletions.
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ The library provides the next features:
* initializing asynchronous sessions through a decorators;
* simple access to one asynchronous session by default key;
* preventing attributes from being expired after commit by default;
* support for different types of request handlers.
* support for different types of request handlers;
* support nested applications.


Documentation
Expand Down
10 changes: 4 additions & 6 deletions aiohttp_sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@

if TYPE_CHECKING:
from aiohttp.web import Application
from typing import Callable, Iterable, Union, Tuple
from typing import Any, Callable, Iterable, Union, Tuple

TSessionFactory = Callable[..., AsyncSession]
TBindTo = Union[str, Callable[..., AsyncSession]]
TSABinding = Tuple[TSessionFactory, str, bool]


__version__ = '0.15.1'
__version__ = '0.15.2'

__all__ = ['bind', 'DuplicateAppKeyError', 'DuplicateRequestKeyError',
'init_db', 'SAAbstractView', 'SABaseView', 'SA_DEFAULT_KEY',
Expand Down Expand Up @@ -59,9 +59,7 @@ def bind(bind_to: 'TBindTo', key: str = SA_DEFAULT_KEY, *,
return bind_to, key, middleware


def sa_bind(bind_to: 'TBindTo', key: str = SA_DEFAULT_KEY, *,
middleware: bool = True) -> 'TSABinding':
return bind(bind_to, key, middleware=middleware)
sa_bind = bind # sa_bind is synonym for bind


def setup(app: 'Application', bindings: 'Iterable[TSABinding]') -> None:
Expand All @@ -76,7 +74,7 @@ def setup(app: 'Application', bindings: 'Iterable[TSABinding]') -> None:
app.middlewares.append(sa_middleware(key))


def __getattr__(name):
def __getattr__(name: str) -> 'Any':
if name == 'DEFAULT_KEY':
msg = "'DEFAULT_KEY' has been deprecated, use 'SA_DEFAULT_KEY'"
warnings.warn(msg, UserWarning, stacklevel=2)
Expand Down
6 changes: 5 additions & 1 deletion aiohttp_sqlalchemy/constants.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from typing import TYPE_CHECKING
import warnings

if TYPE_CHECKING:
from typing import Any


SA_DEFAULT_KEY = 'sa_main'


def __getattr__(name):
def __getattr__(name: str) -> 'Any':
if name == 'DEFAULT_KEY':
msg = "'DEFAULT_KEY' has been deprecated, use 'SA_DEFAULT_KEY'"
warnings.warn(msg, UserWarning, stacklevel=2)
Expand Down
17 changes: 6 additions & 11 deletions aiohttp_sqlalchemy/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,17 @@

if TYPE_CHECKING:
from aiohttp.web import Request, StreamResponse
from typing import Awaitable, Callable, Union, Type
from typing import Any, Awaitable, Callable, Union, Type

TRequestHandler = Callable[
...,
Union[
Type[AbstractView],
Callable[[Request], Awaitable[StreamResponse]]
]
]
TDecorated = Callable[[Request], Awaitable[StreamResponse]]
TResult = Callable[..., Union[Type[AbstractView], TDecorated]]


def sa_decorator(key: str = SA_DEFAULT_KEY) -> 'TRequestHandler':
def sa_decorator(key: str = SA_DEFAULT_KEY) -> 'TResult':
""" SQLAlchemy asynchronous handler decorator. """
def wrapper(handler):
def wrapper(handler: 'TDecorated') -> 'TDecorated':
@wraps(handler)
async def wrapped(*args, **kwargs) -> 'StreamResponse':
async def wrapped(*args: 'Any', **kwargs: 'Any') -> 'StreamResponse':
request = args[0].request \
if isinstance(args[0], AbstractView) \
else args[-1]
Expand Down
6 changes: 4 additions & 2 deletions aiohttp_sqlalchemy/middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
from aiohttp.web import Request, StreamResponse
from typing import Awaitable, Callable

THandler = Callable[..., Awaitable[StreamResponse]]

def sa_middleware(key: str = SA_DEFAULT_KEY) -> 'Callable[..., Awaitable[StreamResponse]]':

def sa_middleware(key: str = SA_DEFAULT_KEY) -> 'THandler':
""" SQLAlchemy asynchronous middleware factory. """
@middleware
async def sa_middleware_(request: 'Request', handler: 'Callable')\
async def sa_middleware_(request: 'Request', handler: 'THandler') \
-> 'StreamResponse':
if key in request:
raise DuplicateRequestKeyError(key)
Expand Down
9 changes: 2 additions & 7 deletions aiohttp_sqlalchemy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,13 @@ async def init_db(
metadata: 'MetaData',
key: str = SA_DEFAULT_KEY,
) -> None:
session_factory = app.get(key)
session_factory = app[key]
session = session_factory()
async with session.bind.begin() as connection:
await connection.run_sync(metadata.create_all)


async def sa_init_db(
app: 'Application',
metadata: 'MetaData',
key: str = SA_DEFAULT_KEY,
) -> None:
await init_db(app, metadata, key)
sa_init_db = init_db # sa_init_db is synonym for init_db


def sa_session(request: 'Request', key: str = SA_DEFAULT_KEY) -> 'AsyncSession':
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
author = 'Ruslan Ilyasovich Gilfanov'

# The full version, including alpha/beta/rc tags
release = '0.15.1'
release = '0.15.2'


# -- General configuration ---------------------------------------------------
Expand Down
47 changes: 20 additions & 27 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,31 @@ The library provides the next features:
* initializing asynchronous sessions through a decorators;
* simple access to one asynchronous session by default key;
* preventing attributes from being expired after commit by default;
* support for different types of request handlers.
* support for different types of request handlers;
* support nested applications.


Installation
------------
::
Installing ``aiohttp-sqlalchemy`` with pip: ::

pip install aiohttp-sqlalchemy
pip install aiohttp-sqlalchemy


Optional requirements
---------------------

For PostgreSQL support, you also need install ``asyncpg``: ::

pip install asyncpg

For MySQL support, you also need install ``aiomysql``: ::

pip install aiomysql

For SQLite3 support, you also need install ``aiosqlite``: ::

pip install aiosqlite


Simple example
Expand Down Expand Up @@ -208,30 +225,6 @@ Decorating handlers
])
Nested apps
-----------
.. code-block:: python
async def main(request):
async with request['sa_main'].bind.begin() as conn:
# some operations with AsyncConnection object with
# an AsyncTransaction established.
async with request['sa_main'].begin():
# some operations with AsyncSession object
app = web.Application()
aiohttp_sqlalchemy.setup(app, [
aiohttp_sqlalchemy.bind('sqlite+aiosqlite:///'),
])
subapp = web.Application()
subapp.add_routes([web.get('', main)])
app.add_subapp(prefix='/subapp', subapp=subapp)
Change log
----------
Version 0.15
Expand Down
10 changes: 8 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[tool.poetry]
name = "aiohttp-sqlalchemy"
version = "0.15.1"
version = "0.15.2"
description = "SQLAlchemy 1.4 / 2.0 support for aiohttp."
authors = [
"Ruslan Ilyasovich Gilfanov <ri.gilfanov@yandex.ru>",
]
classifiers = [
"Development Status :: 5 - Production/Stable",
"Framework :: AsyncIO",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
Expand All @@ -15,7 +16,7 @@ classifiers = [
"Topic :: Internet",
"Topic :: Internet :: WWW/HTTP",
]
keywords = ["aiohttp", "sqlalchemy"]
keywords = ["aiohttp", "sqlalchemy", "asyncio"]
license = "BSD-3-Clause"
readme = "README.rst"

Expand All @@ -42,6 +43,11 @@ pytest-cov = ">=2.12.1"
Sphinx = ">=4.0.2"
sphinx_rtd_theme = ">=0.5.2"

[tool.poetry.extras]
mysql = ["aiomysql"]
postgres = ["asyncpg"]
sqlite = ["aiosqlite"]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

0 comments on commit b89a452

Please sign in to comment.