bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
1996.1.1
by John Arbash Meinel
Adding a ScopeReplacer class, which can replace itself on demand |
1 |
# Copyright (C) 2006 by Canonical Ltd
|
2 |
#
|
|
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
#
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
#
|
|
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
16 |
||
17 |
"""Functionality to create lazy evaluation objects.
|
|
18 |
||
19 |
This includes waiting to import a module until it is actually used.
|
|
20 |
"""
|
|
21 |
||
22 |
import sys |
|
23 |
||
24 |
||
25 |
class ScopeReplacer(object): |
|
26 |
"""A lazy object that will replace itself in the appropriate scope. |
|
27 |
||
28 |
This object sits, ready to create the real object the first time it is
|
|
29 |
needed.
|
|
30 |
"""
|
|
31 |
||
32 |
__slots__ = ('_scope', '_factory', '_name') |
|
33 |
||
34 |
def __init__(self, scope, factory, name): |
|
35 |
"""Create a temporary object in the specified scope. |
|
36 |
Once used, a real object will be placed in the scope.
|
|
37 |
||
38 |
:param scope: The scope the object should appear in
|
|
39 |
:param factory: A callable that will create the real object.
|
|
40 |
It will be passed (self, scope, name)
|
|
41 |
:param name: The variable name in the given scope.
|
|
42 |
"""
|
|
43 |
self._scope = scope |
|
44 |
self._factory = factory |
|
45 |
self._name = name |
|
46 |
scope[name] = self |
|
47 |
||
48 |
def _replace(self): |
|
49 |
"""Actually replace self with other in the given scope""" |
|
50 |
factory = object.__getattribute__(self, '_factory') |
|
51 |
scope = object.__getattribute__(self, '_scope') |
|
52 |
name = object.__getattribute__(self, '_name') |
|
53 |
obj = factory(self, scope, name) |
|
54 |
scope[name] = obj |
|
55 |
return obj |
|
56 |
||
57 |
def _cleanup(self): |
|
58 |
"""Stop holding on to all the extra stuff""" |
|
59 |
del self._factory |
|
60 |
del self._scope |
|
61 |
del self._name |
|
62 |
||
63 |
def __getattribute__(self, attr): |
|
64 |
obj = object.__getattribute__(self, '_replace')() |
|
65 |
object.__getattribute__(self, '_cleanup')() |
|
66 |
return getattr(obj, attr) |
|
67 |
||
68 |
def __call__(self, *args, **kwargs): |
|
69 |
obj = object.__getattribute__(self, '_replace')() |
|
70 |
object.__getattribute__(self, '_cleanup')() |
|
71 |
return obj(*args, **kwargs) |
|
72 |
||
73 |