]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Added dynamic loading of request handlers
authorDavid Wilemski <david@davidwilemski.com>
Sat, 2 Jul 2011 07:06:25 +0000 (03:06 -0400)
committerDavid Wilemski <david@davidwilemski.com>
Sat, 2 Jul 2011 07:06:25 +0000 (03:06 -0400)
This commit implements gh-124.

Users can specify "module.ClassName" rather than load a module that
contains a request handler and then refering to the request handler
class.

tornado/web.py

index 2e5a4c2f79963f7eb978c89001f9b4707cba7d04..ffcbfd1d08d6100bbcf9c672aa372b384045a975 100644 (file)
@@ -1173,6 +1173,17 @@ class Application(object):
                 assert len(spec) in (2, 3)
                 pattern = spec[0]
                 handler = spec[1]
+
+                if isinstance(handler, str):
+                    # Lazy load the Module and instantiate the class
+                    # Must be a fully qualified name (module.ClassName)
+                    parts = handler.split('.')
+                    handler = ".".join(parts[:-1]) # create module name 
+                    handler = __import__(handler) # import the module
+                    for part in parts[1:]:
+                        handler = getattr(handler, part)
+                    # The class has now been loaded and we can continue
+
                 if len(spec) == 3:
                     kwargs = spec[2]
                 else: