Commits

Andriy Kornatskyy committed d36de55

Applied minor optimization in PathRouter.

  • Participants
  • Parent commits acb34a7

Comments (0)

Files changed (2)

File src/wheezy/routing/router.py

         kwargs['route_name'] = name
         # build finishing route
         route = build_route(pattern, True, kwargs, self.route_builders)
-        self.route_map[name] = route
-        self.mapping.append((route, handler))
+        self.route_map[name] = route.path
+        self.mapping.append((route.match, handler))
 
     def include(self, pattern, included, kwargs=None):
         """ Includes nested routes below the current.
             ... ]
             >>> r.include(r'admin/', admin_routes)
             >>> route, inner = r.mapping[0]
-            >>> inner2, route2 = r.routers[0]
-            >>> assert route == route2
-            >>> assert inner == inner2
             >>> assert isinstance(inner, PathRouter)
             >>> r = PathRouter()
             >>> r.include(r'admin/', PathRouter())
         else:
             inner = PathRouter(self.route_builders)
             inner.add_routes(included)
-        self.mapping.append((route, inner))
-        self.routers.append((inner, route))
+        self.mapping.append((route.match, inner))
+        self.routers.append((inner.path_for, route.path))
 
     def add_routes(self, mapping):
         """ Adds routes represented as a list of tuple
             >>> kwargs
             {}
         """
-        for route, handler in self.mapping:
-            matched, kwargs = route.match(path)
+        for route_match, handler in self.mapping:
+            matched, kwargs = route_match(path)
             if matched >= 0:
                 # TODO: isinstance(handler, PathRouter)
                 handler_match = getattr(handler, 'match', None)
 
             >>> r.path_for(r'unknown')
         """
-        route = self.route_map.get(name, None)
-        if route:
-            return route.path(kwargs).rstrip('/')
-        for inner, route in self.routers:
-            inner_path = inner.path_for(name, **kwargs)
+        if name in self.route_map:
+            return self.route_map[name](kwargs).rstrip('/')
+        for inner_path_for, route_path in self.routers:
+            inner_path = inner_path_for(name, **kwargs)
             if inner_path is not None:
-                return route.path(kwargs) + inner_path
+                return route_path(kwargs) + inner_path
         return None

File src/wheezy/routing/tests/test_router.py

         """
         self.r.add_route(r'abc', 'x', name='my_name')
 
-        assert 'my_name' in tuple(self.r.route_map.keys())
+        assert self.r.route_map
 
     def test_with_default_kwargs(self):
         """ ``kwargs`` is None.
         """
         self.r.add_route(r'abc', 'x', kwargs=None, name='n')
 
-        route = self.r.route_map['n']
-        assert route.kwargs
-        assert 'n' == route.kwargs['route_name']
+        assert self.r.route_map['n']
 
     def test_with_kwargs(self):
         """ ``kwargs`` is supplied.
         kw = {'a': 1}
         self.r.add_route(r'abc', 'x', kwargs=kw, name='n')
 
-        route = self.r.route_map['n']
-        self.assertEquals(kw, route.kwargs)
+        assert self.r.route_map['n']
 
     def test_build_route(self):
         """ ``build_route`` call.
         from wheezy.routing import builders
 
         kw = {'a': 1}
+        mock_route = self.m.mock()
+        expect(mock_route.match)
+        expect(mock_route.path)
         mock_build_route = self.m.replace(builders.build_route)
         expect(
             mock_build_route('abc', True, kw, self.r.route_builders)
-        ).result('x')
+        ).result(mock_route)
         self.m.replay()
 
-        self.r.add_route('abc', 'x', kwargs=kw, name='n')
+        self.r.add_route('abc', mock_route, kwargs=kw, name='n')
 
     def test_mapping(self):
         """ mapping has route and handler pair
         self.r.add_route(r'abc', 'x', kwargs=None, name='n')
 
         assert 1 == len(self.r.route_map) == len(self.r.mapping)
-        route = self.r.route_map['n']
-        mroute, handler = self.r.mapping[0]
-        assert route is mroute
+        assert self.r.route_map['n']
 
 
 class PathRouterIncludeTestCase(unittest.TestCase):
         from wheezy.routing import builders
 
         kw = {'a': 1}
+        mock_route = self.m.mock()
+        expect(mock_route.match)
+        expect(mock_route.path)
         mock_build_route = self.m.replace(builders.build_route)
         expect(
             mock_build_route('abc', False, kw, self.r.route_builders)
-        ).result('x')
+        ).result(mock_route)
         self.m.replay()
 
         self.r.include('abc', [], kwargs=kw)
 
         self.r.include('abc', [])
 
-        assert mock_router is self.r.routers[0][0]
-
-    def test_routers(self):
-        """ reverse mapping between routers and mapping.
-        """
-        self.r.include('abc', [])
-
-        mr, mi = self.r.mapping[0]
-        ri, rr = self.r.routers[0]
-
-        assert mr is rr
-        assert mi is ri
+        assert self.r.routers[0][0]
 
 
 class PathRouterAddRoutesTestCase(unittest.TestCase):
         from wheezy.routing import builders
 
         mock_route = self.m.mock()
+        expect(mock_route.path)
         mock_build_route = self.m.replace(builders.build_route)
         expect(
                 mock_build_route('', True,
         from wheezy.routing import builders
 
         mock_route = self.m.mock()
+        expect(mock_route.match)
         mock_build_route = self.m.replace(builders.build_route)
         expect(
                 mock_build_route(