1 # Copyright 2014, 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 # 19 20 21 class PolicySymbol(object): 22 23 """This is a base class for all policy objects.""" 24 25 def __init__(self, policy, qpol_symbol): 26 """ 27 Parameters: 28 policy The low-level policy object. 29 qpol_symbol The low-level policy symbol object. 30 """ 31 32 assert qpol_symbol 33 34 self.policy = policy 35 self.qpol_symbol = qpol_symbol 36 37 def __str__(self): 38 return self.qpol_symbol.name(self.policy) 39 40 def __hash__(self): 41 return hash(str(self)) 42 43 def __eq__(self, other): 44 try: 45 return self.qpol_symbol.this == other.qpol_symbol.this 46 except AttributeError: 47 return str(self) == str(other) 48 49 def __ne__(self, other): 50 return not self == other 51 52 def __lt__(self, other): 53 """Comparison used by Python sorting functions.""" 54 return str(self) < str(other) 55 56 def __repr__(self): 57 return "<{0.__class__.__name__}(<qpol_policy_t id={1}>,\"{0}\")>".format( 58 self, id(self.policy)) 59 60 def __deepcopy__(self, memo): 61 # shallow copy as all of the members are immutable 62 cls = self.__class__ 63 newobj = cls.__new__(cls) 64 newobj.policy = self.policy 65 newobj.qpol_symbol = self.qpol_symbol 66 memo[id(self)] = newobj 67 return newobj 68 69 def statement(self): 70 """ 71 A rendering of the policy statement. This should be 72 overridden by subclasses. 73 """ 74 raise NotImplementedError 75