diff options
author | Brian Harring <ferringb@gentoo.org> | 2005-08-09 07:51:09 +0000 |
---|---|---|
committer | Brian Harring <ferringb@gentoo.org> | 2005-08-09 07:51:09 +0000 |
commit | 98cf91703147b689380e91bd869755363dc7f81d (patch) | |
tree | 749f4b2443b0fb3449bb276593e76291381e008f /portage/restrictions/restrictionSet.py | |
parent | whee. (diff) | |
download | portage-cvs-98cf91703147b689380e91bd869755363dc7f81d.tar.gz portage-cvs-98cf91703147b689380e91bd869755363dc7f81d.tar.bz2 portage-cvs-98cf91703147b689380e91bd869755363dc7f81d.zip |
documentation first off (across the board)
addition of __all__ where I've decided (collapsed and restrictionset at this point)
addition of cmatch method, used for dealing with mutable configuration wrappers (ongoing)
yanked SubStringMatch, since it's covered by ContainmentMatch (fixed that beast too, since first implementation of it sucked).
Diffstat (limited to 'portage/restrictions/restrictionSet.py')
-rw-r--r-- | portage/restrictions/restrictionSet.py | 105 |
1 files changed, 87 insertions, 18 deletions
diff --git a/portage/restrictions/restrictionSet.py b/portage/restrictions/restrictionSet.py index e93bd13..67288d1 100644 --- a/portage/restrictions/restrictionSet.py +++ b/portage/restrictions/restrictionSet.py @@ -1,14 +1,25 @@ # Copyright: 2005 Gentoo Foundation # Author(s): Brian Harring (ferringb@gentoo.org) # License: GPL2 -# $Header: /local/data/ulm/cvs/history/var/cvsroot/gentoo-src/portage/portage/restrictions/Attic/restrictionSet.py,v 1.8 2005/08/03 00:20:55 ferringb Exp $ +# $Header: /local/data/ulm/cvs/history/var/cvsroot/gentoo-src/portage/portage/restrictions/Attic/restrictionSet.py,v 1.9 2005/08/09 07:51:09 ferringb Exp $ + +""" +This module provides classes that can be used to combine arbitrary collections of restrictions in AND, NAND, OR, NOR, XOR, XNOR +style operations. +""" import restriction +from itertools import imap +__all__ = ("AndRestrictionSet", "OrRestrictionSet", "XorRestrictionSet") class RestrictionSet(restriction.base): __slots__ = tuple(["restrictions"] + restriction.base.__slots__) def __init__(self, *restrictions, **kwds): + """Optionally hand in (positionally) restrictions to use as the basis of this restriction + finalize=False, set it to True to notify this instance to internally finalize itself (no way to reverse it yet) + negate=False, controls whether matching results are negated + """ if "finalize" in kwds: finalize = kwds["finalize"] del kwds["finalize"] @@ -26,16 +37,23 @@ class RestrictionSet(restriction.base): self.restrictions = list(restrictions) - def add_restriction(self, NewRestriction, strict=True): - if strict and not isinstance(NewRestriction, restriction.base): - raise TypeError, NewRestriction - - self.restrictions.append(NewRestriction) + def add_restriction(self, *new_restrictions, **kwds): + """add restriction(s) + strict=True, set to false to disable isinstance checks to ensure all restrictions are restriction.base derivatives + """ + if len(new_restrictions) == 0: + raise TypeError("need at least one restriction handed in") + if kwds.get("strict", True): + for r in new_restrictions: + if not isinstance(r, restriction.base): + raise TypeError("instance '%s' isn't a restriction.base, and strict is on" % r) + + self.restrictions.extend(new_restrictions) def finalize(self): self.restrictions = tuple(self.restrictions) - def total_len(self): return sum(map(lambda x: x.total_len(), self.restrictions)) + 1 + def total_len(self): return sum(imap(lambda x: x.total_len(), self.restrictions)) + 1 def __len__(self): return len(self.restrictions) @@ -44,25 +62,46 @@ class RestrictionSet(restriction.base): def __getitem__(self, key): return self.restrictions[key] +def unwind_changes(pkg, pop_count, negate): + while pop_count: + pkg.pop_change() + pop_count-=1 + if negate: + return pkg + return None + class AndRestrictionSet(RestrictionSet): + """Boolean AND grouping of restrictions.""" __slots__ = tuple(RestrictionSet.__slots__) - + def match(self, packagedataInstance): for rest in self.restrictions: if not rest.match(packagedataInstance): return self.negate return not self.negate + def cmatch(self, pkg): + entry_point = pkg.changes_count() + for rest in self.restrictions: + if c.match(pkg) == False: + pkg.rollback_changes(entry_point) + if self.negate: return pkg + return self.negate -# def intersect(self, other): + # for this to be reached, things went well. + if self.negate: + pkg.rollback_changes(entry_point) + # yes, normally it's "not negate", but we no negates status already via the if + return False + return True def __str__(self): - if self.negate: s=" !& " - else: s=" && " - return '( %s )' % s.join(map(str,self.restrictions)) + if self.negate: return "not ( %s )" % " && ".join(imap(str, self.restrictions)) + return "( %s )" % " && ".join(imap(str, self.restrictions)) class OrRestrictionSet(RestrictionSet): + """Boolean OR grouping of restrictions.""" __slots__ = tuple(RestrictionSet.__slots__) def match(self, packagedataInstance): @@ -71,13 +110,27 @@ class OrRestrictionSet(RestrictionSet): return not self.negate return self.negate + def cmatch(self, pkg): + entry_point = pkg.changes_count() + for rest in self.restrictions: + if rest.cmatch(pkg) == True: + if self.negate: + pkg.rollback_changes(entry_point) + return not self.negate + else: + pkg.rollback_changes(entry_point) + + if self.negate: + pkg.rollback_changes(entry_point) + return self.negate + def __str__(self): - if self.negate: s=" !| " - else: s=" || " - return '( %s )' % s.join(map(str,self.restrictions)) + if self.negate: return "not ( %s )" % " || ".join(imap(str, self.restrictions)) + return "( %s )" % " || ".join(imap(str, self.restrictions)) class XorRestrictionSet(RestrictionSet): + """Boolean XOR grouping of restrictions.""" __slots__ = tuple(RestrictionSet.__slots__) def match(self, pkginst): @@ -89,10 +142,26 @@ class XorRestrictionSet(RestrictionSet): armed = True return armed ^ self.negate + def cmatch(self, pkg): + entry_point = None + armed = False + for ret in self.restrictions: + node_entry_point = pkg.changes_count() + if rest.cmatch(pkg): + if armed: + pkg.rollback_changes(entry_point) + return self.negate + armed = True + else: + pkg.rollback_changes(node_entry_point) + + if self.negate and entry_point != None: + pkg.rollback_changes(entry_point) + return armed ^ self.negate + def __str__(self): - if self.negate: s=" !^ " - else: s=" ^^ " - return '( %s )' % s.join(map(str,self.restrictions)) + if self.negate: return "not ( %s )" % " ^^ ".join(imap(str, self.restrictions)) + return "( %s )" % " ^^ ".join(imap(str, self.restrictions)) bases = (AndRestrictionSet, OrRestrictionSet, XorRestrictionSet) |