1# Copyright 2014-2015, Tresys Technology, LLC
2#
3# This file is part of SETools.
4#
5# SETools is free software: you can redistribute it and/or modify
6# it under the terms of the GNU Lesser General Public License as
7# published by the Free Software Foundation, either version 2.1 of
8# the License, or (at your option) any later version.
9#
10# SETools is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU Lesser General Public License for more details.
14#
15# You should have received a copy of the GNU Lesser General Public
16# License along with SETools.  If not, see
17# <http://www.gnu.org/licenses/>.
18#
19import logging
20import re
21
22from .mixins import MatchName, MatchPermission
23from .query import PolicyQuery
24
25
26class CommonQuery(MatchPermission, MatchName, PolicyQuery):
27
28    """
29    Query common permission sets.
30
31    Parameter:
32    policy       The policy to query.
33
34    Keyword Parameters/Class attributes:
35    name         The name of the common to match.
36    name_regex   If true, regular expression matching will
37                 be used for matching the name.
38    perms        The permissions to match.
39    perms_equal  If true, only commons with permission sets
40                 that are equal to the criteria will
41                 match.  Otherwise, any intersection
42                 will match.
43    perms_regex  If true, regular expression matching will be used
44                 on the permission names instead of set logic.
45    """
46
47    def __init__(self, policy, **kwargs):
48        super(CommonQuery, self).__init__(policy, **kwargs)
49        self.log = logging.getLogger(__name__)
50
51    def results(self):
52        """Generator which yields all matching commons."""
53        self.log.info("Generating common results from {0.policy}".format(self))
54        self._match_name_debug(self.log)
55        self._match_perms_debug(self.log)
56
57        for com in self.policy.commons():
58            if not self._match_name(com):
59                continue
60
61            if not self._match_perms(com):
62                continue
63
64            yield com
65