diff options
author | Brian Harring <ferringb@gentoo.org> | 2005-08-03 00:20:55 +0000 |
---|---|---|
committer | Brian Harring <ferringb@gentoo.org> | 2005-08-03 00:20:55 +0000 |
commit | ef066f99c74c9162e2d9ab84a910790b0ece181b (patch) | |
tree | e662a3b95136904a0fbe71442b4a9ea1a040b0ed | |
parent | reworked the visibility filter, now it accepts only one instance of a restric... (diff) | |
download | portage-cvs-ef066f99c74c9162e2d9ab84a910790b0ece181b.tar.gz portage-cvs-ef066f99c74c9162e2d9ab84a910790b0ece181b.tar.bz2 portage-cvs-ef066f99c74c9162e2d9ab84a910790b0ece181b.zip |
collapsed now is fully functional (and fast, too).
added __str__ all over the place, the output looks rather sql like
Aside frmo that, added ContainmentMatch, which is effecitively an 'in' op (so SubStringMatch is going to die soon)
-rw-r--r-- | portage/restrictions/collapsed.py | 95 | ||||
-rw-r--r-- | portage/restrictions/restriction.py | 102 | ||||
-rw-r--r-- | portage/restrictions/restrictionSet.py | 37 |
3 files changed, 170 insertions, 64 deletions
diff --git a/portage/restrictions/collapsed.py b/portage/restrictions/collapsed.py index ae60426..a8c9388 100644 --- a/portage/restrictions/collapsed.py +++ b/portage/restrictions/collapsed.py @@ -1,62 +1,83 @@ # 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/collapsed.py,v 1.1 2005/07/27 02:26:49 ferringb Exp $ +# $Header: /local/data/ulm/cvs/history/var/cvsroot/gentoo-src/portage/portage/restrictions/collapsed.py,v 1.2 2005/08/03 00:20:55 ferringb Exp $ -from restriction import base +from restriction import base, AlwaysTrue from inspect import isroutine +from restrictionSet import bases, OrRestrictionSet +from portage.util.inheritance import check_for_base class DictBased(base): - __slots__ = tuple(["restricts_dict", "get_key", "get_atom_key"] + base.__slots__) - def __init__(self, restriction_items, get_key, get_key_from_atom, *args, **kwargs) + __slots__ = tuple(["restricts_dict", "get_pkg_key", "get_atom_key"] + base.__slots__) + + def __init__(self, restriction_items, get_key_from_package, get_key_from_atom, *args, **kwargs): """restriction_items is a source of restriction keys and remaining restriction (if none, set it to None) get_key is a function to get the key from a pkg instance""" - if not isroutine(get_key): - raise TypeError(get_key) + if not isroutine(get_key_from_package): + raise TypeError(get_key_from_package) + + super(DictBased, self).__init__(*args, **kwargs) + self.restricts_dict = {} + for r in restriction_items: + key, remaining = get_key_from_atom(r) + if len(remaining) == 0: + remaining = AlwaysTrue + else: + if len(remaining) == 1 and (isinstance(remaining, list) or isinstance(remaining, tuple)): + remaining = remaining[0] + if not isinstance(remaining, base): + b = check_for_base(r, bases) + if b == None: + raise KeyError("unable to convert '%s', remaining '%s' isn't of a known base" % (str(r), str(remaining))) + remaining = b(*remaining) + + if key in self.restricts_dict: + self.restricts_dict[key].add_restriction(remaining) + else: + self.restricts_dict[key] = OrRestrictionSet(remaining) - super(LookupBase, self).__init__(*args, **kwargs) - restricts_dict = {} - for r in restrictions: - key, remaining = chunk_it - restricts_dict[key] = remaining - self.get_key, self.get_atom_key = get_key, get_key_from_atom + self.get_pkg_key, self.get_atom_key = get_key_from_package, get_key_from_atom def match(self, pkginst): try: - key = self.get_key(pkginst) + key = self.get_pkg_key(pkginst) except (TypeError, AttributeError): return self.negate - remaining = self.restricts_dict.get(key, False) - if remaining == False: + if key not in self.restricts_dict: return self.negate - elif remaining == None: - return not self.negate + + remaining = self.restricts_dict[key] return remaining.match(pkginst) ^ self.negate + def __contains__(self, restriction): - if isinstance(key, base): - key = get_atom_key(restriction): + if isinstance(restriction, base): + key, r = self.get_atom_key(restriction) if key != None and key in self.restricts_dict: return True return False - def __getitem__(self, key, default=None): - if isinstance(key, base): - key = get_atom_key(restriction): - if key == None: return default - return self.restricts_dict.get(key, default) - - def __setitem__(self, key, val): - if isinstance(key, base): - key = get_atom_key(restriction): - if key == None: - raise KeyError("either passed in, or converted val became None, invalid as key") - self.restricts_dict[key] = val - - def __delitem__(self, key): - if isinstance(key, base): - key = get_atom_key(restriction): - if key != None and key in self.restricts_dict: - del self.restricts_dict[key] + +# def __getitem__(self, restriction, default=None): +# if isinstance(restriction, base): +# key, r = self.get_atom_key(restriction) +# if key == None: return default +# return self.restricts_dict.get(key, default) +# +# +# def __setitem__(self, restriction, val): +# if isinstance(restriction, base): +# key, r = self.get_atom_key(restriction) +# if key == None: +# raise KeyError("either passed in, or converted val became None, invalid as key") +# self.restricts_dict[key] = val +# +# +# def __delitem__(self, restriction): +# if isinstance(restriction, base): +# key = self.get_atom_key(restriction) +# if key != None and key in self.restricts_dict: +# del self.restricts_dict[key] diff --git a/portage/restrictions/restriction.py b/portage/restrictions/restriction.py index 0ae4743..c155181 100644 --- a/portage/restrictions/restriction.py +++ b/portage/restrictions/restriction.py @@ -1,7 +1,7 @@ # 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/restriction.py,v 1.6 2005/07/28 23:41:48 ferringb Exp $ +# $Header: /local/data/ulm/cvs/history/var/cvsroot/gentoo-src/portage/portage/restrictions/restriction.py,v 1.7 2005/08/03 00:20:55 ferringb Exp $ import re, logging @@ -29,10 +29,15 @@ class base(object): def intersect(self, other): return None + def __len__(self): + return 1 + + total_len = __len__ + class AlwaysBoolMatch(base): __slots__ = base.__slots__ - def match(self, *a, **kw): - return self.negate + def match(self, *a, **kw): return self.negate + def __str__(self): return "always '%s'" % self.negate AlwaysFalse = AlwaysBoolMatch(False) AlwaysTrue = AlwaysBoolMatch(True) @@ -62,16 +67,21 @@ class StrRegexMatch(StrMatch): self.flags = flags self.compiled_re = re.compile(regex, flags) - def match(self, value): return (self.compiled_re.match(str(value)) != None) ^ self.negate - def intersect(self, other): if self.regex == other.regex and self.negate == other.negate and self.flags == other.flags: return self return None + def __eq__(self, other): + return self.regex == other.regex and self.negate == other.negate and self.flags == other.flags + + def __str__(self): + if self.negate: return "not like %s" % self.regex + return "like %s" % self.regex + class StrExactMatch(StrMatch): __slots__ = tuple(["exact", "flags"] + StrMatch.__slots__) @@ -85,12 +95,10 @@ class StrExactMatch(StrMatch): self.flags = 0 self.exact = str(exact) - def match(self, value): if self.flags & re.I: return (self.exact == str(value).lower()) ^ self.negate else: return (self.exact == str(value)) ^ self.negate - def intersect(self, other): s1, s2 = self.exact, other.exact if other.flags and not self.flags: @@ -103,6 +111,13 @@ class StrExactMatch(StrMatch): return self return None + def __eq__(self, other): + return self.exact == other.exact and self.negate == other.negate and self.flags == other.flags + + def __str__(self): + if self.negate: return "!= "+self.exact + return "== "+self.exact + class StrSubstringMatch(StrMatch): __slots__ = tuple(["substr"] + StrMatch.__slots__) @@ -116,13 +131,11 @@ class StrSubstringMatch(StrMatch): self.flags = 0 self.substr = str(substr) - def match(self, value): if self.flags & re.I: value = str(value).lower() else: value = str(value) return (value.find(self.substr) != -1) ^ self.negate - def intersect(self, other): if self.negate == other.negate: if self.substr == other.substr and self.flags == other.flags: @@ -140,6 +153,9 @@ class StrSubstringMatch(StrMatch): return other return None + def __eq__(self, other): + return self.substr == other.substr and self.negate == other.negate and self.flags == other.flags + class StrGlobMatch(StrMatch): __slots__ = tuple(["glob"] + StrMatch.__slots__) @@ -152,13 +168,11 @@ class StrGlobMatch(StrMatch): self.flags = 0 self.glob = str(glob) - def match(self, value): value = str(value) if self.flags & re.I: value = value.lower() return value.startswith(self.glob) ^ self.negate - def intersect(self, other): if self.match(other.glob): if self.negate == other.negate: @@ -168,42 +182,88 @@ class StrGlobMatch(StrMatch): return self return None + def __eq__(self, other): + return self.glob == other.glob and self.negate == other.negate and self.flags == other.flags + + def __str__(self): + if self.negate: return "not "+self.glob+"*" + return self.glob+"*" + class PackageRestriction(base): """cpv data restriction. Inherit for anything that's more then cpv mangling please""" - __slots__ = tuple(["attr", "strmatch"] + base.__slots__) + __slots__ = tuple(["attr", "restriction"] + base.__slots__) - def __init__(self, attr, StrMatchInstance, **kwds): + def __init__(self, attr, restriction, **kwds): super(PackageRestriction, self).__init__(**kwds) self.attr = attr.split(".") - self.strmatch = StrMatchInstance + if not isinstance(restriction, base): + raise TypeError("restriction must be of a restriction type") + self.restriction = restriction def match(self, packageinstance): try: o = packageinstance for x in self.attr: o = getattr(o, x) - return self.strmatch.match(o) ^ self.negate + return self.restriction.match(o) ^ self.negate except AttributeError,ae: logging.debug("failed getting attribute %s from %s, exception %s" % \ (".".join(self.attr), str(packageinstance), str(ae))) return self.negate + def __getitem__(self, key): + try: + g = self.restriction[key] + except TypeError: + if key == 0: + return self.restriction + raise IndexError("index out of range") + + def total_len(self): + return len(self.restriction) + 1 def intersect(self, other): if self.negate != other.negate or self.attr != other.attr: return None - if isinstance(self.strmatch, other.strmatch.__class__): - s = self.strmatch.intersect(other.strmatch) - elif isinstance(other.strmatch, self.strmatch.__class__): - s = other.strmatch.intersect(self.strmatch) + if isinstance(self.restriction, other.restriction.__class__): + s = self.restriction.intersect(other.restriction) + elif isinstance(other.restriction, self.restriction.__class__): + s = other.restriction.intersect(self.restriction) else: return None if s == None: return None - if s == self.strmatch: return self - elif s == other.strmatch: return other + if s == self.restriction: return self + elif s == other.restriction: return other # this can probably bite us in the ass self or other is a derivative, and the other isn't. return self.__class__(self.attr, s) + + def __eq__(self, other): + return self.negate == self.negate and self.attr == other.attr and self.restriction == other.restriction + + def __str__(self): + s='.'.join(self.attr)+" " + if self.negate: s += "not " + return s + str(self.restriction) + + +class ContainmentMatch(base): + """used for an 'in' style operation, 'x86' in ['x86','~x86'] for example""" + __slots__ = tuple(["vals"] + base.__slots__) + + def __init__(self, vals, **kwds): + """vals must support a contaiment test""" + super(ContainmentMatch, self).__init__(**kwds) + self.vals = vals + + def match(self, val): + return (val in self.vals) ^ self.negate + + def __str__(self): + if self.negate: s="not in [%s]" + else: s="in [%s]" + return s % ', '.join(map(str, self.vals)) + diff --git a/portage/restrictions/restrictionSet.py b/portage/restrictions/restrictionSet.py index bf5a370..e93bd13 100644 --- a/portage/restrictions/restrictionSet.py +++ b/portage/restrictions/restrictionSet.py @@ -1,7 +1,7 @@ # 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.7 2005/07/27 02:34:30 ferringb Exp $ +# $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 $ import restriction @@ -10,7 +10,7 @@ class RestrictionSet(restriction.base): def __init__(self, *restrictions, **kwds): if "finalize" in kwds: - finalize = kdws["finalize"] + finalize = kwds["finalize"] del kwds["finalize"] else: finalize = False @@ -32,10 +32,17 @@ class RestrictionSet(restriction.base): self.restrictions.append(NewRestriction) - def finalize(self): self.restrictions = tuple(self.restrictions) + def total_len(self): return sum(map(lambda x: x.total_len(), self.restrictions)) + 1 + + def __len__(self): return len(self.restrictions) + + def __iter__(self): return iter(self.restrictions) + + def __getitem__(self, key): + return self.restrictions[key] class AndRestrictionSet(RestrictionSet): __slots__ = tuple(RestrictionSet.__slots__) @@ -48,7 +55,11 @@ class AndRestrictionSet(RestrictionSet): # def intersect(self, other): - + + def __str__(self): + if self.negate: s=" !& " + else: s=" && " + return '( %s )' % s.join(map(str,self.restrictions)) class OrRestrictionSet(RestrictionSet): @@ -57,8 +68,14 @@ class OrRestrictionSet(RestrictionSet): def match(self, packagedataInstance): for rest in self.restrictions: if rest.match(packagedataInstance): - return self.negate - return not self.negate + return not self.negate + return self.negate + + def __str__(self): + if self.negate: s=" !| " + else: s=" || " + return '( %s )' % s.join(map(str,self.restrictions)) + class XorRestrictionSet(RestrictionSet): __slots__ = tuple(RestrictionSet.__slots__) @@ -71,3 +88,11 @@ class XorRestrictionSet(RestrictionSet): return self.negate armed = True return armed ^ self.negate + + def __str__(self): + if self.negate: s=" !^ " + else: s=" ^^ " + return '( %s )' % s.join(map(str,self.restrictions)) + + +bases = (AndRestrictionSet, OrRestrictionSet, XorRestrictionSet) |