In python-3.0, the UserDict.DictMixin class has been replaced by
authorZac Medico <zmedico@gentoo.org>
Thu, 19 Feb 2009 02:52:36 +0000 (02:52 -0000)
committerZac Medico <zmedico@gentoo.org>
Thu, 19 Feb 2009 02:52:36 +0000 (02:52 -0000)
Mapping and MutableMapping from the collections module, but 2to3
doesn't currently account for this change:

http://bugs.python.org/issue2876

As a workaround for the above issue, implement Mapping and
MutableMapping classes as substitutes for UserDict.DictMixin so
that code converted via 2to3 will run.

svn path=/main/trunk/; revision=12628

pym/_emerge/__init__.py
pym/portage/__init__.py
pym/portage/cache/mappings.py

index 10b85ea5730b6553b51baacce7fcac9425387bb7..00ef6f705ae145e69d5a8e57c929d31c2335e762 100644 (file)
@@ -56,7 +56,6 @@ from portage.sets import load_default_config, SETPREFIX
 from portage.sets.base import InternalPackageSet
 
 from itertools import chain, izip
-from UserDict import DictMixin
 
 try:
        import cPickle as pickle
@@ -4011,7 +4010,7 @@ class Dependency(SlotObject):
                if self.depth is None:
                        self.depth = 0
 
-class BlockerCache(DictMixin):
+class BlockerCache(portage.cache.mappings.MutableMapping):
        """This caches blockers of installed packages so that dep_check does not
        have to be done for every single installed package on every invocation of
        emerge.  The cache is invalidated whenever it is detected that something
@@ -4169,11 +4168,6 @@ class BlockerCache(DictMixin):
                """
                return self.BlockerData(*self._cache_data["blockers"][cpv])
 
-       def keys(self):
-               """This needs to be implemented so that self.__repr__() doesn't raise
-               an AttributeError."""
-               return list(self)
-
 class BlockerDB(object):
 
        def __init__(self, root_config):
index bc76ce8888d6e76b0b917d3dc1a9fddbd844b1e3..fa4e721be5377fb639c29e373b963ef39449f2ba 100644 (file)
@@ -28,7 +28,6 @@ try:
        import commands
        from time import sleep
        from random import shuffle
-       import UserDict
        from itertools import chain, izip
        import platform
        import warnings
@@ -7249,7 +7248,7 @@ from portage.dbapi.bintree import bindbapi, binarytree
 from portage.dbapi.vartree import vardbapi, vartree, dblink
 from portage.dbapi.porttree import close_portdbapi_caches, portdbapi, portagetree
 
-class FetchlistDict(UserDict.DictMixin):
+class FetchlistDict(portage.cache.mappings.Mapping):
        """This provide a mapping interface to retrieve fetch lists.  It's used
        to allow portage.manifest.Manifest to access fetch lists via a standard
        mapping interface rather than use the dbapi directly."""
@@ -7269,10 +7268,17 @@ class FetchlistDict(UserDict.DictMixin):
        def has_key(self, pkg_key):
                """Returns true if the given package exists within pkgdir."""
                return pkg_key in self
+
+       def __iter__(self):
+               return iter(self.portdb.cp_list(self.cp, mytree=self.mytree))
+
        def keys(self):
                """Returns keys for all packages within pkgdir"""
                return self.portdb.cp_list(self.cp, mytree=self.mytree)
 
+       if sys.hexversion >= 0x3000000:
+               keys = __iter__
+
 def pkgmerge(mytbz2, myroot, mysettings, mydbapi=None,
        vartree=None, prev_mtimes=None, blockers=None):
        """will merge a .tbz2 file, returning a list of runtime dependencies
index 010eb7f8e8d8b19bad1477acf365ecdcebeb693e..74ed5fa5eeae1cc90d1c369aff45c001bbd1cc98 100644 (file)
 # $Id$
 
 import sys
-import UserDict
 import warnings
 import weakref
 
-class ProtectedDict(UserDict.DictMixin):
+class Mapping(object):
+       """
+       In python-3.0, the UserDict.DictMixin class has been replaced by
+       Mapping and MutableMapping from the collections module, but 2to3
+       doesn't currently account for this change:
+
+           http://bugs.python.org/issue2876
+
+       As a workaround for the above issue, use this class as a substitute
+       for UserDict.DictMixin so that code converted via 2to3 will run.
+       """
+
+       def __iter__(self):
+               return self.iterkeys()
+
+       def keys(self):
+               return list(self.__iter__())
+
+       def has_key(self, key):
+               warnings.warn("portage.cache.mappings.Mapping.has_key() " + \
+                       "is deprecated, use the in operator instead", DeprecationWarning)
+               return key in self
+
+       def __contains__(self, key):
+               try:
+                       value = self[key]
+               except KeyError:
+                       return False
+               return True
+
+       def iteritems(self):
+               for k in self:
+                       yield (k, self[k])
+
+       def iterkeys(self):
+               return self.__iter__()
+
+       def itervalues(self):
+               for _, v in self.iteritems():
+                       yield v
+
+       def values(self):
+               return [v for _, v in self.iteritems()]
+
+       def items(self):
+               return list(self.iteritems())
+
+       def get(self, key, default=None):
+               try:
+                       return self[key]
+               except KeyError:
+                       return default
+
+       def __repr__(self):
+               return repr(dict(self.iteritems()))
+
+       def __len__(self):
+               return len(self.keys())
+
+       if sys.hexversion >= 0x3000000:
+               items = iteritems
+               keys = __iter__
+               values = itervalues
+
+class MutableMapping(Mapping):
+       """
+       A mutable vesion of the Mapping class.
+       """
+
+       def clear(self):
+               for key in self.keys():
+                       del self[key]
+
+       def setdefault(self, key, default=None):
+               try:
+                       return self[key]
+               except KeyError:
+                       self[key] = default
+               return default
+
+       def pop(self, key, *args):
+               if len(args) > 1:
+                       raise TypeError("pop expected at most 2 arguments, got " + \
+                               repr(1 + len(args)))
+               try:
+                       value = self[key]
+               except KeyError:
+                       if args:
+                               return args[0]
+                       raise
+               del self[key]
+               return value
+
+       def popitem(self):
+               try:
+                       k, v = self.iteritems().next()
+               except StopIteration:
+                       raise KeyError('container is empty')
+               del self[k]
+               return (k, v)
+
+       def update(self, other=None, **kwargs):
+               if other is None:
+                       pass
+               elif hasattr(other, 'iteritems'):
+                       for k, v in other.iteritems():
+                               self[k] = v
+               elif hasattr(other, 'keys'):
+                       for k in other.keys():
+                               self[k] = other[k]
+               else:
+                       for k, v in other:
+                               self[k] = v
+               if kwargs:
+                       self.update(kwargs)
+
+class ProtectedDict(MutableMapping):
        """
        given an initial dict, this wraps that dict storing changes in a secondary dict, protecting
        the underlying dict from changes
@@ -52,11 +167,6 @@ class ProtectedDict(UserDict.DictMixin):
                        if k not in self.blacklist and k not in self.new:
                                yield k
 
-
-       def keys(self):
-               return list(self.__iter__())
-
-
        def __contains__(self, key):
                return key in self.new or (key not in self.blacklist and key in self.orig)
 
@@ -66,11 +176,7 @@ class ProtectedDict(UserDict.DictMixin):
                        DeprecationWarning)
                return key in self
 
-       if sys.hexversion >= 0x3000000:
-               keys = __iter__
-               items = iteritems
-
-class LazyLoad(UserDict.DictMixin):
+class LazyLoad(Mapping):
        """
        Lazy loading of values for a dict
        """
@@ -90,16 +196,11 @@ class LazyLoad(UserDict.DictMixin):
                        self.pull = None
                return self.d[key]
 
-
        def __iter__(self):
-               return iter(self.keys())
-
-       def keys(self):
-               if self.pull != None:
+               if self.pull is not None:
                        self.d.update(self.pull())
                        self.pull = None
-               return self.d.keys()
-
+               return iter(self.d)
 
        def has_key(self, key):
                warnings.warn("portage.cache.mappings.LazyLoad.has_key() is "
@@ -116,10 +217,6 @@ class LazyLoad(UserDict.DictMixin):
                        self.pull = None
                return key in self.d
 
-       if sys.hexversion >= 0x3000000:
-               keys = __iter__
-               items = iteritems
-
 _slot_dict_classes = weakref.WeakValueDictionary()
 
 def slot_dict_class(keys, prefix="_val_"):
@@ -235,6 +332,9 @@ def slot_dict_class(keys, prefix="_val_"):
                                return hasattr(self, self._prefix + k)
 
                        def has_key(self, k):
+                               warnings.warn("portage.cache.mappings.SlotDict.has_key()" + \
+                                       " is deprecated, use the in operator instead",
+                                       DeprecationWarning)
                                return k in self
 
                        def pop(self, key, *args):