Home | History | Annotate | Download | only in setools
      1 # Derived from portconquery.py
      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 #
     19 import logging
     20 
     21 from .mixins import MatchContext
     22 from .query import PolicyQuery
     23 
     24 
     25 class PcideviceconQuery(MatchContext, PolicyQuery):
     26 
     27     """
     28     Pcidevicecon context query.
     29 
     30     Parameter:
     31     policy          The policy to query.
     32 
     33     Keyword Parameters/Class attributes:
     34     device          A single PCI device ID.
     35 
     36     user            The criteria to match the context's user.
     37     user_regex      If true, regular expression matching
     38                     will be used on the user.
     39 
     40     role            The criteria to match the context's role.
     41     role_regex      If true, regular expression matching
     42                     will be used on the role.
     43 
     44     type_           The criteria to match the context's type.
     45     type_regex      If true, regular expression matching
     46                     will be used on the type.
     47 
     48     range_          The criteria to match the context's range.
     49     range_subset    If true, the criteria will match if it is a subset
     50                     of the context's range.
     51     range_overlap   If true, the criteria will match if it overlaps
     52                     any of the context's range.
     53     range_superset  If true, the criteria will match if it is a superset
     54                     of the context's range.
     55     range_proper    If true, use proper superset/subset operations.
     56                     No effect if not using set operations.
     57     """
     58 
     59     _device = None
     60 
     61     @property
     62     def device(self):
     63         return self._device
     64 
     65     @device.setter
     66     def device(self, value):
     67         if value:
     68             if value < 1:
     69                 raise ValueError("PCI device ID must be positive: {0}".format(value))
     70 
     71             self._device = value
     72         else:
     73             self._device = None
     74 
     75     def __init__(self, policy, **kwargs):
     76         super(PcideviceconQuery, self).__init__(policy, **kwargs)
     77         self.log = logging.getLogger(__name__)
     78 
     79     def results(self):
     80         """Generator which yields all matching pcidevicecons."""
     81         self.log.info("Generating results from {0.policy}".format(self))
     82         self.log.debug("Device ID: {0.device!r}".format(self))
     83         self._match_context_debug(self.log)
     84 
     85         for pcidevicecon in self.policy.pcidevicecons():
     86 
     87             if self.device and self.device != pcidevicecon.device:
     88                 continue
     89 
     90             if not self._match_context(pcidevicecon.context):
     91                 continue
     92 
     93             yield pcidevicecon
     94