settings -> config
authorKenneth Reitz <me@kennethreitz.com>
Sun, 22 May 2011 17:27:17 +0000 (13:27 -0400)
committerKenneth Reitz <me@kennethreitz.com>
Sun, 22 May 2011 17:27:17 +0000 (13:27 -0400)
reimpliment settings as singleton

requests/config.py [new file with mode: 0644]
requests/settings.py [deleted file]

diff --git a/requests/config.py b/requests/config.py
new file mode 100644 (file)
index 0000000..99ffc33
--- /dev/null
@@ -0,0 +1,52 @@
+# -*- coding: utf-8 -*-
+
+"""
+requests.config
+~~~~~~~~~~~~~~~
+
+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 = {}
+    __attrs__ = ('timeout',)
+
+    def __init__(self, **kwargs):
+        super(Settings, self).__init__()
+
+        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 __enter__(self):
+        pass
+
+    def __exit__(self, *args):
+
+        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
+
+
+settings = Settings()
\ No newline at end of file
diff --git a/requests/settings.py b/requests/settings.py
deleted file mode 100644 (file)
index 1dc9007..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-# -*- coding: utf-8 -*-
-
-"""
-requests.settings
-~~~~~~~~~~~~~~~~~
-
-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):
-
-    def __init__(self, **settings):
-        self._cache_settings(**settings)
-        self._alter_settings(**settings)
-
-    def __enter__(self):
-        pass
-
-    def __exit__(self, type, value, traceback):
-        self._restore_settings()
-
-    def _cache_settings(self, **settings):
-        self.cache = {}
-        for setting in settings:
-            self.cache[setting] = globals()[setting]
-
-    def _alter_settings(self, **settings):
-        for setting, value in settings.items():
-            globals()[setting] = value
-
-    def _restore_settings(self):
-        for setting, value in self.cache.items():
-            globals()[setting] = value