From: Ben Darnell Date: Sun, 5 May 2013 16:58:34 +0000 (-0400) Subject: Create UIModule proxies on demand instead of in RequestHandler init. X-Git-Tag: v3.1.0~79 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=84bd64bd66a8ebe031184af7317fa7c3429e9787;p=thirdparty%2Ftornado.git Create UIModule proxies on demand instead of in RequestHandler init. It would be nice to do something similar for UI methods, but it's tricker and they are less common (there are three UIModules defined by default for every handler). --- diff --git a/tornado/web.py b/tornado/web.py index ff3572c8b..f1f83b85a 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -104,6 +104,22 @@ except ImportError: from urllib.parse import urlencode # py3 +class _UIModuleNamespace(object): + """Lazy namespace which creates UIModule proxies bound to a handler.""" + def __init__(self, handler, ui_modules): + self.handler = handler + self.ui_modules = ui_modules + + def __getitem__(self, key): + return self.handler._ui_module(key, self.ui_modules[key]) + + def __getattr__(self, key): + try: + return self[key] + except KeyError as e: + raise AttributeError(str(e)) + + class RequestHandler(object): """Subclass this class and define `get()` or `post()` to make a handler. @@ -136,9 +152,8 @@ class RequestHandler(object): # but could be clobbered by user additions to the namespace. # The template {% module %} directive looks in `_tt_modules` to avoid # possible conflicts. - self.ui["_tt_modules"] = ObjectDict( - (n, self._ui_module(n, m)) for n, m in - application.ui_modules.items()) + self.ui["_tt_modules"] = _UIModuleNamespace(self, + application.ui_modules) self.ui["modules"] = self.ui["_tt_modules"] self.clear() # Check since connection is not available in WSGI