Implement __contains__, pop() and clear() for Package.metadata attributes.
authorZac Medico <zmedico@gentoo.org>
Wed, 25 Jun 2008 08:42:02 +0000 (08:42 -0000)
committerZac Medico <zmedico@gentoo.org>
Wed, 25 Jun 2008 08:42:02 +0000 (08:42 -0000)
svn path=/main/trunk/; revision=10780

pym/_emerge/__init__.py

index 8ce5d1969a7f4fe3a38e7cc787b39b9c8cdcd016..7b6dc6e4086c65d2e6672f7041b1545ae60735ac 100644 (file)
@@ -1446,6 +1446,29 @@ class _PackageMetadataWrapper(object):
                except KeyError:
                        return default
 
+       def __contains__(self, k):
+               return hasattr(self, "_val_" + k)
+
+       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 clear(self):
+               for k in self._keys:
+                       try:
+                               delattr(self, "_val_" + k)
+                       except AttributError:
+                               pass
+
        def _set_inherited(self, k, v):
                if isinstance(v, basestring):
                        v = frozenset(v.split())