[Checkins] SVN: bobo/branches/d2m.routestack/bobo add routestack information, make tests run

Michael Haubenwallner michael at d2m.at
Wed Aug 12 05:01:25 EDT 2009


Log message for revision 102706:
  add routestack information, make tests run

Changed:
  U   bobo/branches/d2m.routestack/bobo/src/bobo.py
  U   bobo/branches/d2m.routestack/bobodoctestumentation/src/bobodoctestumentation/decorator.test

-=-
Modified: bobo/branches/d2m.routestack/bobo/src/bobo.py
===================================================================
--- bobo/branches/d2m.routestack/bobo/src/bobo.py	2009-08-12 08:49:49 UTC (rev 102705)
+++ bobo/branches/d2m.routestack/bobo/src/bobo.py	2009-08-12 09:01:25 UTC (rev 102706)
@@ -36,6 +36,7 @@
 
 import re
 import sys
+import types
 import webob
 
 bbbbad_errors = KeyboardInterrupt, SystemExit, MemoryError
@@ -198,6 +199,8 @@
         except bbbbad_errors:
             raise
         except Exception:
+            if self.config.get('debug', False):
+                print 'Routestack:', request.route_stack.routes
             if not hasattr(self, 'exception'):
                 raise
             return self.exception(request, method, sys.exc_info())
@@ -206,6 +209,7 @@
         """Handle a WSGI application request.
         """
         request = webob.Request(environ)
+        request.route_stack = Routestack(debug=self.config.get('debug',False))
         if request.charset is None:
             # Maybe middleware can be more tricky?
             request.charset = 'utf8'
@@ -584,6 +588,7 @@
     def bobo_response(self, *args):
         request, path, method = args[-3:]
         route_data = self.match(request, path, method)
+        request.route_stack.update(self.bobo_route, route_data, path)
         if route_data is None:
             return self.bobo_sub_find(*args)
 
@@ -1045,6 +1050,7 @@
     def bobo_response(self, *args):
         request, path, method = args[-3:]
         route_data = self.match(request, path)
+        request.route_stack.update(self.bobo_route, route_data, path)
         if route_data is None:
             return self.bobo_sub_find(*args)
 
@@ -1176,6 +1182,7 @@
     def bobo_response(self, request, path, method):
         for matcher in matchers:
             route_data = matcher(route, path)
+            request.route_stack.update(route, route_data, path)
             if route_data:
                 route_data, path = route_data
                 resource = ob(request, **route_data)
@@ -1285,3 +1292,32 @@
 
     This exception can be raised by application code.
     """
+
+class Routestack:
+    """A sequence that holds the already consumed path, its matching route and
+    route data and the remaining path.
+    """
+    def __init__(self, debug=False):
+        self.routes = []
+        self.debug = debug
+
+    def routestack(self):
+        return [part['route'] for part in self.routes]
+
+    def pathstack(self):
+        return [part['path'] for part in self.routes]
+
+    def update(self, route, route_data, path):
+        if self.debug:
+            self.routes.append(route)
+            if route_data is None:
+                self.routes.pop()
+            else:
+                params = route_data
+                path_ = path
+                leftover = ''
+                if type(route_data) == types.TupleType:
+                    params, leftover = route_data
+                    if len(leftover) > 0:
+                        path_ = path[:- len(leftover)]
+                self.routes[-1] = {'route':route, 'route_data':params, 'path':path_, 'leftover':leftover}

Modified: bobo/branches/d2m.routestack/bobodoctestumentation/src/bobodoctestumentation/decorator.test
===================================================================
--- bobo/branches/d2m.routestack/bobodoctestumentation/src/bobodoctestumentation/decorator.test	2009-08-12 08:49:49 UTC (rev 102705)
+++ bobo/branches/d2m.routestack/bobodoctestumentation/src/bobodoctestumentation/decorator.test	2009-08-12 09:01:25 UTC (rev 102706)
@@ -174,6 +174,9 @@
     ... def hi(request, name=None):
     ...     print 'request:'
     ...     print str(request).replace('\r', '')
+    ...     if hasattr(request, 'route_stack'):
+    ...         print '-----'
+    ...         print 'Routestack:', request.route_stack.routes
     ...     print '-----'
     ...     return 'Hi %s.' % name
 
@@ -197,6 +200,7 @@
     ...         env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded'
     ...         env['REQUEST_METHOD'] = 'POST'
     ...     request = webob.Request.blank(url, env, **kw)
+    ...     request.route_stack = bobo.Routestack(debug=True)
     ...     try:
     ...         found = resource.bobo_response(
     ...             request, request.path_info, request.method)
@@ -213,6 +217,8 @@
     <BLANKLINE>
     <BLANKLINE>
     -----
+    Routestack: [{'route_data': {}, 'path': '/foo', 'route': '/foo', 'leftover': ''}]
+    -----
     BoboException:
     {'body': 'Hi None.',
      'content_type': 'text/html; charset=UTF-8',
@@ -233,6 +239,8 @@
     <BLANKLINE>
     <BLANKLINE>
     -----
+    Routestack: [{'route_data': {'name': 'bar'}, 'path': '/bar', 'route': '/:name', 'leftover': ''}]
+    -----
     BoboException:
     {'body': 'Hi bar.',
      'content_type': 'text/plain; charset=Latin-1',



More information about the Checkins mailing list