Commits

Andriy Kornatskyy committed acb34a7

Added __slots__ to routes and path router.

Comments (0)

Files changed (5)

src/wheezy/routing/builders.py

 
 import re
 
-from wheezy.routing.route import Route
 from wheezy.routing.route import PlainRoute
 from wheezy.routing.route import RegexRoute
 from wheezy.routing.curly import RE_SPLIT as RE_CURLY_ROUTE
 from wheezy.routing.curly import convert as curly_convert
 
 
-RE_PLAIN_ROUTE = re.compile('^[\w/-]+$')
+RE_PLAIN_ROUTE = re.compile(r'^[\w/-]+$')
 
 
 def try_build_plain_route(pattern, finishing, kwargs=None):
     """ If the plain route regular expression match the pattern
         than create a PlainRoute instance.
 
+        >>> pr = PlainRoute(r'abc', True)
+        >>> r = try_build_plain_route(pr, True)
+        >>> assert pr == r
         >>> r = try_build_plain_route(r'abc', True)
         >>> assert isinstance(r, PlainRoute)
 
         >>> r = try_build_plain_route(r'ab[c]', False)
         >>> assert r is None
     """
+    if isinstance(pattern, PlainRoute):
+        return pattern
     if pattern == '' or RE_PLAIN_ROUTE.match(pattern):
         return PlainRoute(pattern, finishing, kwargs)
     return None
     """ Convert pattern expression into regex with
         named groups and create regex route.
 
+        >>> pr = RegexRoute(r'abc', True)
+        >>> r = try_build_curly_route(pr, True)
+        >>> assert pr == r
         >>> r = try_build_curly_route('abc/{n}', True)
         >>> assert isinstance(r, RegexRoute)
 
 
         >>> r = try_build_curly_route('abc', True)
     """
+    if isinstance(pattern, RegexRoute):
+        return pattern
     if RE_CURLY_ROUTE.search(pattern):
         return RegexRoute(curly_convert(pattern), finishing, kwargs)
     return None
     """ There is no special tests to match regex selection
         strategy.
 
+        >>> pr = RegexRoute(r'abc', True)
+        >>> r = try_build_regex_route(pr, True)
+        >>> assert pr == r
         >>> r = try_build_regex_route(r'abc', True)
         >>> assert isinstance(r, RegexRoute)
     """
+    if isinstance(pattern, RegexRoute):
+        return pattern
     return RegexRoute(pattern, finishing, kwargs)
 
 
 def build_route(pattern, finishing, kwargs, route_builders):
-    """ If ``pattern`` is an object drived from ``Route`` than it
-        simply returned.
-
-        >>> pattern = PlainRoute(r'abc', False)
-        >>> r = build_route(pattern, False, None, [])
-        >>> assert pattern == r
-
-        If ``pattern`` is a string than try to find
-        sutable route builder to create a route.
+    """ Try to find suitable route builder to create a route.
 
         >>> from wheezy.routing.config import route_builders
         >>> r = build_route(r'abc', False, {'a': 1}, route_builders)
             ...
         LookupError: No matching route factory found
     """
-    if isinstance(pattern, Route):
-        route = pattern
+    for try_build_route in route_builders:
+        route = try_build_route(pattern, finishing, kwargs)
+        if route:
+            return route
     else:
-        for try_build_route in route_builders:
-            route = try_build_route(pattern, finishing, kwargs)
-            if route:
-                break
-        else:
-            raise LookupError("No matching route factory found")
-    return route
+        raise LookupError("No matching route factory found")

src/wheezy/routing/route.py

 class Route(object):
     """ Route abstract contract.
     """
+    __slots__ = ()
 
     def match(self, path):
         """ if the ``path`` matches, return the end of
         raise NotImplementedError()
 
 
-class PlainRoute(Route):
+class PlainRoute(object):
     """ Route based on string equalty operation.
     """
+    __slots__ = ('pattern', 'kwargs', 'matched', 'match')
 
     def __init__(self, pattern, finishing, kwargs=None):
         """ Initializes the route by given ``pattern``. If
 class RegexRoute(object):
     """ Route based on regular expression matching.
     """
+    __slots__ = ('match', 'path', 'parts', 'kwargs', 'regex')
 
     def __init__(self, pattern, finishing, kwargs=None):
         pattern = pattern.lstrip('^').rstrip('$')

src/wheezy/routing/router.py

 class PathRouter(object):
     """
     """
+    __slots__ = ('mapping', 'route_map', 'routers', 'route_builders')
 
     def __init__(self, route_builders=None):
         """
             >>> assert route == route2
             >>> assert inner == inner2
             >>> assert isinstance(inner, PathRouter)
+            >>> r = PathRouter()
+            >>> r.include(r'admin/', PathRouter())
+            >>> assert r.routers
         """
         # try build intermediate route
         route = build_route(pattern, False, kwargs, self.route_builders)
-        inner = PathRouter(self.route_builders)
-        inner.add_routes(included)
+        if isinstance(included, PathRouter):
+            inner = included
+        else:
+            inner = PathRouter(self.route_builders)
+            inner.add_routes(included)
         self.mapping.append((route, inner))
         self.routers.append((inner, route))
 

src/wheezy/routing/tests/test_builders.py

         """ ``pattern`` is an object drived from Route.
         """
         from wheezy.routing.builders import build_route
-        from wheezy.routing.route import Route
+        from wheezy.routing.route import PlainRoute
 
-        r = Route()
+        r = PlainRoute('', True)
 
-        self.assertEqual(r, build_route(r, False, None, None))
+        self.assertRaises(LookupError,
+                lambda: build_route(r, False, None, []))
 
     def test_found(self):
         """ Sutable route strategy has been found.

src/wheezy/routing/tests/test_router.py

     """ Test the ``PathRouter.add_routes``.
     """
 
-    def setUp(self):
-        from wheezy.routing.router import PathRouter
-        self.m = Mocker()
-        self.r = PathRouter()
-
-    def tearDown(self):
-        self.m.restore()
-        self.m.verify()
-
     def test_mapping_is_tuple_of_two(self):
         """ ``mapping`` is a tuple of two elements.
         """
-        m = [('pattern', 'handler')]
-        self.r.add_route = mock_add_route = self.m.mock()
-        expect(mock_add_route('pattern', 'handler', None, None))
-        self.m.replay()
-
-        self.r.add_routes(m)
+        from wheezy.routing.router import PathRouter
+        r = PathRouter()
+        r.add_routes([('pattern', 'handler')])
+        assert r.route_map
+        assert r.mapping
 
     def test_mapping_is_tuple_of_three(self):
         """ ``mapping`` is a tuple of three elements.
         """
-        m = [('pattern', 'handler', 'kwargs')]
-        self.r.add_route = mock_add_route = self.m.mock()
-        expect(
-            mock_add_route('pattern', 'handler', 'kwargs', None)
-        )
-        self.m.replay()
-
-        self.r.add_routes(m)
+        from wheezy.routing.router import PathRouter
+        r = PathRouter()
+        r.add_routes([('pattern', 'handler', {})])
+        assert r.route_map
+        assert r.mapping
 
     def test_mapping_is_tuple_of_four(self):
         """ ``mapping`` is a tuple of four elements.
         """
-        m = [('pattern', 'handler', 'kw', 'name')]
-        self.r.add_route = mock_add_route = self.m.mock()
-        expect(
-            mock_add_route('pattern', 'handler', 'kw', 'name')
-        )
-        self.m.replay()
-
-        self.r.add_routes(m)
+        from wheezy.routing.router import PathRouter
+        r = PathRouter()
+        r.add_routes([('pattern', 'handler', {}, 'name')])
+        assert r.route_map
+        assert r.mapping
 
     def test_include(self):
         """ ``include`` call.
         """
         from wheezy.routing.router import PathRouter
-
-        r = PathRouter()
-        self.r.include = mock_include = self.m.mock()
-        for h in ([], (), r):
-            expect(mock_include('pattern', h, 'kwargs'))
-        self.m.replay()
-
-        for h in ([], (), r):
-            m = [('pattern', h, 'kwargs')]
-            self.r.add_routes(m)
+        for h in ([], (), PathRouter()):
+            r = PathRouter()
+            r.add_routes([('pattern', h, {})])
+            assert r.mapping
+            assert r.routers
 
 
 class PathRouterMatchTestCase(unittest.TestCase):