Make PackageIndex use SlotDict for package metadata storage. The set of
authorZac Medico <zmedico@gentoo.org>
Thu, 26 Jun 2008 00:49:10 +0000 (00:49 -0000)
committerZac Medico <zmedico@gentoo.org>
Thu, 26 Jun 2008 00:49:10 +0000 (00:49 -0000)
allowed keys is passed into the PackageIndex constructor (normal dict
instances will be used if the set of keys is not passed in for some
reason). A SlotDict.allowed_keys attribute now provides access to a
frozenset of allowed keys.

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

pym/portage/cache/mappings.py
pym/portage/dbapi/bintree.py
pym/portage/getbinpkg.py

index f35761765b8140b2bd61f98aed1c568c508f67ac..2cddd8147a3ca702cf7eeb139ba035cbc164c0bb 100644 (file)
@@ -125,8 +125,9 @@ def slot_dict_class(keys):
 
                class SlotDict(object):
 
-                       _keys = keys_set
-                       __slots__ = ("__weakref__",) + tuple("_val_" + k for k in _keys)
+                       allowed_keys = keys_set
+                       __slots__ = ("__weakref__",) + \
+                               tuple("_val_" + k for k in allowed_keys)
 
                        def __iter__(self):
                                for k, v in self.iteritems():
@@ -142,7 +143,7 @@ def slot_dict_class(keys):
                                return list(self)
 
                        def iteritems(self):
-                               for k in self._keys:
+                               for k in self.allowed_keys:
                                        try:
                                                yield (k, getattr(self, "_val_" + k))
                                        except AttributeError:
@@ -229,12 +230,12 @@ def slot_dict_class(keys):
                                return c
 
                        def clear(self):
-                               for k in self._keys:
+                               for k in self.allowed_keys:
                                        try:
                                                delattr(self, "_val_" + k)
                                        except AttributeError:
                                                pass
 
                v = SlotDict
-               _slot_dict_classes[keys_set] = v
+               _slot_dict_classes[v.allowed_keys] = v
        return v
index 9b470cf6003c6d3028229a405df1f7c4e072624f..2e0a1a536882f04a111716e99670c039505dd2c4 100644 (file)
@@ -18,7 +18,7 @@ import portage.xpak, portage.getbinpkg
 
 import os, errno, stat
 import re
-from itertools import izip
+from itertools import chain, izip
 
 class bindbapi(fakedbapi):
        _known_keys = frozenset(list(fakedbapi._known_keys) + \
@@ -182,6 +182,16 @@ class binarytree(object):
                                ("repository"    ,   "REPO"),
                        )
 
+                       self._pkgindex_allowed_pkg_keys = set(chain(
+                               self._pkgindex_keys,
+                               self._pkgindex_aux_keys,
+                               self._pkgindex_hashes,
+                               self._pkgindex_default_pkg_data,
+                               self._pkgindex_inherited_keys,
+                               self._pkgindex_default_header_data,
+                               chain(*self._pkgindex_translated_keys)
+                       ))
+
        def move_ent(self, mylist):
                if not self.populated:
                        self.populate()
@@ -827,6 +837,7 @@ class binarytree(object):
 
        def _new_pkgindex(self):
                return portage.getbinpkg.PackageIndex(
+                       allowed_pkg_keys=self._pkgindex_allowed_pkg_keys,
                        default_header_data=self._pkgindex_default_header_data,
                        default_pkg_data=self._pkgindex_default_pkg_data,
                        inherited_keys=self._pkgindex_inherited_keys,
index 75f3a352a4551b4bf2f03924763afdfcbb175184..497194d67d2fa710158925069e2c41daba4ecc20 100644 (file)
@@ -4,6 +4,7 @@
 # $Id$
 
 from portage.output import red, yellow, green
+from portage.cache.mappings import slot_dict_class
 import portage.xpak
 import HTMLParser
 import sys
@@ -680,8 +681,17 @@ def _cmp_cpv(d1, d2):
 
 class PackageIndex(object):
 
-       def __init__(self, default_header_data=None, default_pkg_data=None,
-               inherited_keys=None, translated_keys=None):
+       def __init__(self,
+               allowed_pkg_keys=None,
+               default_header_data=None,
+               default_pkg_data=None,
+               inherited_keys=None,
+               translated_keys=None):
+
+               self._pkg_slot_dict = None
+               if allowed_pkg_keys is not None:
+                       self._pkg_slot_dict = slot_dict_class(allowed_pkg_keys)
+
                self._default_header_data = default_header_data
                self._default_pkg_data = default_pkg_data
                self._inherited_keys = inherited_keys
@@ -696,8 +706,15 @@ class PackageIndex(object):
                self.packages = []
                self.modified = True
 
-       def _readpkgindex(self, pkgfile):
-               d = {}
+       def _readpkgindex(self, pkgfile, pkg_entry=True):
+
+               allowed_keys = None
+               if self._pkg_slot_dict is None or not pkg_entry:
+                       d = {}
+               else:
+                       d = self._pkg_slot_dict()
+                       allowed_keys = d.allowed_keys
+
                for line in pkgfile:
                        line = line.rstrip("\n")
                        if not line:
@@ -708,7 +725,11 @@ class PackageIndex(object):
                        k, v = line
                        if v:
                                v = v[1:]
-                       d[self._read_translation_map.get(k, k)] = v
+                       k = self._read_translation_map.get(k, k)
+                       if allowed_keys is not None and \
+                               k not in allowed_keys:
+                               continue
+                       d[k] = v
                return d
 
        def _writepkgindex(self, pkgfile, items):
@@ -722,7 +743,7 @@ class PackageIndex(object):
                self.readBody(pkgfile)
 
        def readHeader(self, pkgfile):
-               self.header.update(self._readpkgindex(pkgfile))
+               self.header.update(self._readpkgindex(pkgfile, pkg_entry=False))
 
        def readBody(self, pkgfile):
                while True: