Add singleton support to LazyItemsDict to make it easier to use with arbitrary functi...
authorZac Medico <zmedico@gentoo.org>
Sat, 15 Apr 2006 05:26:44 +0000 (05:26 -0000)
committerZac Medico <zmedico@gentoo.org>
Sat, 15 Apr 2006 05:26:44 +0000 (05:26 -0000)
svn path=/main/trunk/; revision=3149

pym/portage.py
pym/portage_util.py

index f53436ba0e9443252fa60cf3b5baa7320570b750..7a8258547b6b9adc763b7e62b3a8f59eadc3218a 100644 (file)
@@ -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
 
index cd69733ba735505180bb1e2c8072a4f33bbba6d8..a1e2d02272497da7b6eff174de996f768fe581fe 100644 (file)
@@ -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):