From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1QelFF-00028G-Rg for garchives@archives.gentoo.org; Thu, 07 Jul 2011 09:51:42 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 0C2A421C062; Thu, 7 Jul 2011 09:51:33 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id C083221C04B for ; Thu, 7 Jul 2011 09:51:33 +0000 (UTC) Received: from pelican.gentoo.org (unknown [66.219.59.40]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 359EA2AC078 for ; Thu, 7 Jul 2011 09:51:33 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by pelican.gentoo.org (Postfix) with ESMTP id 8F4278003D for ; Thu, 7 Jul 2011 09:51:32 +0000 (UTC) From: "Michał Górny" To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Michał Górny" Message-ID: <5ef78b31ae1493ed1ccffb2cb6a6640f8d051bbf.mgorny@gentoo> Subject: [gentoo-commits] proj/gentoopm:master commit in: gentoopm/basepm/ X-VCS-Repository: proj/gentoopm X-VCS-Files: gentoopm/basepm/pkg.py X-VCS-Directories: gentoopm/basepm/ X-VCS-Committer: mgorny X-VCS-Committer-Name: Michał Górny X-VCS-Revision: 5ef78b31ae1493ed1ccffb2cb6a6640f8d051bbf Date: Thu, 7 Jul 2011 09:51:32 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: X-Archives-Hash: 33c2b1f9934b8eefc92c358498a6698b commit: 5ef78b31ae1493ed1ccffb2cb6a6640f8d051bbf Author: Micha=C5=82 G=C3=B3rny gentoo org> AuthorDate: Thu Jul 7 08:43:02 2011 +0000 Commit: Micha=C5=82 G=C3=B3rny gentoo org> CommitDate: Thu Jul 7 08:43:02 2011 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/gentoopm.git;= a=3Dcommit;h=3D5ef78b31 Update filtering description/API. --- gentoopm/basepm/pkg.py | 43 +++++++++++++++++++++---------------------= - 1 files changed, 21 insertions(+), 22 deletions(-) diff --git a/gentoopm/basepm/pkg.py b/gentoopm/basepm/pkg.py index 4c71c62..8f94833 100644 --- a/gentoopm/basepm/pkg.py +++ b/gentoopm/basepm/pkg.py @@ -17,21 +17,19 @@ class PMPackageSet(ABCObject): =20 def filter(self, *args, **kwargs): """ - Filter the packages based on keys passed as arguments. Positional - arguments refer to keys by their level (with first arg being the - top-level key), None means match-all. Keyword arguments refer to keys - by their names. + Filter the packages based on arguments. Return a PMFilteredPackageSet + evaluating to a number of PMPackages. =20 - If an argument doesn't match any key (i.e. too many args are passed), - a KeyError or IndexError will be raised. If the same key is referred - through positional and keyword arguments, a TypeError will be raised. + The positional arguments can provide a number of PMPackageMatchers (se= e + gentoopm.basepm.filter) and/or an atom string. The keyword arguments + match metadata keys using '=3D=3D' comparison with passed values (obje= cts). =20 - The filtering will result in an iterable of PMKeyedPackageDicts - or PMPackages, depending on whether the filtering criteria are able - to uniquely identify packages. + Multiple filters will be AND-ed together. Same applies for .filter() + called multiple times. You should, however, avoid passing multiple + atoms as it is not supported by all PMs. =20 - The '=3D=3D' operator is used to match packages. To extend matching, y= ou - can provide a class with __eq__() redefined as an argument. + This function can raise KeyError when a keyword argument does referenc= e + an incorrect metadata key. """ =20 return PMFilteredPackageSet(iter(self), args, kwargs) @@ -70,8 +68,8 @@ class PMFilteredPackageSet(PMPackageSet): =20 def __iter__(self): for el in self._iter: - for x in el.filter(*self._args, **self._kwargs): - yield x + if el._matches(*self._args, **self._kwargs): + yield el =20 class PMPackage(ABCObject): """ @@ -79,16 +77,17 @@ class PMPackage(ABCObject): in the package tree. """ =20 - def filter(self, **kwargs): + def _matches(self, *args, **kwargs): """ - Filter packages on metadata. This is mostly to extend superclass - .filter() method. + Check whether the package matches passed filters. Please note that thi= s + method may not be called at all if PM is capable of a more efficient + filtering. =20 - If args are non-empty, raises an IndexError (unused args). If kwargs - contains keys not matching metadata, raises a KeyError. Otherwise, - returns an iterator -- either over the package itself or an empty one. + If kwargs reference incorrect metadata keys, a KeyError will be raised= . """ =20 + # XXX: apply filters + for k, m in kwargs.items(): try: v =3D self.metadata[k] @@ -96,9 +95,9 @@ class PMPackage(ABCObject): raise KeyError('Unmatched keyword argument: %s' % k) else: if not m =3D=3D v: - return + return False =20 - yield self + return True =20 @abstractproperty def id(self):