aboutsummaryrefslogtreecommitdiffstats
path: root/libbe/storage
diff options
context:
space:
mode:
Diffstat (limited to 'libbe/storage')
-rw-r--r--libbe/storage/base.py72
-rw-r--r--libbe/storage/util/properties.py15
-rw-r--r--libbe/storage/util/settings_object.py6
-rw-r--r--libbe/storage/vcs/base.py5
-rw-r--r--libbe/storage/vcs/darcs.py10
-rw-r--r--libbe/storage/vcs/monotone.py15
6 files changed, 63 insertions, 60 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)
diff --git a/libbe/storage/util/properties.py b/libbe/storage/util/properties.py
index ad9d06c..6a2b964 100644
--- a/libbe/storage/util/properties.py
+++ b/libbe/storage/util/properties.py
@@ -46,11 +46,6 @@ if libbe.TESTING:
import unittest
-# https://stackoverflow.com/a/56719588/164233
-def cmp(a, b):
- return (int(a) > int(b)) - (int(a) < int(b))
-
-
class ValueCheckError (ValueError):
def __init__(self, name, value, allowed):
action = "in" # some list of allowed values
@@ -72,8 +67,8 @@ def Property(funcs):
args["fdel"] = funcs.get("fdel", None)
args["doc"] = funcs.get("doc", None)
- #print "Creating a property with"
- #for key, val in args.items(): print key, value
+ #print("Creating a property with")
+ #for key, val in args.items(): print(key, value)
return property(**args)
def doc_property(doc=None):
@@ -179,12 +174,12 @@ def _get_cached_mutable_property(self, cacher_name, property_name, default=None)
if (cacher_name, property_name) not in self._mutable_property_cache_copy:
return default
return self._mutable_property_cache_copy[(cacher_name, property_name)]
-def _cmp_cached_mutable_property(self, cacher_name, property_name, value, default=None):
+def _eq_cached_mutable_property(self, cacher_name, property_name, value, default=None):
_init_mutable_property_cache(self)
if (cacher_name, property_name) not in self._mutable_property_cache_hash:
_set_cached_mutable_property(self, cacher_name, property_name, default)
old_hash = self._mutable_property_cache_hash[(cacher_name, property_name)]
- return cmp(_hash_mutable_value(value), old_hash)
+ return _hash_mutable_value(value) == old_hash
def defaulting_property(default=None, null=None,
@@ -389,7 +384,7 @@ def change_hook_property(hook, mutable=False, default=None):
value = new_value # compare new value with cached
else:
value = fget(self) # compare current value with cached
- if _cmp_cached_mutable_property(self, "change hook property", name, value, default) != 0:
+ if _eq_cached_mutable_property(self, "change hook property", name, value, default) != 0:
# there has been a change, cache new value
old_value = _get_cached_mutable_property(self, "change hook property", name, default)
_set_cached_mutable_property(self, "change hook property", name, value)
diff --git a/libbe/storage/util/settings_object.py b/libbe/storage/util/settings_object.py
index 323d6e9..d872f49 100644
--- a/libbe/storage/util/settings_object.py
+++ b/libbe/storage/util/settings_object.py
@@ -79,7 +79,7 @@ def setting_name_to_attr_name(self, name):
Examples
--------
- >>> print setting_name_to_attr_name(None,"User-id")
+ >>> print(setting_name_to_attr_name(None,"User-id"))
user_id
See Also
@@ -94,7 +94,7 @@ def attr_name_to_setting_name(self, name):
Examples:
- >>> print attr_name_to_setting_name(None, "user_id")
+ >>> print(attr_name_to_setting_name(None, "user_id"))
User-id
See Also
@@ -294,7 +294,7 @@ if libbe.TESTING == True:
return self.readable
def is_writeable(self):
return self.writeable
-
+
class TestObject (SavedSettingsObject):
def load_settings(self):
self.load_count += 1
diff --git a/libbe/storage/vcs/base.py b/libbe/storage/vcs/base.py
index 579935e..0521e90 100644
--- a/libbe/storage/vcs/base.py
+++ b/libbe/storage/vcs/base.py
@@ -51,11 +51,6 @@ if libbe.TESTING:
import libbe.ui.util.user
-# https://stackoverflow.com/a/56719588/164233
-def cmp(a, b):
- return (int(a) > int(b)) - (int(a) < int(b))
-
-
VCS_ORDER = ['bzr', 'darcs', 'git', 'hg', 'monotone']
"""List VCS modules in order of preference.
diff --git a/libbe/storage/vcs/darcs.py b/libbe/storage/vcs/darcs.py
index 5ec9a26..12eabf4 100644
--- a/libbe/storage/vcs/darcs.py
+++ b/libbe/storage/vcs/darcs.py
@@ -44,12 +44,6 @@ if libbe.TESTING:
import unittest
-# https://stackoverflow.com/a/56719588/164233
-def cmp(a, b):
- return (int(a) > int(b)) - (int(a) < int(b))
-
-
-
def new():
return Darcs()
@@ -286,9 +280,9 @@ class Darcs(base.VCS):
xml_str = output.encode('unicode_escape').replace(r'\n', '\n')
element = ElementTree.XML(xml_str)
assert element.tag == 'changelog', element.tag
- for patch in element.getchildren():
+ for patch in element:
assert patch.tag == 'patch', patch.tag
- for child in patch.getchildren():
+ for child in patch:
if child.tag == 'name':
text = unescape(str(child.text).decode('unicode_escape').strip())
revisions.append(text)
diff --git a/libbe/storage/vcs/monotone.py b/libbe/storage/vcs/monotone.py
index add7be9..1d74c5b 100644
--- a/libbe/storage/vcs/monotone.py
+++ b/libbe/storage/vcs/monotone.py
@@ -33,16 +33,11 @@ import libbe.ui.util.user
from ...util.subproc import CommandError
from . import base
-if libbe.TESTING == True:
+if libbe.TESTING:
import doctest
import sys
-# https://stackoverflow.com/a/56719588/164233
-def cmp(a, b):
- return (int(a) > int(b)) - (int(a) < int(b))
-
-
def new():
return Monotone()
@@ -127,7 +122,7 @@ class Monotone (base.VCS):
private = False
for line in output.splitlines():
line = line.strip()
- if private == True: # HACK. Just pick the first key.
+ if private: # HACK. Just pick the first key.
return line.split(' ', 1)[1]
if line == '[private keys]':
private = True
@@ -149,7 +144,7 @@ class Monotone (base.VCS):
'automate', 'get_workspace_root', cwd=dirname)
else:
mtn_dir = self._u_search_parent_directories(path, '_MTN')
- if mtn_dir == None:
+ if mtn_dir is None:
return None
return os.path.dirname(mtn_dir)
return output.strip()
@@ -206,7 +201,7 @@ class Monotone (base.VCS):
pass
def _vcs_get_file_contents(self, path, revision=None):
- if revision == None:
+ if revision is None:
return base.VCS._vcs_get_file_contents(self, path, revision)
else:
self._require_version_ge(4, 0)
@@ -373,7 +368,7 @@ class Monotone (base.VCS):
return self._parse_diff(self._diff(revision))
-if libbe.TESTING == True:
+if libbe.TESTING:
base.make_vcs_testcase_subclasses(Monotone, sys.modules[__name__])
unitsuite =unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])