1# Copyright 2016, 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#
19from . import exception
20from .symbol import PolicySymbol
21from .qpol import qpol_typebounds_t
22from .typeattr import type_factory
23
24
25def bounds_factory(policy, sym):
26    """Factory for creating bounds statement objects."""
27
28    if isinstance(sym, qpol_typebounds_t):
29        return Bounds(policy, sym)
30    else:
31        raise TypeError("typebounds rules cannot be looked up.")
32
33
34def validate_ruletype(t):
35    """Validate *bounds rule types."""
36    if t not in ["typebounds"]:
37        raise exception.InvalidBoundsType("{0} is not a valid *bounds  rule type.".format(t))
38
39    return t
40
41
42class Bounds(PolicySymbol):
43
44    """A typebounds statement."""
45
46    def __str__(self):
47        return "{0.ruletype} {0.parent} {0.child};".format(self)
48
49    def __hash__(self):
50        return hash("{0.ruletype}|{0.child};".format(self))
51
52    ruletype = "typebounds"
53
54    @property
55    def parent(self):
56        return type_factory(self.policy, self.qpol_symbol.parent_name(self.policy))
57
58    @property
59    def child(self):
60        return type_factory(self.policy, self.qpol_symbol.child_name(self.policy))
61