From fcadd71b2403856df158f387500cd33c501fb97c Mon Sep 17 00:00:00 2001 From: Chris PeBenito Date: Thu, 25 Aug 2016 19:13:06 -0400 Subject: [PATCH] Implement utility enumeration for policyrep classes. Requires enum34 (not enum) package for Python < 3.4. --- README.md | 1 + setools/policyrep/util.py | 62 +++++++++++++++++++++++++++++++++++++++ tox.ini | 4 +++ 3 files changed, 67 insertions(+) create mode 100644 setools/policyrep/util.py diff --git a/README.md b/README.md index 2c450db..b029a2f 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ To run SETools command line tools, the following packages are required: * Python 2.7 or 3.3+ * NetworkX 1.8+ * setuptools +* enum34 (on Python 2.7 and 3.3 only) * libselinux Python bindings (optional but recommended) To run SETools graphical tools, the following packages are also required: diff --git a/setools/policyrep/util.py b/setools/policyrep/util.py new file mode 100644 index 0000000..7eab645 --- /dev/null +++ b/setools/policyrep/util.py @@ -0,0 +1,62 @@ +# Copyright 2016, Chris PeBenito +# +# This file is part of SETools. +# +# SETools is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as +# published by the Free Software Foundation, either version 2.1 of +# the License, or (at your option) any later version. +# +# SETools is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with SETools. If not, see +# . +# +import warnings + +from enum import Enum + + +class PolicyEnum(Enum): + + """ + Base class for policy enumerations. + + Standard Enum behavior except for returning + the enum name for the default string representation + and basic string format. + """ + + def __str__(self): + return self.name + + def __format__(self, spec): + if not spec: + return self.name + else: + return super(PolicyEnum, self).__format__(spec) + + def __eq__(self, other): + if isinstance(other, str): + warnings.warn("{} has changed to an enumeration. In the future, direct string " + "comparisons will be deprecated.".format(self.__class__.__name__), + PendingDeprecationWarning) + return self.name == other + else: + return super(PolicyEnum, self).__eq__(other) + + def __hash__(self): + return hash(self.name) + + @classmethod + def lookup(cls, value): + """Look up an enumeration by name or value.""" + + try: + return cls(value) + except ValueError: + return cls[value] diff --git a/tox.ini b/tox.ini index 19668e7..eed56ea 100644 --- a/tox.ini +++ b/tox.ini @@ -21,6 +21,7 @@ commands = pep8 --version basepython = python3.3 deps = networkx==1.9 coverage + enum34 commands = coverage --version coverage erase coverage run setup.py test @@ -31,6 +32,7 @@ basepython = python3.3 deps = pylint networkx==1.9 mock + enum34 commands = {envpython} setup.py build_ext -i pylint --version pylint -E --rcfile .pylintrc setools tests seinfo seinfoflow sedta sesearch sediff @@ -40,5 +42,7 @@ commands = {envpython} setup.py build_ext -i [testenv] deps = networkx==1.9 py27: mock + py27: enum34 + py33: enum34 commands = {envpython} setup.py test recreate = True