From: Bob Halley Date: Mon, 30 Mar 2009 23:32:22 +0000 (+0000) Subject: make EDNS options comparable X-Git-Tag: v1.7.0~28 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=68480dc55511008abaa9d9c5c9e6df5b39643781;p=thirdparty%2Fdnspython.git make EDNS options comparable --- diff --git a/dns/edns.py b/dns/edns.py index 093e36a1..1731cedd 100644 --- a/dns/edns.py +++ b/dns/edns.py @@ -49,6 +49,51 @@ class Option(object): from_wire = classmethod(from_wire) + def _cmp(self, other): + """Compare an ENDS option with another option of the same type. + Return < 0 if self < other, 0 if self == other, and > 0 if self > other. + """ + raise NotImplementedError + + def __eq__(self, other): + if not isinstance(other, Option): + return False + if self.otype != other.otype: + return False + return self._cmp(other) == 0 + + def __ne__(self, other): + if not isinstance(other, Option): + return False + if self.otype != other.otype: + return False + return self._cmp(other) != 0 + + def __lt__(self, other): + if not isinstance(other, Option) or \ + self.otype != other.otype: + return NotImplemented + return self._cmp(other) < 0 + + def __le__(self, other): + if not isinstance(other, Option) or \ + self.otype != other.otype: + return NotImplemented + return self._cmp(other) <= 0 + + def __ge__(self, other): + if not isinstance(other, Option) or \ + self.otype != other.otype: + return NotImplemented + return self._cmp(other) >= 0 + + def __gt__(self, other): + if not isinstance(other, Option) or \ + self.otype != other.otype: + return NotImplemented + return self._cmp(other) > 0 + + class GenericOption(Option): """Generate Rdata Class @@ -68,6 +113,8 @@ class GenericOption(Option): from_wire = classmethod(from_wire) + def _cmp(self, other): + return cmp(self.data, other.data) _type_to_class = { }