~ubuntu-branches/ubuntu/quantal/python-django/quantal

« back to all changes in this revision

Viewing changes to django/utils/functional.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2009-07-29 11:26:28 UTC
  • mfrom: (1.1.8 upstream) (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090729112628-pg09ino8sz0sj21t
Tags: 1.1-1
* New upstream release.
* Merge from experimental:
  - Ship FastCGI initscript and /etc/default file in python-django's examples
    directory (Closes: #538863)
  - Drop "05_10539-sphinx06-compatibility.diff"; it has been applied
    upstream.
  - Bump Standards-Version to 3.8.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
251
251
            return func(*args, **kwargs)
252
252
        return lazy(func, *resultclasses)(*args, **kwargs)
253
253
    return wraps(func)(wrapper)
 
254
 
 
255
class LazyObject(object):
 
256
    """
 
257
    A wrapper for another class that can be used to delay instantiation of the
 
258
    wrapped class.
 
259
 
 
260
    This is useful, for example, if the wrapped class needs to use Django
 
261
    settings at creation time: we want to permit it to be imported without
 
262
    accessing settings.
 
263
    """
 
264
    def __init__(self):
 
265
        self._wrapped = None
 
266
 
 
267
    def __getattr__(self, name):
 
268
        if self._wrapped is None:
 
269
            self._setup()
 
270
        if name == "__members__":
 
271
            # Used to implement dir(obj)
 
272
            return self._wrapped.get_all_members()
 
273
        return getattr(self._wrapped, name)
 
274
 
 
275
    def __setattr__(self, name, value):
 
276
        if name == "_wrapped":
 
277
            # Assign to __dict__ to avoid infinite __setattr__ loops.
 
278
            self.__dict__["_wrapped"] = value
 
279
        else:
 
280
            if self._wrapped is None:
 
281
                self._setup()
 
282
            setattr(self._wrapped, name, value)
 
283
 
 
284
    def _setup(self):
 
285
        """
 
286
        Must be implemented by subclasses to initialise the wrapped object.
 
287
        """
 
288
        raise NotImplementedError
 
289