# XXX Global usage of db... Needs to go away somehow.
global db, root
if "auto" in self["USE_ORDER"].split(":") and db.has_key(root) and db[root].has_key("vartree"):
- class LazyAutouse(object):
- def __init__(self, *pargs, **kwargs):
- self._use = None
- self._pargs = pargs
- self._kwargs = kwargs
- def __call__(self):
- if self._use is None:
- global db, root
- self._use = autouse(db[root]["vartree"], *self._pargs, **self._kwargs)
- return self._use
self.configdict["auto"] = portage_util.LazyItemsDict(self.configdict["auto"])
- self.configdict["auto"].addLazyItem("USE", LazyAutouse(use_cache=use_cache))
+ self.configdict["auto"].addLazySingleton("USE", autouse,
+ db[root]["vartree"], use_cache=use_cache)
else:
self.configdict["auto"]["USE"]=""
else:
return settings.getvirtuals(myroot)
def do_vartree(mysettings):
- class LazyVirtualsItem(object):
- def __init__(self, myroot):
- self._myroot = myroot
- self._virtuals = None
- def __call__(self):
- if self._virtuals is None:
- global settings
- self._virtuals = settings.getvirtuals(self._myroot)
- return self._virtuals
-
- global db, root
+ global db, root, settings
db["/"] = portage_util.LazyItemsDict()
- db["/"].addLazyItem("virtuals", LazyVirtualsItem("/"))
+ db["/"].addLazySingleton("virtuals", settings.getvirtuals, "/")
db["/"]["vartree"] = vartree("/")
if root!="/":
db[root] = portage_util.LazyItemsDict()
- db[root].addLazyItem("virtuals", LazyVirtualsItem(root))
+ db[root].addLazySingleton("virtuals", settings.getvirtuals, root)
db[root]["vartree"] = vartree(root)
#We need to create the vartree first, then load our settings, and then set up our other trees
self.update(initial_items)
if isinstance(initial_items, LazyItemsDict):
self.lazy_items.update(initial_items.lazy_items)
- def addLazyItem(self, item_key, value_callable):
+ def addLazyItem(self, item_key, value_callable, *pargs, **kwargs):
"""Add a lazy item for the given key. When the item is requested,
- value_callable will be called with no arguments."""
- self.lazy_items[item_key] = value_callable
+ value_callable will be called with *pargs and **kwargs arguments."""
+ self.lazy_items[item_key] = (value_callable, pargs, kwargs)
# make it show up in self.keys(), etc...
dict.__setitem__(self, item_key, None)
+ def addLazySingleton(self, item_key, value_callable, *pargs, **kwargs):
+ """This is like addLazyItem except value_callable will only be called
+ a maximum of 1 time and the result will be cached for future requests."""
+ class SingletonItem(object):
+ def __init__(self, value_callable, *pargs, **kwargs):
+ self._callable = value_callable
+ self._pargs = pargs
+ self._kwargs = kwargs
+ self._called = False
+ def __call__(self):
+ if not self._called:
+ self._called = True
+ self._value = self._callable(*self._pargs, **self._kwargs)
+ return self._value
+ self.addLazyItem(item_key, SingletonItem(value_callable, *pargs, **kwargs))
def __getitem__(self, item_key):
if item_key in self.lazy_items:
- return self.lazy_items[item_key]()
+ value_callable, pargs, kwargs = self.lazy_items[item_key]
+ return value_callable(*pargs, **kwargs)
else:
return dict.__getitem__(self, item_key)
def __setitem__(self, item_key, value):