Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Routes view #519

Merged
merged 2 commits into from
Oct 13, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Implement RoutesView
  • Loading branch information
asvetlov committed Sep 22, 2015
commit 804764839913b74f46f15170190ebacd0506dd0a
20 changes: 20 additions & 0 deletions aiohttp/web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,23 @@ def __repr__(self):
', '.join(sorted(self._allowed_methods))))


class RoutesView:

__slots__ = '_urls'

def __init__(self, urls):
self._urls = urls

def __len__(self):
return len(self._urls)

def __iter__(self):
yield from self._urls

def __contains__(self, route):
return route in self._urls


class UrlDispatcher(AbstractRouter, collections.abc.Mapping):

DYN = re.compile(r'^\{(?P<var>[a-zA-Z][_a-zA-Z0-9]*)\}$')
Expand Down Expand Up @@ -417,6 +434,9 @@ def __contains__(self, name):
def __getitem__(self, name):
return self._routes[name]

def routes(self):
return RoutesView(self._urls)

def register_route(self, route):
assert isinstance(route, Route), 'Instance of Route class is required.'

Expand Down
21 changes: 21 additions & 0 deletions tests/test_urldispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,3 +605,24 @@ def test_static_handle_exception(self):
self.assertIs(exc, fut.exception())
self.assertFalse(loop.add_writer.called)
self.assertFalse(loop.remove_writer.called)

def fill_routes(self):
route1 = self.router.add_route('GET', '/plain', self.make_handler())
route2 = self.router.add_route('GET', '/variable/{name}',
self.make_handler())
route3 = self.router.add_static('/static',
os.path.dirname(aiohttp.__file__))
return route1, route2, route3

def test_routes_view_len(self):
self.fill_routes()
self.assertEqual(3, len(self.router.routes()))

def test_routes_view_iter(self):
routes = self.fill_routes()
self.assertEqual(list(routes), list(self.router.routes()))

def test_routes_view_contains(self):
routes = self.fill_routes()
for route in routes:
self.assertIn(route, self.router.routes())