commit 1c2f851dd20ae16a5b00f340f8f369566b58e860 Author: Damian Johnson atagar@torproject.org Date: Tue Nov 27 18:42:19 2018 -0800
Move comparison and hashing to base Descriptor class
Huh. Not sure why I added these to subclasses rather than their common parent. Maybe there's a reason that will make me regret this, but certainly seems to work. --- stem/descriptor/__init__.py | 25 ++++++++++ stem/descriptor/microdescriptor.py | 21 --------- stem/descriptor/networkstatus.py | 84 ---------------------------------- stem/descriptor/router_status_entry.py | 84 ---------------------------------- stem/descriptor/server_descriptor.py | 42 ----------------- 5 files changed, 25 insertions(+), 231 deletions(-)
diff --git a/stem/descriptor/__init__.py b/stem/descriptor/__init__.py index 1b38783c..5f153f81 100644 --- a/stem/descriptor/__init__.py +++ b/stem/descriptor/__init__.py @@ -699,6 +699,7 @@ class Descriptor(object): self._raw_contents = contents self._lazy_loading = lazy_load self._entries = {} + self._hash = None self._unrecognized_lines = []
@classmethod @@ -1040,6 +1041,30 @@ class Descriptor(object): else: return self._raw_contents
+ def _compare(self, other, method): + if type(self) != type(other): + return False + + return method(str(self).strip(), str(other).strip()) + + def __hash__(self): + if self._hash is None: + self._hash = hash(str(self).strip()) + + return self._hash + + def __eq__(self, other): + return self._compare(other, lambda s, o: s == o) + + def __ne__(self, other): + return not self == other + + def __lt__(self, other): + return self._compare(other, lambda s, o: s < o) + + def __le__(self, other): + return self._compare(other, lambda s, o: s <= o) +
class NewlineNormalizer(object): """ diff --git a/stem/descriptor/microdescriptor.py b/stem/descriptor/microdescriptor.py index 8672f039..78b2b532 100644 --- a/stem/descriptor/microdescriptor.py +++ b/stem/descriptor/microdescriptor.py @@ -364,24 +364,3 @@ class Microdescriptor(Descriptor):
def _name(self, is_plural = False): return 'microdescriptors' if is_plural else 'microdescriptor' - - def _compare(self, other, method): - if not isinstance(other, Microdescriptor): - return False - - return method(str(self).strip(), str(other).strip()) - - def __hash__(self): - return hash(str(self).strip()) - - def __eq__(self, other): - return self._compare(other, lambda s, o: s == o) - - def __ne__(self, other): - return not self == other - - def __lt__(self, other): - return self._compare(other, lambda s, o: s < o) - - def __le__(self, other): - return self._compare(other, lambda s, o: s <= o) diff --git a/stem/descriptor/networkstatus.py b/stem/descriptor/networkstatus.py index baba556c..8cf07f03 100644 --- a/stem/descriptor/networkstatus.py +++ b/stem/descriptor/networkstatus.py @@ -1294,12 +1294,6 @@ class NetworkStatusDocumentV3(NetworkStatusDocument): else: return False # malformed document
- def _compare(self, other, method): - if not isinstance(other, NetworkStatusDocumentV3): - return False - - return method(str(self).strip(), str(other).strip()) - def _header(self, document_file, validate): content = bytes.join(b'', _read_until_keywords((AUTH_START, ROUTERS_START, FOOTER_START), document_file)) entries = _descriptor_components(content, validate) @@ -1384,21 +1378,6 @@ class NetworkStatusDocumentV3(NetworkStatusDocument): if value < minimum or value > maximum: raise ValueError("'%s' value on the params line must be in the range of %i - %i, was %i" % (key, minimum, maximum, value))
- def __hash__(self): - return hash(str(self).strip()) - - def __eq__(self, other): - return self._compare(other, lambda s, o: s == o) - - def __ne__(self, other): - return not self == other - - def __lt__(self, other): - return self._compare(other, lambda s, o: s < o) - - def __le__(self, other): - return self._compare(other, lambda s, o: s <= o) -
def _check_for_missing_and_disallowed_fields(document, entries, fields): """ @@ -1691,27 +1670,6 @@ class DirectoryAuthority(Descriptor):
self.fingerprint = self.v3ident
- def _compare(self, other, method): - if not isinstance(other, DirectoryAuthority): - return False - - return method(str(self).strip(), str(other).strip()) - - def __hash__(self): - return hash(str(self).strip()) - - def __eq__(self, other): - return self._compare(other, lambda s, o: s == o) - - def __ne__(self, other): - return not self == other - - def __lt__(self, other): - return self._compare(other, lambda s, o: s < o) - - def __le__(self, other): - return self._compare(other, lambda s, o: s <= o) -
def _parse_dir_address_line(descriptor, entries): # "dir-address" IPPort @@ -1828,27 +1786,6 @@ class KeyCertificate(Descriptor): else: self._entries = entries
- def _compare(self, other, method): - if not isinstance(other, KeyCertificate): - return False - - return method(str(self).strip(), str(other).strip()) - - def __hash__(self): - return hash(str(self).strip()) - - def __eq__(self, other): - return self._compare(other, lambda s, o: s == o) - - def __ne__(self, other): - return not self == other - - def __lt__(self, other): - return self._compare(other, lambda s, o: s < o) - - def __le__(self, other): - return self._compare(other, lambda s, o: s <= o) -
class DocumentSignature(object): """ @@ -2007,27 +1944,6 @@ class DetachedSignature(Descriptor): else: self._entries = entries
- def _compare(self, other, method): - if not isinstance(other, DetachedSignature): - return False - - return method(str(self).strip(), str(other).strip()) - - def __hash__(self): - return hash(str(self).strip()) - - def __eq__(self, other): - return self._compare(other, lambda s, o: s == o) - - def __ne__(self, other): - return not self == other - - def __lt__(self, other): - return self._compare(other, lambda s, o: s < o) - - def __le__(self, other): - return self._compare(other, lambda s, o: s <= o) -
class BridgeNetworkStatusDocument(NetworkStatusDocument): """ diff --git a/stem/descriptor/router_status_entry.py b/stem/descriptor/router_status_entry.py index b3334620..45385135 100644 --- a/stem/descriptor/router_status_entry.py +++ b/stem/descriptor/router_status_entry.py @@ -503,27 +503,6 @@ class RouterStatusEntry(Descriptor):
return ()
- def _compare(self, other, method): - if not isinstance(other, RouterStatusEntry): - return False - - return method(str(self).strip(), str(other).strip()) - - def __hash__(self): - return hash(str(self).strip()) - - def __eq__(self, other): - return self._compare(other, lambda s, o: s == o) - - def __ne__(self, other): - return not self == other - - def __lt__(self, other): - return self._compare(other, lambda s, o: s < o) - - def __le__(self, other): - return self._compare(other, lambda s, o: s <= o) -
class RouterStatusEntryV2(RouterStatusEntry): """ @@ -558,27 +537,6 @@ class RouterStatusEntryV2(RouterStatusEntry): def _single_fields(self): return ('r', 's', 'v')
- def _compare(self, other, method): - if not isinstance(other, RouterStatusEntryV2): - return False - - return method(str(self).strip(), str(other).strip()) - - def __hash__(self): - return hash(str(self).strip()) - - def __eq__(self, other): - return self._compare(other, lambda s, o: s == o) - - def __ne__(self, other): - return not self == other - - def __lt__(self, other): - return self._compare(other, lambda s, o: s < o) - - def __le__(self, other): - return self._compare(other, lambda s, o: s <= o) -
class RouterStatusEntryV3(RouterStatusEntry): """ @@ -661,27 +619,6 @@ class RouterStatusEntryV3(RouterStatusEntry): def _single_fields(self): return ('r', 's', 'v', 'w', 'p', 'pr')
- def _compare(self, other, method): - if not isinstance(other, RouterStatusEntryV3): - return False - - return method(str(self).strip(), str(other).strip()) - - def __hash__(self): - return hash(str(self).strip()) - - def __eq__(self, other): - return self._compare(other, lambda s, o: s == o) - - def __ne__(self, other): - return not self == other - - def __lt__(self, other): - return self._compare(other, lambda s, o: s < o) - - def __le__(self, other): - return self._compare(other, lambda s, o: s <= o) -
class RouterStatusEntryMicroV3(RouterStatusEntry): """ @@ -753,24 +690,3 @@ class RouterStatusEntryMicroV3(RouterStatusEntry):
def _single_fields(self): return ('r', 's', 'v', 'w', 'm', 'pr') - - def _compare(self, other, method): - if not isinstance(other, RouterStatusEntryMicroV3): - return False - - return method(str(self).strip(), str(other).strip()) - - def __hash__(self): - return hash(str(self).strip()) - - def __eq__(self, other): - return self._compare(other, lambda s, o: s == o) - - def __ne__(self, other): - return not self == other - - def __lt__(self, other): - return self._compare(other, lambda s, o: s < o) - - def __le__(self, other): - return self._compare(other, lambda s, o: s <= o) diff --git a/stem/descriptor/server_descriptor.py b/stem/descriptor/server_descriptor.py index 568af108..89b80d0a 100644 --- a/stem/descriptor/server_descriptor.py +++ b/stem/descriptor/server_descriptor.py @@ -985,12 +985,6 @@ class RelayDescriptor(ServerDescriptor): data = signing_key_digest + base64.b64decode(stem.util.str_tools._to_bytes(self.ed25519_master_key) + b'=') return stem.util.str_tools._to_unicode(binascii.hexlify(data).upper())
- def _compare(self, other, method): - if not isinstance(other, RelayDescriptor): - return False - - return method(str(self).strip(), str(other).strip()) - def _check_constraints(self, entries): super(RelayDescriptor, self)._check_constraints(entries)
@@ -1000,21 +994,6 @@ class RelayDescriptor(ServerDescriptor): elif not self.ed25519_signature: raise ValueError("Descriptor must have a 'router-sig-ed25519' when identity-ed25519 is present")
- def __hash__(self): - return hash(str(self).strip()) - - def __eq__(self, other): - return self._compare(other, lambda s, o: s == o) - - def __ne__(self, other): - return not self == other - - def __lt__(self, other): - return self._compare(other, lambda s, o: s < o) - - def __le__(self, other): - return self._compare(other, lambda s, o: s <= o) -
class BridgeDescriptor(ServerDescriptor): """ @@ -1131,24 +1110,3 @@ class BridgeDescriptor(ServerDescriptor):
def _last_keyword(self): return None - - def _compare(self, other, method): - if not isinstance(other, BridgeDescriptor): - return False - - return method(str(self).strip(), str(other).strip()) - - def __hash__(self): - return hash(str(self).strip()) - - def __eq__(self, other): - return self._compare(other, lambda s, o: s == o) - - def __ne__(self, other): - return not self == other - - def __lt__(self, other): - return self._compare(other, lambda s, o: s < o) - - def __le__(self, other): - return self._compare(other, lambda s, o: s <= o)