From bc42fc14334de10b0c6adbc97441accebb2e458c Mon Sep 17 00:00:00 2001 From: David Wilemski Date: Sat, 2 Jul 2011 03:06:25 -0400 Subject: [PATCH] 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. --- tornado/web.py | 11 +++++++++++ 1 file changed, 11 insertions(+) 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: -- 2.47.2