From 5210ef39ff48f5701a837931881680e65aebf716 Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Sat, 15 Apr 2006 05:26:44 +0000 Subject: [PATCH] Add singleton support to LazyItemsDict to make it easier to use with arbitrary function calls. svn path=/main/trunk/; revision=3149 --- pym/portage.py | 29 +++++------------------------ pym/portage_util.py | 24 ++++++++++++++++++++---- 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/pym/portage.py b/pym/portage.py index f53436ba0..7a8258547 100644 --- a/pym/portage.py +++ b/pym/portage.py @@ -1404,18 +1404,9 @@ class config: # 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: @@ -6478,23 +6469,13 @@ def getvirtuals(myroot): 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 diff --git a/pym/portage_util.py b/pym/portage_util.py index cd69733ba..a1e2d0227 100644 --- a/pym/portage_util.py +++ b/pym/portage_util.py @@ -722,15 +722,31 @@ class LazyItemsDict(dict): 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): -- 2.26.2