mirror of
https://github.com/django/django.git
synced 2025-09-18 00:10:22 +00:00
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
This commit is contained in:
parent
ec710220dd
commit
cf3071242a
3 changed files with 44 additions and 29 deletions
|
@ -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
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue