diff options
Diffstat (limited to 'libbe/storage/base.py')
-rw-r--r-- | libbe/storage/base.py | 46 |
1 files changed, 25 insertions, 21 deletions
diff --git a/libbe/storage/base.py b/libbe/storage/base.py index 4c05287..7020c10 100644 --- a/libbe/storage/base.py +++ b/libbe/storage/base.py @@ -23,7 +23,6 @@ Abstract bug repository data storage to easily support multiple backends. import copy import os import pickle -import types from libbe.error import NotSupported import libbe.storage @@ -32,7 +31,7 @@ from libbe.util import InvalidObject import libbe.version from libbe import TESTING -if TESTING == True: +if TESTING: import doctest import os.path import sys @@ -40,12 +39,17 @@ if TESTING == True: 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)) + + class ConnectionError (Exception): pass class InvalidStorageVersion(ConnectionError): def __init__(self, active_version, expected_version=None): - if expected_version == None: + if expected_version is None: expected_version = libbe.storage.STORAGE_VERSION msg = 'Storage in "%s" not the expected "%s"' \ % (active_version, expected_version) @@ -60,7 +64,7 @@ class InvalidID (KeyError): self.id = id self.revision = revision def __str__(self): - if self.msg == None: + if self.msg is None: return '%s in revision %s' % (self.id, self.revision) return self.msg @@ -93,7 +97,7 @@ class _EMPTY (object): class Entry (Tree): def __init__(self, id, value=_EMPTY, parent=None, directory=False, children=None): - if children == None: + if children is None: Tree.__init__(self) else: Tree.__init__(self, children) @@ -114,14 +118,14 @@ class Entry (Tree): return str(self) def __cmp__(self, other, local=False): - if other == None: + 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: - if self.parent == None: + 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: @@ -250,7 +254,7 @@ class Storage (object): self._add(id, *args, **kwargs) def _add(self, id, parent=None, directory=False): - if parent == None: + if parent is None: parent = '__ROOT__' p = self._data[parent] self._data[id] = Entry(id, parent=p, directory=directory) @@ -296,7 +300,7 @@ class Storage (object): return self._ancestors(*args, **kwargs) def _ancestors(self, id=None, revision=None): - if id == None: + if id is None: return [] ancestors = [] stack = [id] @@ -316,7 +320,7 @@ class Storage (object): return self._children(*args, **kwargs) def _children(self, id=None, revision=None): - if id == None: + if id is None: id = '__ROOT__' return [c.id for c in self._data[id] if not c.id.startswith('__')] @@ -362,7 +366,7 @@ class Storage (object): def _set(self, id, value): if id not in self._data: raise InvalidID(id) - if self._data[id].directory == True: + if self._data[id].directory: raise InvalidDirectory( 'Directory %s cannot have data' % self.parent) self._data[id].value = value @@ -408,13 +412,13 @@ class VersionedStorage (Storage): self._data = None def _add(self, id, parent=None, directory=False): - if parent == None: + if parent is None: parent = '__ROOT__' p = self._data[-1][parent] self._data[-1][id] = Entry(id, parent=p, directory=directory) def _exists(self, id, revision=None): - if revision == None: + if revision is None: revision = -1 else: revision = int(revision) @@ -432,9 +436,9 @@ class VersionedStorage (Storage): self._remove(entry.id) def _ancestors(self, id=None, revision=None): - if id == None: + if id is None: return [] - if revision == None: + if revision is None: revision = -1 else: revision = int(revision) @@ -450,9 +454,9 @@ class VersionedStorage (Storage): return ancestors def _children(self, id=None, revision=None): - if id == None: + if id is None: id = '__ROOT__' - if revision == None: + if revision is None: revision = -1 else: revision = int(revision) @@ -460,7 +464,7 @@ class VersionedStorage (Storage): if not c.id.startswith('__')] def _get(self, id, default=InvalidObject, revision=None): - if revision == None: + if revision is None: revision = -1 else: revision = int(revision) @@ -508,7 +512,7 @@ class VersionedStorage (Storage): If the specified revision does not exist, raise InvalidRevision. """ - if index == None: + if index is None: return None try: if int(index) != index: @@ -518,7 +522,7 @@ class VersionedStorage (Storage): L = len(self._data) - 1 # -1 b/c of initial commit if index >= -L and index <= L: return str(index % L) - raise InvalidRevision(i) + raise InvalidRevision(index) def changed(self, revision): """Return a tuple of lists of ids `(new, modified, removed)` from the @@ -540,7 +544,7 @@ class VersionedStorage (Storage): return (new, modified, removed) -if TESTING == True: +if TESTING: class StorageTestCase (unittest.TestCase): """Test cases for Storage class.""" |