From f887906ec3def9d96b622f2da737f7b1cb13b3ea Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Sun, 22 May 2011 13:31:09 -0400 Subject: [PATCH] =?utf8?q?cleanup=20=E2=80=94=20comments?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- requests/config.py | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/requests/config.py b/requests/config.py index 99ffc33..6d6d190 100644 --- a/requests/config.py +++ b/requests/config.py @@ -8,14 +8,10 @@ This module provides the Requests settings feature set. """ -# Time (in seconds) to allow the request to connect to -# the remote host before timing it out. - -# timeout = None - - class Settings(object): _singleton = {} + + # attributes with defaults __attrs__ = ('timeout',) def __init__(self, **kwargs): @@ -23,30 +19,37 @@ class Settings(object): self.__dict__ = self._singleton - def __getattribute__(self, key): - if key in object.__getattribute__(self, '__attrs__'): - try: - return object.__getattribute__(self, key) - except AttributeError: - return None - return object.__getattribute__(self, key) + + def __call__(self, *args, **kwargs): + # new instance of class to call + r = self.__class__() + + # cache previous settings for __exit__ + r.__cache = self.__dict__.copy() + + # set new settings + self.__dict__.update(*args, **kwargs) + + return r def __enter__(self): pass + def __exit__(self, *args): + # restore cached copy self.__dict__.update(self.__cache.copy()) del self.__cache - def __call__(self, *args, **kwargs): - r = self.__class__() - r.__cache = self.__dict__.copy() - self.__dict__.update(*args, **kwargs) - - return r - + def __getattribute__(self, key): + if key in object.__getattribute__(self, '__attrs__'): + try: + return object.__getattribute__(self, key) + except AttributeError: + return None + return object.__getattribute__(self, key) settings = Settings() \ No newline at end of file -- 2.7.4