Skip to content

Commit

Permalink
Fix compat with aiohttp>=0.21.0
Browse files Browse the repository at this point in the history
Rename add_resource to add_resource_object to prevent clash
with URLDispatcher's add_resource
  • Loading branch information
sloria committed Mar 14, 2016
1 parent 184c554 commit 0f7678a
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 29 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -2,6 +2,12 @@
Changelog
*********

2.0.0 (unreleased)
==================

- Fix compatibility with aiohttp>=0.21.0.
- [routing] *Backwards-incompatible*: Renamed ``ResourceRouter.add_resource`` to ``ResourceRouter.add_resource_object``, to prevent clashing with aiohttp's URLDispatcher.

1.0.0 (2015-10-27)
==================

Expand Down
2 changes: 1 addition & 1 deletion README.rst
Expand Up @@ -37,7 +37,7 @@ aiohttp_utils
})
app.router.add_resource('/', HelloResource())
app.router.add_resource_object('/', HelloResource())
# Content negotiation
negotiation.setup(
Expand Down
8 changes: 4 additions & 4 deletions aiohttp_utils/__init__.py
@@ -1,12 +1,12 @@
# -*- coding: utf-8 -*-
__version__ = '1.0.0'
__author__ = 'Steven Loria'
__license__ = "MIT"

from .negotiation import Response
from .constants import APP_KEY, CONFIG_KEY
from .runner import run

__version__ = '2.0.0.dev0'
__author__ = 'Steven Loria'
__license__ = "MIT"

__all__ = (
'Response',
'run',
Expand Down
27 changes: 13 additions & 14 deletions aiohttp_utils/path_norm.py
Expand Up @@ -15,10 +15,12 @@
"""
import asyncio

from aiohttp import web, hdrs
from aiohttp import web
from aiohttp.web_urldispatcher import (
SystemRoute, UrlMappingMatchInfo, _MethodNotAllowedMatchInfo, _NotFoundMatchInfo
SystemRoute,
MatchInfoError,
)
from aiohttp.web_exceptions import HTTPMethodNotAllowed, HTTPNotFound

from .constants import CONFIG_KEY

Expand All @@ -37,21 +39,18 @@
def resolve2(router, method, path):
allowed_methods = set()

for route in router._urls:
match_dict = route.match(path)
if match_dict is None:
continue

route_method = route.method
if route_method == method or route_method == hdrs.METH_ANY:
return UrlMappingMatchInfo(match_dict, route)

allowed_methods.add(route_method)
for resource in router._resources:
match_dict, allowed = yield from resource.resolve(method, path)
if match_dict is not None:
return match_dict
else:
allowed_methods |= allowed
else:
if allowed_methods:
return _MethodNotAllowedMatchInfo(method, allowed_methods)
return MatchInfoError(HTTPMethodNotAllowed(method,
allowed_methods))
else:
return _NotFoundMatchInfo()
return MatchInfoError(HTTPNotFound())

class NormalizePathMiddleware:
"""Middleware for path normalization.
Expand Down
8 changes: 4 additions & 4 deletions aiohttp_utils/routing.py
Expand Up @@ -34,7 +34,7 @@ async def post(self, request):
return web.Response(body=b'Posted it', content_type='text/plain')
app.router.add_resource('/', IndexResource())
app.router.add_resource_object('/', IndexResource())
# Normal function-based handlers still work
async def handler(request):
Expand All @@ -48,7 +48,7 @@ async def handler(request):
You can override the default names by passing a ``names`` dict to `add_resource`. ::
app.router.add_resource('/', IndexResource(), names={'get': 'index_get'})
app.router.add_resource_object('/', IndexResource(), names={'get': 'index_get'})
app.router['index_get'].url() == '/'
"""

Expand All @@ -57,7 +57,7 @@ async def handler(request):
def get_default_handler_name(self, resource, method_name: str):
return '{resource.__class__.__name__}:{method_name}'.format(**locals())

def add_resource(self, path: str, resource, methods: tuple=tuple(), names: Mapping=None):
def add_resource_object(self, path: str, resource, methods: tuple=tuple(), names: Mapping=None):
"""Add routes by an resource instance's methods.
:param path: route path. Should be started with slash (``'/'``).
Expand Down Expand Up @@ -260,5 +260,5 @@ def add_route(
)
for method_name in supported_method_names
}
return app.router.add_resource(path, resource, names=names)
return app.router.add_resource_object(path, resource, names=names)
yield add_route
2 changes: 1 addition & 1 deletion dev-requirements.txt
Expand Up @@ -4,7 +4,7 @@ invoke
# Testing
pytest
tox>=1.5.0
webtest-aiohttp
webtest-aiohttp>=1.1.0
Mako # Used in the integration tests

# Packaging
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Expand Up @@ -30,7 +30,7 @@ Release v\ |version|. (:ref:`Changelog <changelog>`)
})
app.router.add_resource('/', HelloResource())
app.router.add_resource_object('/', HelloResource())
# Content negotiation
negotiation.setup(
Expand Down
3 changes: 2 additions & 1 deletion setup.cfg
Expand Up @@ -8,8 +8,9 @@ universal=1
# E128: continuation line under-indented for visual indent
# E265: block comment should start with #
# E302: expected 2 blank lines, found 0
# E266: too many leading '#' for block comment
[flake8]
ignore = E127,E128,E265,E302
ignore = E127,E128,E265,E302,E266
max-line-length = 100
exclude=docs,.git,tests/,aiohttp_utils/compat.py,build,setup.py,env,venv

6 changes: 3 additions & 3 deletions tests/test_routing.py
Expand Up @@ -37,9 +37,9 @@ def post(self, request):
class ChildResource(MyResource):
pass

app.router.add_resource('/my', MyResource())
app.router.add_resource('/my2', MyResource2(), names={'get': 'my_resource2_get'})
app.router.add_resource('/child', ChildResource(), methods=('get', ))
app.router.add_resource_object('/my', MyResource())
app.router.add_resource_object('/my2', MyResource2(), names={'get': 'my_resource2_get'})
app.router.add_resource_object('/child', ChildResource(), methods=('get', ))


class TestResourceRouter:
Expand Down

0 comments on commit 0f7678a

Please sign in to comment.