summaryrefslogtreecommitdiff
path: root/django/utils
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2009-03-02 04:48:22 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2009-03-02 04:48:22 +0000
commitcf3071242a289e20526ade81021b1f895fde706d (patch)
treeaaf91d5c55097ac9d98050f62e22222e7e7f5ff5 /django/utils
parentec710220dd347ed362a0a535d6a848e906d102e9 (diff)
Factor out some common pieces of django.conf.LazySettings.
This is in preparation for some reuse elsewhere in the core code. git-svn-id: http://code.djangoproject.com/svn/django/trunk@9945 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils')
-rw-r--r--django/utils/functional.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/django/utils/functional.py b/django/utils/functional.py
index 9a12eda5a1..23614d1712 100644
--- a/django/utils/functional.py
+++ b/django/utils/functional.py
@@ -251,3 +251,39 @@ def allow_lazy(func, *resultclasses):
return func(*args, **kwargs)
return lazy(func, *resultclasses)(*args, **kwargs)
return wraps(func)(wrapper)
+
+class LazyObject(object):
+ """
+ A wrapper for another class that can be used to delay instantiation of the
+ wrapped class.
+
+ This is useful, for example, if the wrapped class needs to use Django
+ settings at creation time: we want to permit it to be imported without
+ accessing settings.
+ """
+ def __init__(self):
+ self._wrapped = None
+
+ def __getattr__(self, name):
+ if self._wrapped is None:
+ self._setup()
+ if name == "__members__":
+ # Used to implement dir(obj)
+ return self._wrapped.get_all_members()
+ return getattr(self._wrapped, name)
+
+ def __setattr__(self, name, value):
+ if name == "_wrapped":
+ # Assign to __dict__ to avoid infinite __setattr__ loops.
+ self.__dict__["_wrapped"] = value
+ else:
+ if self._wrapped is None:
+ self._setup()
+ setattr(self._wrapped, name, value)
+
+ def _setup(self):
+ """
+ Must be implemented by subclasses to initialise the wrapped object.
+ """
+ raise NotImplementedError
+