~coreygoldberg/uci-tests/ucitests-py3-packaging

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# This file is part of the Ubuntu Continuous Integration test tools
#
# Copyright 2014 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License version 3, as published by the
# Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
# SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program.  If not, see <http://www.gnu.org/licenses/>.

import re


class NameMatcher(object):
    """Defines rules to select names.

    The rules are defined by two lists of regular expressions:
    - includes: matching succeeds if one of the regular expression match.
    - excludes: matching fails if one of the regular expression match.

    Matching fails if no rules are given.
    """

    def __init__(self, includes=None, excludes=None):
        self.includes = []
        if includes is not None:
            for inc in includes:
                self.includes.append(re.compile(inc))
        self.excludes = []
        if excludes is not None:
            for exc in excludes:
                self.excludes.append(re.compile(exc))

    def matches(self, name):
        for exc in self.excludes:
            if exc.search(name):
                return False
        for inc in self.includes:
            if inc.search(name):
                return True
        # Not explicitely included
        return False