Walt Woods committed de8495a

Changed SubHandler to SegmentHandler, fills out request.segments

Comments (0)

Files changed (3)


     return decorated
-class SubHandler(object):
+class SegmentHandler(object):
     """This is a utility class designed to be mounted on a cherrypy object
-    to serve up a different handler based on a parameter in the url.  For
+    to serve up a different handler based on a segment of the url.  For
     instance, if your application has multiple databases, each of which
     have requests that are handled by a common DatabaseHandler class, but you
     want the database ID to appear as the first part of the URL, you would
-    use SubHandler as follows:
+    use SegmentHandler as follows:
     class DatabaseHandler:
         def __init__(self, myDbName):
     class Root:
-        db = cherrypy.SubHandler(DatabaseHandler, 'myDbName')
+        db = cherrypy.SegmentHandler(DatabaseHandler, 'myDbName')
     This will make the URL '/db/myDb/klar' first create a DatabaseHandler
     object initialized with the kwarg myDbName='myDb'.  Then the cherrypy
     default dispatcher would look for the property arg.klar in that 
-    DatabaseHandler object to handle the request.
+    DatabaseHandler object to handle the request.  Additionally, the parameter
+    request.segments['myDbName'] would be set to 'myDb'.
     Note that if vpath elements end up in a path that does not exist, 
-    SubHandler will still create the handler, but will not pass those kwargs
+    SegmentHandler will still create the handler, but will not pass those kwargs
     not existing in the path.  The caveat to this is that if no argument is 
-    passed, SubHandler() will not be executed, and any parent object's default()
-    method will be used instead.  That is, at least one of the arguments
-    specified in SubHandler() must be passed in the URL for SubHandler
-    to do anything.
+    passed, SegmentHandler() will not be executed, and any parent object's 
+    default() method will be used instead.  That is, at least one of the 
+    arguments specified in SegmentHandler() must be passed in the URL for 
+    SegmentHandler to do anything.
     def __init__(self, handler_type, *args):
         self._handler_type = handler_type
         for arg in self._args:
             if len(vpath) == 0:
-            parms[arg] = vpath.pop(0)
+            value = vpath.pop(0)
+            serving.request.segments[arg] = value
+            parms[arg] = value
         return self._handler_type(**parms)
-        request.params.update(parms)
 def url(path="", qs="", script_name=None, base=None, relative=None):
     """Create an absolute URL for the given path.


     variables. This is populated in two stages: GET params are added
     before the 'on_start_resource' hook, and POST params are added
     between the 'before_request_body' and 'before_handler' hooks."""
+    segments = {}
+    """
+    A dict which contains any segment values popped off by 
+    cherrypy.SegmentHandler."""
     # Message attributes
     header_list = []
             path = path or "/"
             self.query_string = query_string or ''
             self.params = {}
+            self.segments = {}
             # Compare request and server HTTP protocol versions, in case our
             # server does not support the requested protocol. Limit our output


         """Test cherrypy.popargs() with a function call handler"""
     ParameterizedPopArgs = cherrypy.popargs('a', handler=ParameterizedHandler)(ParameterizedPopArgs)
-    class SubHandler:
-        """Test cherrypy.SubHandler()"""
+    class TestSegmentHandler:
+        """Test cherrypy.SegmentHandler()"""
         def __init__(self, **kwargs):
             self.kwargs = kwargs
         def index(self):
             return str(self.kwargs) = True
         def method(self):
-            return "SubHandler.method()"
+            return "SegmentHandler.method()" = True
-    class SubHandler2:
-        """Second test handler; ensures that nested class doesn't cause
-        issues.
+    class TestSegmentHandler2:
+        """Second test handler; ensures that multiple segment handlers work ok.
+        Also used to check that cherrypy.request.segments gets set with the
+        correct variable.
         def __init__(self, a):
             self.a = a
         def index(self):
-            return "second handler"
+            return "second handler: " + cherrypy.request.segments['a'] = True
     Root.decorated = DecoratedPopArgs()
     Root.undecorated = NonDecoratedPopArgs()
     Root.index_only = IndexOnly()
     Root.parameter_test = ParameterizedPopArgs()
-    Root.subhandler = cherrypy.SubHandler(SubHandler, 'a', 'b')
-    Root.subhandler2 = cherrypy.SubHandler(SubHandler2, 'a')
+    Root.segmenthandler = cherrypy.SegmentHandler(TestSegmentHandler, 'a', 'b')
+    Root.segmenthandler2 = cherrypy.SegmentHandler(TestSegmentHandler2, 'a')
     Root.users = UserContainerNode()
         # No params falls back to default
-        self.getPage("/subhandler/")
-        self.assertBody("default ('subhandler',)")
+        self.getPage("/segmenthandler/")
+        self.assertBody("default ('segmenthandler',)")
-        self.getPage("/subhandler/342/")
+        self.getPage("/segmenthandler/342/")
         self.assertBody("{'a': '342'}")
-        self.getPage("/subhandler/344/245/")
+        self.getPage("/segmenthandler/344/245/")
         self.assertBody("{'a': '344', 'b': '245'}")
-        self.getPage("/subhandler/344/245/method/")
-        self.assertBody("SubHandler.method()")
+        self.getPage("/segmenthandler/344/245/method/")
+        self.assertBody("SegmentHandler.method()")
-        self.getPage("/subhandler2/a/")
-        self.assertBody("second handler")
+        self.getPage("/segmenthandler2/b/")
+        self.assertBody("second handler: b")