From: David Wilemski Date: Sat, 2 Jul 2011 07:06:25 +0000 (-0400) Subject: Added dynamic loading of request handlers X-Git-Tag: v2.1.0~134^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bc42fc14334de10b0c6adbc97441accebb2e458c;p=thirdparty%2Ftornado.git Added dynamic loading of request handlers 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. --- diff --git a/tornado/web.py b/tornado/web.py index 2e5a4c2f7..ffcbfd1d0 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -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: