diff options
author | Christian Heimes <cheimes@redhat.com> | 2016-02-26 17:18:57 +0100 |
---|---|---|
committer | Endi S. Dewata <edewata@redhat.com> | 2016-04-05 22:45:19 +0200 |
commit | 7a9612411834649cac2089afc6c19942d8062ab7 (patch) | |
tree | 11dc7154e512ec02df016071f44cc5433b8aa367 /base/server/python/pki/server/__init__.py | |
parent | ff98bf4bd2b901d368ec224b4ba280fd624128b9 (diff) | |
download | pki-7a9612411834649cac2089afc6c19942d8062ab7.tar.gz pki-7a9612411834649cac2089afc6c19942d8062ab7.tar.xz pki-7a9612411834649cac2089afc6c19942d8062ab7.zip |
Implement total ordering for PKISubsystem and PKIInstance
In Python 3 subclasses no longer implement automatic ordering. To
provide ordering for sort() and custom comparison, __eq__ and __lt__ are
required.
https://fedorahosted.org/pki/ticket/2216
Diffstat (limited to 'base/server/python/pki/server/__init__.py')
-rw-r--r-- | base/server/python/pki/server/__init__.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/base/server/python/pki/server/__init__.py b/base/server/python/pki/server/__init__.py index 5803f3f52..2c735f0ef 100644 --- a/base/server/python/pki/server/__init__.py +++ b/base/server/python/pki/server/__init__.py @@ -21,6 +21,7 @@ from __future__ import absolute_import from lxml import etree +import functools import getpass import grp import io @@ -59,6 +60,7 @@ class PKIServer(object): return instances +@functools.total_ordering class PKISubsystem(object): def __init__(self, instance, subsystem_name): @@ -87,6 +89,29 @@ class PKISubsystem(object): # custom subsystem location self.doc_base = os.path.join(self.base_dir, 'webapps', self.name) + def __eq__(self, other): + if not isinstance(other, PKISubsystem): + return NotImplemented + return (self.name == other.name and + self.instance == other.instance and + self.type == other.type) + + def __ne__(self, other): + if not isinstance(other, PKISubsystem): + return NotImplemented + return not self.__eq__(other) + + def __lt__(self, other): + if not isinstance(other, PKISubsystem): + return NotImplemented + self_type = self.type if self.type is not None else '' + other_type = other.type if other.type is not None else '' + return (self.name < other.name or + self.instance < other.instance or + self_type < other_type) + + __hash__ = None + def load(self): self.config.clear() @@ -317,6 +342,7 @@ class PKISubsystem(object): return str(self.instance) + '/' + self.name +@functools.total_ordering class PKIInstance(object): def __init__(self, name, instanceType=10): @@ -350,6 +376,25 @@ class PKIInstance(object): self.subsystems = [] + def __eq__(self, other): + if not isinstance(other, PKIInstance): + return NotImplemented + return (self.name == other.name and + self.type == other.type) + + def __ne__(self, other): + if not isinstance(other, PKIInstance): + return NotImplemented + return not self.__eq__(other) + + def __lt__(self, other): + if not isinstance(other, PKIInstance): + return NotImplemented + return (self.name < other.name or + self.type < other.type) + + __hash__ = None + def is_valid(self): return os.path.exists(self.conf_dir) |