mirror of
https://github.com/SELinuxProject/setools
synced 2025-05-13 21:58:06 +00:00
Implement utility enumeration for policyrep classes.
Requires enum34 (not enum) package for Python < 3.4.
This commit is contained in:
parent
9f8f8f72c1
commit
fcadd71b24
@ -17,6 +17,7 @@ To run SETools command line tools, the following packages are required:
|
|||||||
* Python 2.7 or 3.3+
|
* Python 2.7 or 3.3+
|
||||||
* NetworkX 1.8+
|
* NetworkX 1.8+
|
||||||
* setuptools
|
* setuptools
|
||||||
|
* enum34 (on Python 2.7 and 3.3 only)
|
||||||
* libselinux Python bindings (optional but recommended)
|
* libselinux Python bindings (optional but recommended)
|
||||||
|
|
||||||
To run SETools graphical tools, the following packages are also required:
|
To run SETools graphical tools, the following packages are also required:
|
||||||
|
62
setools/policyrep/util.py
Normal file
62
setools/policyrep/util.py
Normal 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]
|
4
tox.ini
4
tox.ini
@ -21,6 +21,7 @@ commands = pep8 --version
|
|||||||
basepython = python3.3
|
basepython = python3.3
|
||||||
deps = networkx==1.9
|
deps = networkx==1.9
|
||||||
coverage
|
coverage
|
||||||
|
enum34
|
||||||
commands = coverage --version
|
commands = coverage --version
|
||||||
coverage erase
|
coverage erase
|
||||||
coverage run setup.py test
|
coverage run setup.py test
|
||||||
@ -31,6 +32,7 @@ basepython = python3.3
|
|||||||
deps = pylint
|
deps = pylint
|
||||||
networkx==1.9
|
networkx==1.9
|
||||||
mock
|
mock
|
||||||
|
enum34
|
||||||
commands = {envpython} setup.py build_ext -i
|
commands = {envpython} setup.py build_ext -i
|
||||||
pylint --version
|
pylint --version
|
||||||
pylint -E --rcfile .pylintrc setools tests seinfo seinfoflow sedta sesearch sediff
|
pylint -E --rcfile .pylintrc setools tests seinfo seinfoflow sedta sesearch sediff
|
||||||
@ -40,5 +42,7 @@ commands = {envpython} setup.py build_ext -i
|
|||||||
[testenv]
|
[testenv]
|
||||||
deps = networkx==1.9
|
deps = networkx==1.9
|
||||||
py27: mock
|
py27: mock
|
||||||
|
py27: enum34
|
||||||
|
py33: enum34
|
||||||
commands = {envpython} setup.py test
|
commands = {envpython} setup.py test
|
||||||
recreate = True
|
recreate = True
|
||||||
|
Loading…
Reference in New Issue
Block a user