1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Copyright 2014, Tresys Technology, LLC
2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#
3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# This file is part of SETools.
4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#
5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# SETools is free software: you can redistribute it and/or modify
6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# it under the terms of the GNU Lesser General Public License as
7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# published by the Free Software Foundation, either version 2.1 of
8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# the License, or (at your option) any later version.
9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#
10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# SETools is distributed in the hope that it will be useful,
11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# but WITHOUT ANY WARRANTY; without even the implied warranty of
12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# GNU Lesser General Public License for more details.
14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#
15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# You should have received a copy of the GNU Lesser General Public
16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# License along with SETools.  If not, see
17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# <http://www.gnu.org/licenses/>.
18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#
19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass PolicySymbol(object):
22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    """This is a base class for all policy objects."""
24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __init__(self, policy, qpol_symbol):
26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """
27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        Parameters:
28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        policy        The low-level policy object.
29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        qpol_symbol   The low-level policy symbol object.
30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """
31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        assert qpol_symbol
33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.policy = policy
35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.qpol_symbol = qpol_symbol
36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __str__(self):
38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return self.qpol_symbol.name(self.policy)
39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __hash__(self):
41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return hash(str(self))
42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
43edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __eq__(self, other):
44edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
45edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return self.qpol_symbol.this == other.qpol_symbol.this
46edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except AttributeError:
47edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return str(self) == str(other)
48edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
49edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __ne__(self, other):
50edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return not self == other
51edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
52edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __lt__(self, other):
53edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """Comparison used by Python sorting functions."""
54edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return str(self) < str(other)
55edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
56edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __repr__(self):
57edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return "<{0.__class__.__name__}(<qpol_policy_t id={1}>,\"{0}\")>".format(
58edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self, id(self.policy))
59edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
60edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __deepcopy__(self, memo):
61edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # shallow copy as all of the members are immutable
62edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cls = self.__class__
63edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        newobj = cls.__new__(cls)
64edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        newobj.policy = self.policy
65edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        newobj.qpol_symbol = self.qpol_symbol
66edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        memo[id(self)] = newobj
67edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return newobj
68edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
69edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def statement(self):
70edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """
71edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        A rendering of the policy statement.  This should be
72edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        overridden by subclasses.
73edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        """
74edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        raise NotImplementedError
75