diff options
Diffstat (limited to 'libbe/storage/base.py')
-rw-r--r-- | libbe/storage/base.py | 72 |
1 files changed, 48 insertions, 24 deletions
diff --git a/libbe/storage/base.py b/libbe/storage/base.py index 7020c10..09e176b 100644 --- a/libbe/storage/base.py +++ b/libbe/storage/base.py @@ -21,6 +21,7 @@ Abstract bug repository data storage to easily support multiple backends. """ import copy +import functools import os import pickle @@ -39,14 +40,18 @@ if TESTING: from libbe.util.utility import Dir + # https://stackoverflow.com/a/56719588/164233 def cmp(a, b): - return (int(a) > int(b)) - (int(a) < int(b)) + if a == b: + return 0 + return (a > b) - (a < b) class ConnectionError (Exception): pass + class InvalidStorageVersion(ConnectionError): def __init__(self, active_version, expected_version=None): if expected_version is None: @@ -57,12 +62,14 @@ class InvalidStorageVersion(ConnectionError): self.active_version = active_version self.expected_version = expected_version + class InvalidID (KeyError): def __init__(self, id=None, revision=None, msg=None): KeyError.__init__(self, id) self.msg = msg self.id = id self.revision = revision + def __str__(self): if self.msg is None: return '%s in revision %s' % (self.id, self.revision) @@ -94,6 +101,7 @@ class _EMPTY (object): """Entry has been added but has no user-set value.""" pass +@functools.total_ordering class Entry (Tree): def __init__(self, id, value=_EMPTY, parent=None, directory=False, children=None): @@ -104,7 +112,7 @@ class Entry (Tree): self.id = id self.value = value self.parent = parent - if self.parent != None: + if self.parent is not None: if self.parent.directory == False: raise InvalidDirectory( 'Non-directory %s cannot have children' % self.parent) @@ -117,33 +125,49 @@ class Entry (Tree): def __repr__(self): return str(self) - def __cmp__(self, other, local=False): + def __eq__(self, other, local=False): + if other is None: + return False + if (self.id != other.id) or (self.value != other.value): + return False + if not local: + if self.parent is None: + if self.parent != other.parent: + return False + elif not self.parent.__eq__(other.parent, local=True): + return False + for sc, oc in zip(self, other): + if not sc.__eq__(oc, local=True): + return False + return True + + def __lt__(self, other, local=False): if other is None: - return cmp(1, None) - if cmp(self.id, other.id) != 0: - return cmp(self.id, other.id) - if cmp(self.value, other.value) != 0: - return cmp(self.value, other.value) - if local == False: + return False + if self.id != other.id: + return self.id < other.id + if self.value != other.value: + return self.value < other.value + if not local: if self.parent is None: - if cmp(self.parent, other.parent) != 0: - return cmp(self.parent, other.parent) - elif self.parent.__cmp__(other.parent, local=True) != 0: - return self.parent.__cmp__(other.parent, local=True) + if self.parent != other.parent: + return self.parent < other.parent + elif not self.parent.__eq__(other.parent, local=True): + return self.parent.__lt__(other.parent, local=True) for sc,oc in zip(self, other): - if sc.__cmp__(oc, local=True) != 0: - return sc.__cmp__(oc, local=True) - return 0 + if not sc.__eq__(oc, local=True): + return sc.__lt__(oc, local=True) + return False def _objects_to_ids(self): - if self.parent != None: + if self.parent is not None: self.parent = self.parent.id for i,c in enumerate(self): self[i] = c.id return self def _ids_to_objects(self, dict): - if self.parent != None: + if self.parent is not None: self.parent = dict[self.parent] for i,c in enumerate(self): self[i] = dict[c] @@ -276,7 +300,7 @@ class Storage (object): self._remove(*args, **kwargs) def _remove(self, id): - if self._data[id].directory == True \ + if self._data[id].directory \ and len(self.children(id)) > 0: raise DirectoryNotEmpty(id) e = self._data.pop(id) @@ -307,7 +331,7 @@ class Storage (object): while len(stack) > 0: id = stack.pop(0) parent = self._data[id].parent - if parent != None and not parent.id.startswith('__'): + if parent is not None and not parent.id.startswith('__'): ancestor = parent.id ancestors.append(ancestor) stack.append(ancestor) @@ -339,8 +363,8 @@ class Storage (object): else: decode = False value = self._get(*args, **kwargs) - if value != None: - if decode == True and type(value) != str: + if value is not None: + if decode and type(value) != str: return str(value, self.encoding) elif decode == False and type(value) != bytes: return value.encode(self.encoding) @@ -425,7 +449,7 @@ class VersionedStorage (Storage): return id in self._data[revision] def _remove(self, id): - if self._data[-1][id].directory == True \ + if self._data[-1][id].directory \ and len(self.children(id)) > 0: raise DirectoryNotEmpty(id) e = self._data[-1].pop(id) @@ -447,7 +471,7 @@ class VersionedStorage (Storage): while len(stack) > 0: id = stack.pop(0) parent = self._data[revision][id].parent - if parent != None and not parent.id.startswith('__'): + if parent is not None and not parent.id.startswith('__'): ancestor = parent.id ancestors.append(ancestor) stack.append(ancestor) |