Skip to content

Commit

Permalink
Update to latest aiohttp
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Avanov committed Jun 10, 2018
1 parent 3813dbc commit d81c448
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 162 deletions.
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
aiohttp==2.0.7
aiohttp_session[aioredis]==0.8.0
aiohttp==3.3.1
aiohttp_session[aioredis]==2.5.1
aiopg==0.14.0
hiredis==0.2.0
aioredis==0.3.5
aioredis==1.1.0
psycopg2==2.7.4
sqlalchemy==1.2.8
alembic==0.9.9
Expand Down
4 changes: 2 additions & 2 deletions requirements_test.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pytest==2.9.2
pytest-aiohttp==0.1.3
pytest==3.6.1
pytest-aiohttp==0.3.0
coverage==4.1
pytest-cov==2.5.1
mypy==0.501
4 changes: 2 additions & 2 deletions solo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from . import importer
from . import import_hooks

importer.install()
import_hooks.activate()

from .configurator import Configurator
from .configurator import http_defaults
Expand Down
4 changes: 3 additions & 1 deletion solo/apps/accounts/providers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ def enable_provider(config: Configurator,
auth_registry = config.registry.setdefault('solo.apps.accounts', {})
redirect_uri = '{}{}'.format(
config.registry['config']['server']['public_uri'].rstrip('/'),
config.router.url('solo.apps.accounts:/login/{provider}/callback', parts={'provider': provider.value})
config.router.url_for('solo.apps.accounts:/login/{provider}/callback',
provider=provider.value)
)
print(redirect_uri)
provider_impl = provider['auth_provider_impl']
auth_registry[name] = provider_impl(client_id=client_id,
client_secret=client_secret,
Expand Down
8 changes: 4 additions & 4 deletions solo/apps/accounts/providers/base_oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,18 @@ async def validate_callback(self, request: web.Request) -> Tuple[str, str]:
"""
session = await get_session(request)
session_state = session.pop('oauth.state', None)
request_state = request.GET.get('state')
request_state = request.query.get('state')
if not session_state or session_state != request_state:
raise CSRFError(
'State mismatch. Requested: {request_state}. Actual: {session_state}'.format(
request_state=request_state,
session_state=session_state
)
)
code = request.GET.get('code')
code = request.query.get('code')
if not code:
reason = request.GET.get('error', 'n/a')
error_description = request.GET.get('error_description', '(no description)')
reason = request.query.get('error', 'n/a')
error_description = request.query.get('error_description', '(no description)')
raise AuthorizationError("Authorization code was not provided. Reason: {} {}".format(reason, error_description),
reason=reason, provider=self)
return (session_state, code)
Expand Down
3 changes: 2 additions & 1 deletion solo/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def run_cmd(args: argparse.Namespace, solo_cfg: Dict[str, Any]):

with loop.run_until_complete(init_webapp(loop, solo_cfg)) as app:
app.create_server()
log.debug('Serving on {}'.format(app.server.sockets[0].getsockname()))
socket_name = app.server.sockets[0].getsockname()
log.debug(f'Serving on {socket_name}')
try:
loop.run_forever()
except KeyboardInterrupt:
Expand Down
4 changes: 2 additions & 2 deletions solo/configurator/config/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ def check_routes_consistency(self, package):
namespace=namespace
)
)
def url(self, name: str, *args, **kwargs):
def url_for(self, name: str, *args, **kwargs):
aiohttp_name = self.routes_aiohttp_mapping[name]
return self.app.router[aiohttp_name].url(*args, **kwargs)
return self.app.router[aiohttp_name].url_for(*args, **kwargs)


class ViewMeta:
Expand Down
3 changes: 3 additions & 0 deletions solo/import_hooks/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .importer import activate

__all__ = ('activate',)
15 changes: 15 additions & 0 deletions solo/import_hooks/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
""" Based on
https://greentreesnakes.readthedocs.io
"""
import ast


class AstTransformer(ast.NodeTransformer):
pass


def apply_transformers(tree):
tree = AstTransformer().visit(tree)
# Add lineno & col_offset to possibly modified nodes
ast.fix_missing_locations(tree)
return tree
8 changes: 7 additions & 1 deletion solo/importer.py → solo/import_hooks/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import struct
from typing import Optional, Sequence

from .hooks import apply_transformers


PY37 = sys.version_info >= (3, 7)


Expand Down Expand Up @@ -54,6 +57,9 @@ def exec_module(self, module: ModuleType):
spec = module.__spec__
with Path(spec.origin).open('rb') as f:
source_ast = ast.parse(f.read(), spec.origin)

source_ast = apply_transformers(source_ast)

try:
code = compile(source_ast, spec.origin, "exec")
except (SyntaxError, ValueError):
Expand All @@ -77,5 +83,5 @@ def find_spec(cls, fullname: str, path: Optional[Sequence[str]] = None, target=N
return spec


def install():
def activate():
sys.meta_path.insert(0, MyImporter)
3 changes: 2 additions & 1 deletion solo/server/memstore.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import asyncio
from typing import Dict, Any
import aioredis
from aioredis.abc import AbcPool


async def init_pool(loop: asyncio.AbstractEventLoop,
config: Dict[str, Any]) -> aioredis.RedisPool:
config: Dict[str, Any]) -> AbcPool:
c = config['redis']
address = (c['host'], c['port'])
pool = await aioredis.create_pool(address=address,
Expand Down
145 changes: 0 additions & 145 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,148 +1,3 @@
# """ https://pytest.org/latest/fixture.html#sharing-a-fixture-across-tests-in-a-module-or-class-session
#
# This conftest is a copy of https://github.com/KeepSafe/aiohttp/blob/master/tests/conftest.py
#
# Since there's no a working test suite for aiohttp (https://github.com/sloria/webtest-aiohttp/issues/1),
# we use the same suite as the base framework.
# """
# import asyncio
# import aiohttp
# import collections
# import logging
# import pytest
# import re
# import sys
# import warnings
#
# from solo.testutils import (
# loop_context, unused_port
# )
#
# from solo.cli.util import parse_app_config
# from solo import init_webapp
#
#

#
# @pytest.yield_fixture
# def create_server(loop):
# app = handler = srv = None
# config = parse_app_config('./test_config.yml') # TODO: move path to outer scope
#
# async def create(*, debug=False, ssl_ctx=None, proto='http'):
# nonlocal app, handler, srv, config
# port = unused_port()
# app_manager = await init_webapp(loop, config)
# server_future = app_manager.create_server_future(host='127.0.0.1', port=port, ssl=ssl_ctx)
# if ssl_ctx:
# proto += 's'
# url = "{}://127.0.0.1:{}/".format(proto, port)
#
# app = app_manager.app
# handler = app_manager.handler
# srv = await server_future
# return app, url
#
# yield create
#
#
# async def finish():
# if hasattr(app, 'dbengine'):
# app.dbengine.terminate()
# await app.dbengine.wait_closed()
#
# if hasattr(app, 'memstore'):
# await app.memstore.clear()
#
# await handler.finish_connections()
# await app.finish()
# srv.close()
# await srv.wait_closed()
#
# loop.run_until_complete(finish())
#
#
# class Client:
# def __init__(self, session, url):
# self._session = session
# self._url = url
#
# def close(self):
# self._session.close()
#
# def get(self, path, **kwargs):
# url = "{}/{}".format(self._url.rstrip('/'), path.lstrip('/'))
# return self._session.get(url, **kwargs)
#
# def post(self, path, **kwargs):
# url = "{}/{}".format(self._url.rstrip('/'), path.lstrip('/'))
# return self._session.post(url, **kwargs)
#
# def delete(self, path, **kwargs):
# url = "{}/{}".format(self._url.rstrip('/'), path.lstrip('/'))
# return self._session.delete(url)
#
# def ws_connect(self, path, **kwargs):
# url = "{}/{}".format(self._url.rstrip('/'), path.lstrip('/'))
# return self._session.ws_connect(url, **kwargs)
#
#
# @pytest.yield_fixture
# def create_app_and_client(create_server, loop):
# client = None
#
# @asyncio.coroutine
# def maker(*, server_params=None, client_params=None):
# nonlocal client
# if server_params is None:
# server_params = {}
# server_params.setdefault('debug', False)
# server_params.setdefault('ssl_ctx', None)
# app, url = yield from create_server(**server_params)
# if client_params is None:
# client_params = {}
# client = Client(aiohttp.ClientSession(loop=loop, **client_params), url)
# return app, client
#
# yield maker
# client.close()
#
#
# @pytest.mark.tryfirst
# def pytest_pycollect_makeitem(collector, name, obj):
# if collector.funcnamefilter(name):
# if not callable(obj):
# return
# item = pytest.Function(name, parent=collector)
# if 'run_loop' in item.keywords:
# return list(collector._genfunctions(name, obj))
#
#
# @pytest.mark.tryfirst
# def pytest_pyfunc_call(pyfuncitem):
# """
# Run asyncio marked test functions in an event loop instead of a normal
# function call.
# """
# if 'run_loop' in pyfuncitem.keywords:
# funcargs = pyfuncitem.funcargs
# loop = funcargs['loop']
# testargs = {arg: funcargs[arg]
# for arg in pyfuncitem._fixtureinfo.argnames}
# loop.run_until_complete(pyfuncitem.obj(**testargs))
# return True
#
#
# def pytest_runtest_setup(item):
# if 'run_loop' in item.keywords and 'loop' not in item.fixturenames:
# # inject an event loop fixture for all async tests
# item.fixturenames.append('loop')
#
#
# def pytest_ignore_collect(path, config):
# if 'test_py35' in str(path):
# if sys.version_info < (3, 5, 0):
# return True
import pytest
from aiohttp import web

Expand Down

0 comments on commit d81c448

Please sign in to comment.