Commits

Andriy Kornatskyy committed 48c12e2

Apply specialization cases for regex route path.

Comments (0)

Files changed (4)

src/wheezy/routing/route.py

 class RegexRoute(object):
     """ Route based on regular expression matching.
     """
-    __slots__ = ('match', 'path_format', 'kwargs', 'regex')
+    __slots__ = ('match', 'path', 'path_format', 'kwargs', 'regex')
 
     def __init__(self, pattern, finishing, kwargs=None):
         pattern = pattern.lstrip('^').rstrip('$')
         default_values = dict.fromkeys(default_values, '')
         if kwargs:
             self.match = self.match_with_kwargs
+            self.path = self.path_with_kwargs
             self.kwargs = dict(default_values, **kwargs)
         else:
             self.match = self.match_no_kwargs
+            self.path = self.path_no_kwargs
             self.kwargs = default_values
 
         pattern = '^' + pattern
             return (m.end(), merge(self.kwargs.copy(), kwargs))
         return -1, None
 
-    def path(self, values=None):
+    def path_with_kwargs(self, values=None):
         """ Build the path for the given route by substituting
             the named places of the regual expression.
 
-            >>> r = RegexRoute(
-            ...     r'abc/(?P<month>\d+)/(?P<day>\d+)',
-            ...     True
-            ... )
-            >>> r.path(dict(month=6, day=9))
-            'abc/6/9'
-            >>> r.path(dict(month=6))
-            'abc/6/'
-            >>> r.path()  # stripped by router
-            'abc//'
+            Specialization case: route was initialized with
+            default kwargs.
 
-            path for route with default values
             >>> r = RegexRoute(
             ...     r'abc/(?P<month>\d+)/(?P<day>\d+)',
             ...     True,
             return self.path_format % dict(self.kwargs, **values)
         else:
             return self.path_format % self.kwargs
+
+    def path_no_kwargs(self, values):
+        """ Build the path for the given route by substituting
+            the named places of the regual expression.
+
+            Specialization case: route was initialized with
+            no default kwargs.
+
+            >>> r = RegexRoute(
+            ...     r'abc/(?P<month>\d+)/(?P<day>\d+)',
+            ...     True
+            ... )
+            >>> r.path(dict(month=6, day=9))
+            'abc/6/9'
+            >>> r.path(dict(month=6))
+            Traceback (most recent call last):
+                ...
+            KeyError: 'day'
+        """
+        return self.path_format % values

src/wheezy/routing/router.py

         name = name or route_name(handler)
         if name in self.route_map:
             warn('PathRouter: overriding route: %s.' % name)
+        # build finishing route
         kwargs = kwargs or {}
+        route = build_route(pattern, True, kwargs, self.route_builders)
+        # allow build strategy specialize math/path cases
         kwargs['route_name'] = name
-        # build finishing route
-        route = build_route(pattern, True, kwargs, self.route_builders)
         self.route_map[name] = route.path
         if hasattr(route, 'exact_matches'):
             for pattern, kwargs in route.exact_matches:

src/wheezy/routing/tests/test_route.py

     def test_no_values(self):
         """
         """
-        path = self.r.path()
-
-        self.assertEquals('abc/', path)
+        self.assertRaises(TypeError, lambda: self.r.path())
 
     def test_with_values(self):
         """
     def test_no_values(self):
         """
         """
-        path = self.r.path()
-
-        self.assertEquals('abc/', path)
+        self.assertRaises(TypeError, lambda: self.r.path())
 
     def test_with_values(self):
         """

src/wheezy/routing/tests/test_router.py

         expect(
             mock_build_route(
                 '', True,
-                {'route_name': 'x'},
+                {},
                 self.r.route_builders
             )
         ).result(mock_route)
             mock_build_route(
                 'abc',
                 True,
-                {'route_name': 'n'},
+                {},
                 self.r.route_builders)
         ).result(mock_route)
         expect(mock_route.path({'a': 1})).result('abc')