]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Add an initialize() method to RequestHandler for use by subclasses.
authorBen Darnell <bdarnell@beaker.local>
Mon, 26 Jul 2010 19:04:00 +0000 (12:04 -0700)
committerBen Darnell <bdarnell@beaker.local>
Mon, 26 Jul 2010 19:04:00 +0000 (12:04 -0700)
This is simpler than overriding __init__ since it doesn't require
knowledge of the application and request arguments that must be
passed through to the superclass's __init__.

tornado/web.py

index d2a8c4c98af99d4fcdbeda47800681a89e5a7090..2dd2686fac1884a9f1540b3320589704d98fa888 100644 (file)
@@ -83,7 +83,7 @@ class RequestHandler(object):
     """
     SUPPORTED_METHODS = ("GET", "HEAD", "POST", "DELETE", "PUT")
 
-    def __init__(self, application, request):
+    def __init__(self, application, request, **kwargs):
         self.application = application
         self.request = request
         self._headers_written = False
@@ -99,6 +99,27 @@ class RequestHandler(object):
         if hasattr(self.request, "connection"):
             self.request.connection.stream.set_close_callback(
                 self.on_connection_close)
+        self.initialize(**kwargs)
+
+    def initialize(self):
+        """Hook for subclass initialization.
+
+        A dictionary passed as the third argument of a url spec will be
+        supplied as keyword arguments to initialize().
+
+        Example:
+            class ProfileHandler(RequestHandler):
+                def initialize(self, database):
+                    self.database = database
+
+                def get(self, username):
+                    ...
+
+            app = Application([
+                (r'/user/(.*)', ProfileHandler, dict(database=database)),
+                ])
+        """
+        pass
 
     @property
     def settings(self):