<commit_before>#
# Copyright (c) 2008 rPath, Inc.
#
# This program is distributed under the terms of the Common Public License,
# version 1.0. A copy of this license should have been distributed with this
# source file in a file called LICENSE. If it is not present, the license
# is always available at http://www.rpath.com/permanent/licenses/CPL-1.0.
#
# This program is distributed in the hope that it will be useful, but
# without any warranty; without even the implied warranty of merchantability
# or fitness for a particular purpose. See the Common Public License for
# full details.
#
"""
This module defines trove filters.
"""

import itertools
import re

from conary.deps import arch, deps
from conary import versions

VT_NONE         = 0
VT_LABEL        = 1
VT_BRANCH       = 2
VT_VERSION      = 3
VT_REVISION     = 4
VT_SRC_REVISION = 5
VT_BIN_REVISION = 6


class AbstractFilter(object):
    def match(self, *args, **kwargs):
        raise NotImplementedError
    def __invert__(self):
        return NotFilter(self)
    def __or__(self, filter):
        return OrFilter(self, filter)
    def __and__(self, filter):
        return AndFilter(self, filter)
    __mul__ = __and__
    __add__ = __or__
    __neg__ = __invert__
    def __sub__(self, filter):
        return OrFilter(self, NotFilter(filter))
    def compile(self):
        pass

class AndFilter(AbstractFilter):
    def __init__(self, *filters):
        self.filters = filters
    def match(self, *args, **kwargs):
        for filter in self.filters:
            if not filter.match(*args, **kwargs):
                return False
        return True

class OrFilter(AbstractFilter):
    def __init__(self, *filters):
        self.filters = filters
    def match(self, *args, **kwargs):
        for filter in self.filters:
            if filter.match(*args, **kwargs):
                return True
        return False

class NotFilter(AbstractFilter):
    def __init__(self, filter):
        self.filter = filter
    def match(self, *args, **kwargs):
        return not self.filter.match(*args, **kwargs)

class TroveFilter(AbstractFilter):
    def __init__(self, recipe, name = None, version = None, flavor = None):
        self.compiled = False
        self.macros = recipe.macros
        self.name = self.label = self.branch = self.version = self.flavor = \
                self.revision = None
        if name is not None:
            self._validateRegexp(name, 'name')
            self.name = name
        if version is not None:
            self.version = version
        if flavor is not None:
            self.flavor = deps.parseFlavor(flavor)

    def __eq__(self, filter):
        return self.name == filter.name and \
                self.version == filter.version and \
                self.flavor == filter.flavor

    def _validateRegexp(self, pattern, param):
        try:
            re.compile(pattern)
        except:
            raise RuntimeError("Bad Regexp: '%s' for %s" % (pattern, param))

    def _compilePattern(self, pattern):
        if pattern is not None:
            if not pattern or pattern[0] != '^':
                pattern = '^' + pattern
            if pattern[-1] != '$':
                pattern += '$'<commit_msg>cleaner trovefilter output. make trovefilters work well with sets (CNY-2221)<commit_after>#
# Copyright (c) 2008 rPath, Inc.
#
# This program is distributed under the terms of the Common Public License,
# version 1.0. A copy of this license should have been distributed with this
# source file in a file called LICENSE. If it is not present, the license
# is always available at http://www.rpath.com/permanent/licenses/CPL-1.0.
#
# This program is distributed in the hope that it will be useful, but
# without any warranty; without even the implied warranty of merchantability
# or fitness for a particular purpose. See the Common Public License for
# full details.
#
"""
This module defines trove filters.
"""

import itertools
import re

from conary.deps import arch, deps
from conary import versions

VT_NONE         = 0
VT_LABEL        = 1
VT_BRANCH       = 2
VT_VERSION      = 3
VT_REVISION     = 4
VT_SRC_REVISION = 5
VT_BIN_REVISION = 6


class AbstractFilter(object):
    def match(self, *args, **kwargs):
        raise NotImplementedError
    def __invert__(self):
        return NotFilter(self)
    def __or__(self, filter):
        return OrFilter(self, filter)
    def __and__(self, filter):
        return AndFilter(self, filter)
    def __hash__(self):
        if 'filters' in self.__dict__:
            res = None
            for f in self.filters:
                res = hash((res, hash(f)))
            return res
        else:
            return object.__hash__(self)
    __mul__ = __and__
    __add__ = __or__
    __neg__ = __invert__
    def __sub__(self, filter):
        return OrFilter(self, NotFilter(filter))
    def compile(self):
        pass

class AndFilter(AbstractFilter):
    def __init__(self, *filters):
        self.filters = filters
    def match(self, *args, **kwargs):
        for filter in self.filters:
            if not filter.match(*args, **kwargs):
                return False
        return True

class OrFilter(AbstractFilter):
    def __init__(self, *filters):
        self.filters = filters
    def match(self, *args, **kwargs):
        for filter in self.filters:
            if filter.match(*args, **kwargs):
                return True
        return False

class NotFilter(AbstractFilter):
    def __init__(self, filter):
        self.filter = filter
    def match(self, *args, **kwargs):
        return not self.filter.match(*args, **kwargs)

class TroveFilter(AbstractFilter):
    def __init__(self, recipe, name = None, version = None, flavor = None):
        self.compiled = False
        self.macros = recipe.macros
        self.name = self.label = self.branch = self.version = self.flavor = \
                self.revision = None
        if name is not None:
            self._validateRegexp(name, 'name')
            self.name = name
        if version is not None:
            self.version = version
        if flavor is not None:
            self.flavor = deps.parseFlavor(flavor)

    def __eq__(self, filter):
        return self.name == filter.name and \
                self.version == filter.version and \
                self.flavor == filter.flavor

    def __hash__(self):
        return hash((self.name, self.version, self.flavor))

    def __str__(self):
        name = self.name or ''
        ver = self.version or ''
        if ver:
            ver = '=' + ver
        flv = self.flavor and '[%s]' % self.flavor or ''
        return "<TroveFilter: '%s%s%s'>" % (name, ver, flv)

    def _validateRegexp(self, pattern, param):
        try:
            re.compile(pattern)
        except:
            raise RuntimeError("Bad Regexp: '%s' for %s" % (pattern, param))

    def _compilePattern(self, pattern):
        if pattern is not None:
            if not pattern or pattern[0] != '^':
                pattern = '^' + pattern
            if pattern[-1] != '$':
                pattern += '$'
{'fraction_non_alphanumeric': 0.09719283396811962, 'fraction_numerical': 0.00536041754831429, 'mean_word_length': 2.2900232018561484, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '92171', 'n_tokens_mistral': 2051, 'n_tokens_neox': 1998, 'n_words': 675}