summaryrefslogtreecommitdiffstats
path: root/pym/portage/cache/mappings.py
diff options
context:
space:
mode:
authorZac Medico <zmedico@gentoo.org>2009-02-19 02:52:36 +0000
committerZac Medico <zmedico@gentoo.org>2009-02-19 02:52:36 +0000
commitf522aabc3ddc4a55513c75a39d0b11378ba8f41b (patch)
treecbbe2a9b592029a6e62931cd0b367777acbaa4a3 /pym/portage/cache/mappings.py
parent0903870129174bb57c0ba33959373eb82f261054 (diff)
downloadportage-f522aabc3ddc4a55513c75a39d0b11378ba8f41b.tar.gz
portage-f522aabc3ddc4a55513c75a39d0b11378ba8f41b.tar.bz2
portage-f522aabc3ddc4a55513c75a39d0b11378ba8f41b.zip
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, implement Mapping and MutableMapping classes as substitutes for UserDict.DictMixin so that code converted via 2to3 will run. svn path=/main/trunk/; revision=12628
Diffstat (limited to 'pym/portage/cache/mappings.py')
-rw-r--r--pym/portage/cache/mappings.py146
1 files changed, 123 insertions, 23 deletions
diff --git a/pym/portage/cache/mappings.py b/pym/portage/cache/mappings.py
index 010eb7f8e..74ed5fa5e 100644
--- a/pym/portage/cache/mappings.py
+++ b/pym/portage/cache/mappings.py
@@ -4,11 +4,126 @@
# $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):