From: Zac Medico Date: Thu, 26 Jun 2008 00:49:10 +0000 (-0000) Subject: Make PackageIndex use SlotDict for package metadata storage. The set of X-Git-Tag: v2.2_rc2~324 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=9a58611323e313a7a5d48fa65a316a3ffedc3954;p=portage.git Make PackageIndex use SlotDict for package metadata storage. The set of 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 --- diff --git a/pym/portage/cache/mappings.py b/pym/portage/cache/mappings.py index f35761765..2cddd8147 100644 --- a/pym/portage/cache/mappings.py +++ b/pym/portage/cache/mappings.py @@ -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 diff --git a/pym/portage/dbapi/bintree.py b/pym/portage/dbapi/bintree.py index 9b470cf60..2e0a1a536 100644 --- a/pym/portage/dbapi/bintree.py +++ b/pym/portage/dbapi/bintree.py @@ -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, diff --git a/pym/portage/getbinpkg.py b/pym/portage/getbinpkg.py index 75f3a352a..497194d67 100644 --- a/pym/portage/getbinpkg.py +++ b/pym/portage/getbinpkg.py @@ -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: