Implement utility enumeration for policyrep classes.

Requires enum34 (not enum) package for Python < 3.4.
This commit is contained in:
Chris PeBenito 2016-08-25 19:13:06 -04:00
parent 9f8f8f72c1
commit fcadd71b24
3 changed files with 67 additions and 0 deletions

View File

@ -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:

62
setools/policyrep/util.py Normal file
View File

@ -0,0 +1,62 @@
# Copyright 2016, Chris PeBenito <pebenito@ieee.org>
#
# 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
# <http://www.gnu.org/licenses/>.
#
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]

View File

@ -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