From 452ebff3865cd475b43764d6986f2ff3f289863b Mon Sep 17 00:00:00 2001 From: Alek Storm Date: Sat, 10 Sep 2011 23:25:10 +0000 Subject: [PATCH] Move web._O to util.ObjectDict --- tornado/httputil.py | 2 +- tornado/test/web_test.py | 6 +++--- tornado/util.py | 12 ++++++++++++ tornado/web.py | 18 +++--------------- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/tornado/httputil.py b/tornado/httputil.py index 3b78dc640..f27148c9f 100644 --- a/tornado/httputil.py +++ b/tornado/httputil.py @@ -20,7 +20,7 @@ import logging import urllib import re -from tornado.util import b +from tornado.util import b, ObjectDict class HTTPHeaders(dict): """A dictionary that maintains Http-Header-Case for all keys. diff --git a/tornado/test/web_test.py b/tornado/test/web_test.py index ca473d899..6898ffc03 100644 --- a/tornado/test/web_test.py +++ b/tornado/test/web_test.py @@ -2,8 +2,8 @@ from tornado.escape import json_decode, utf8, to_unicode, recursive_unicode, nat from tornado.iostream import IOStream from tornado.template import DictLoader from tornado.testing import LogTrapTestCase, AsyncHTTPTestCase -from tornado.util import b, bytes_type -from tornado.web import RequestHandler, _O, authenticated, Application, asynchronous, url, HTTPError, StaticFileHandler, _create_signature +from tornado.util import b, bytes_type, ObjectDict +from tornado.web import RequestHandler, authenticated, Application, asynchronous, url, HTTPError, StaticFileHandler, _create_signature import binascii import logging @@ -17,7 +17,7 @@ class CookieTestRequestHandler(RequestHandler): def __init__(self): # don't call super.__init__ self._cookies = {} - self.application = _O(settings=dict(cookie_secret='0123456789')) + self.application = ObjectDict(settings=dict(cookie_secret='0123456789')) def get_cookie(self, name): return self._cookies.get(name) diff --git a/tornado/util.py b/tornado/util.py index 4ac59908f..6752401af 100644 --- a/tornado/util.py +++ b/tornado/util.py @@ -1,5 +1,17 @@ """Miscellaneous utility functions.""" +class ObjectDict(dict): + """Makes a dictionary behave like an object.""" + def __getattr__(self, name): + try: + return self[name] + except KeyError: + raise AttributeError(name) + + def __setattr__(self, name, value): + self[name] = value + + def import_object(name): """Imports an object by name. diff --git a/tornado/web.py b/tornado/web.py index 59445209b..e87f33054 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -82,7 +82,7 @@ from tornado import locale from tornado import stack_context from tornado import template from tornado.escape import utf8, _unicode -from tornado.util import b, bytes_type, import_object +from tornado.util import b, bytes_type, import_object, ObjectDict try: from io import BytesIO # python 3 @@ -105,14 +105,14 @@ class RequestHandler(object): self._finished = False self._auto_finish = True self._transforms = None # will be set in _execute - self.ui = _O((n, self._ui_method(m)) for n, m in + self.ui = ObjectDict((n, self._ui_method(m)) for n, m in application.ui_methods.iteritems()) # UIModules are available as both `modules` and `_modules` in the # template namespace. Historically only `modules` was available # but could be clobbered by user additions to the namespace. # The template {% module %} directive looks in `_modules` to avoid # possible conflicts. - self.ui["_modules"] = _O((n, self._ui_module(n, m)) for n, m in + self.ui["_modules"] = ObjectDict((n, self._ui_module(n, m)) for n, m in application.ui_modules.iteritems()) self.ui["modules"] = self.ui["_modules"] self.clear() @@ -1911,15 +1911,3 @@ def _create_signature(secret, *parts): hash = hmac.new(utf8(secret), digestmod=hashlib.sha1) for part in parts: hash.update(utf8(part)) return utf8(hash.hexdigest()) - - -class _O(dict): - """Makes a dictionary behave like an object.""" - def __getattr__(self, name): - try: - return self[name] - except KeyError: - raise AttributeError(name) - - def __setattr__(self, name, value): - self[name] = value -- 2.47.2