[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [stem/master] Dropping Version.meets_requirements() in favour of comparisons
commit 479f535614081f097ee2e39ecde70ffaf658ba25
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date: Sun Feb 3 11:29:53 2013 -0800
Dropping Version.meets_requirements() in favour of comparisons
Once upon a time you checked your requirements via simple comparisons...
if my_version >= requirement:
... do stuff...
I reluctantly changed this to a meets_requirements() method when we added the
VersionRequirements class since it was no longer simple comparisons the __cmp__
method could handle. However, now that we're using rich comparison operators we
can go back to the nicer style of comparisons. Apologies for any confusion
this back-and-forth has caused.
---
stem/control.py | 6 +-
stem/version.py | 58 +++++++++++++---------------
test/integ/connection/authentication.py | 4 +-
test/integ/control/controller.py | 2 +-
test/integ/response/protocolinfo.py | 2 +-
test/unit/version.py | 62 +++++++++++++++---------------
6 files changed, 65 insertions(+), 69 deletions(-)
diff --git a/stem/control.py b/stem/control.py
index 8708783..5c7a54e 100644
--- a/stem/control.py
+++ b/stem/control.py
@@ -1435,7 +1435,7 @@ class Controller(BaseController):
for event_type in events:
event_version = stem.response.events.EVENT_TYPE_TO_CLASS[event_type]._VERSION_ADDED
- if not self.get_version().meets_requirements(event_version):
+ if self.get_version() < event_version:
raise stem.InvalidRequest(552, "%s event requires Tor version %s or later" % (event_type, event_version))
for event_type in events:
@@ -1558,7 +1558,7 @@ class Controller(BaseController):
if defaulted_version:
our_version = self.get_version(None)
- if our_version and our_version.meets_requirements(defaulted_version):
+ if our_version and our_version >= defaulted_version:
self._enabled_features.append(feature)
return feature in self._enabled_features
@@ -1728,7 +1728,7 @@ class Controller(BaseController):
if path is None and circuit_id == '0':
path_opt_version = stem.version.Requirement.EXTENDCIRCUIT_PATH_OPTIONAL
- if not self.get_version().meets_requirements(path_opt_version):
+ if not self.get_version() >= path_opt_version:
raise stem.InvalidRequest(512, "EXTENDCIRCUIT requires the path prior to version %s" % path_opt_version)
args = [circuit_id]
diff --git a/stem/version.py b/stem/version.py
index a7c816c..00e0769 100644
--- a/stem/version.py
+++ b/stem/version.py
@@ -8,7 +8,7 @@ easily parsed and compared, for instance...
>>> my_version = get_system_tor_version()
>>> print my_version
0.2.1.30
- >>> my_version.meets_requirements(Requirement.CONTROL_SOCKET)
+ >>> my_version >= Requirement.CONTROL_SOCKET
True
**Module Overview:**
@@ -18,12 +18,6 @@ easily parsed and compared, for instance...
get_system_tor_version - gets the version of our system's tor installation
Version - Tor versioning information
- +- meets_requirements - checks if this version meets the given requirements
-
- VersionRequirements - Series of version requirements
- |- greater_than - adds rule that matches if we're greater than a version
- |- less_than - adds rule that matches if we're less than a version
- +- in_range - adds rule that matches if we're within a given version range
.. data:: Requirement (enum)
@@ -160,24 +154,6 @@ class Version(object):
else:
raise ValueError("'%s' isn't a properly formatted tor version" % version_str)
- def meets_requirements(self, requirements):
- """
- Checks if this version meets the requirements for a given feature. We can
- be compared to either a :class:`~stem.version.Version` or
- :class:`~stem.version.VersionRequirements`.
-
- :param requirements: requirements to be checked for
- """
-
- if isinstance(requirements, Version):
- return self >= requirements
- else:
- for rule in requirements.rules:
- if rule(self):
- return True
-
- return False
-
def __str__(self):
"""
Provides the string used to construct the version.
@@ -219,14 +195,34 @@ class Version(object):
def __eq__(self, other):
return self._compare(other, lambda s, o: s == o)
- def __lt__(self, other):
- return self._compare(other, lambda s, o: s < o)
+ def __gt__(self, other):
+ """
+ Checks if this version meets the requirements for a given feature. We can
+ be compared to either a :class:`~stem.version.Version` or
+ :class:`~stem.version._VersionRequirements`.
+ """
+
+ if isinstance(other, _VersionRequirements):
+ for rule in other.rules:
+ if rule(self):
+ return True
+
+ return False
+
+ return self._compare(other, lambda s, o: s > o)
+
+ def __ge__(self, other):
+ if isinstance(other, _VersionRequirements):
+ for rule in other.rules:
+ if rule(self):
+ return True
+
+ return False
- def __le__(self, other):
- return self._compare(other, lambda s, o: s <= o)
+ return self._compare(other, lambda s, o: s >= o)
-class VersionRequirements(object):
+class _VersionRequirements(object):
"""
Series of version constraints that can be compared to. For instance, this
allows for comparisons like 'if I'm greater than version X in the 0.2.2
@@ -283,7 +279,7 @@ class VersionRequirements(object):
self.rules.append(new_rule)
-safecookie_req = VersionRequirements()
+safecookie_req = _VersionRequirements()
safecookie_req.in_range(Version("0.2.2.36"), Version("0.2.3.0"))
safecookie_req.greater_than(Version("0.2.3.13"))
diff --git a/test/integ/connection/authentication.py b/test/integ/connection/authentication.py
index b8833df..d8c0a8a 100644
--- a/test/integ/connection/authentication.py
+++ b/test/integ/connection/authentication.py
@@ -43,7 +43,7 @@ def _can_authenticate(auth_type):
tor_options = runner.get_options()
password_auth = test.runner.Torrc.PASSWORD in tor_options
cookie_auth = test.runner.Torrc.COOKIE in tor_options
- safecookie_auth = cookie_auth and runner.get_tor_version().meets_requirements(stem.version.Requirement.AUTH_SAFECOOKIE)
+ safecookie_auth = cookie_auth and runner.get_tor_version() >= stem.version.Requirement.AUTH_SAFECOOKIE
if not password_auth and not cookie_auth:
# open socket, anything but safecookie will work
@@ -104,7 +104,7 @@ class TestAuthenticate(unittest.TestCase):
self.cookie_auth_methods = [stem.connection.AuthMethod.COOKIE]
tor_version = test.runner.get_runner().get_tor_version()
- if tor_version.meets_requirements(stem.version.Requirement.AUTH_SAFECOOKIE):
+ if tor_version >= stem.version.Requirement.AUTH_SAFECOOKIE:
self.cookie_auth_methods.append(stem.connection.AuthMethod.SAFECOOKIE)
def test_authenticate_general_socket(self):
diff --git a/test/integ/control/controller.py b/test/integ/control/controller.py
index 15168a4..3cabcd9 100644
--- a/test/integ/control/controller.py
+++ b/test/integ/control/controller.py
@@ -369,7 +369,7 @@ class TestController(unittest.TestCase):
if test.runner.Torrc.COOKIE in tor_options:
auth_methods.append(stem.response.protocolinfo.AuthMethod.COOKIE)
- if tor_version.meets_requirements(stem.version.Requirement.AUTH_SAFECOOKIE):
+ if tor_version >= stem.version.Requirement.AUTH_SAFECOOKIE:
auth_methods.append(stem.response.protocolinfo.AuthMethod.SAFECOOKIE)
if test.runner.Torrc.PASSWORD in tor_options:
diff --git a/test/integ/response/protocolinfo.py b/test/integ/response/protocolinfo.py
index 8756c4e..6f3e79a 100644
--- a/test/integ/response/protocolinfo.py
+++ b/test/integ/response/protocolinfo.py
@@ -138,7 +138,7 @@ class TestProtocolInfo(unittest.TestCase):
if test.runner.Torrc.COOKIE in tor_options:
auth_methods.append(stem.response.protocolinfo.AuthMethod.COOKIE)
- if tor_version.meets_requirements(stem.version.Requirement.AUTH_SAFECOOKIE):
+ if tor_version >= stem.version.Requirement.AUTH_SAFECOOKIE:
auth_methods.append(stem.response.protocolinfo.AuthMethod.SAFECOOKIE)
chroot_path = runner.get_chroot()
diff --git a/test/unit/version.py b/test/unit/version.py
index 068492a..8d15dc0 100644
--- a/test/unit/version.py
+++ b/test/unit/version.py
@@ -142,62 +142,62 @@ class TestVersion(unittest.TestCase):
Checks a VersionRequirements with a single greater_than rule.
"""
- requirements = stem.version.VersionRequirements()
+ requirements = stem.version._VersionRequirements()
requirements.greater_than(Version("0.2.2.36"))
- self.assertTrue(Version("0.2.2.36").meets_requirements(requirements))
- self.assertTrue(Version("0.2.2.37").meets_requirements(requirements))
- self.assertTrue(Version("0.2.3.36").meets_requirements(requirements))
- self.assertFalse(Version("0.2.2.35").meets_requirements(requirements))
- self.assertFalse(Version("0.2.1.38").meets_requirements(requirements))
+ self.assertTrue(Version("0.2.2.36") >= requirements)
+ self.assertTrue(Version("0.2.2.37") >= requirements)
+ self.assertTrue(Version("0.2.3.36") >= requirements)
+ self.assertFalse(Version("0.2.2.35") >= requirements)
+ self.assertFalse(Version("0.2.1.38") >= requirements)
- requirements = stem.version.VersionRequirements()
+ requirements = stem.version._VersionRequirements()
requirements.greater_than(Version("0.2.2.36"), False)
- self.assertFalse(Version("0.2.2.35").meets_requirements(requirements))
- self.assertFalse(Version("0.2.2.36").meets_requirements(requirements))
- self.assertTrue(Version("0.2.2.37").meets_requirements(requirements))
+ self.assertFalse(Version("0.2.2.35") >= requirements)
+ self.assertFalse(Version("0.2.2.36") >= requirements)
+ self.assertTrue(Version("0.2.2.37") >= requirements)
def test_requirements_less_than(self):
"""
Checks a VersionRequirements with a single less_than rule.
"""
- requirements = stem.version.VersionRequirements()
+ requirements = stem.version._VersionRequirements()
requirements.less_than(Version("0.2.2.36"))
- self.assertTrue(Version("0.2.2.36").meets_requirements(requirements))
- self.assertTrue(Version("0.2.2.35").meets_requirements(requirements))
- self.assertTrue(Version("0.2.1.38").meets_requirements(requirements))
- self.assertFalse(Version("0.2.2.37").meets_requirements(requirements))
- self.assertFalse(Version("0.2.3.36").meets_requirements(requirements))
+ self.assertTrue(Version("0.2.2.36") >= requirements)
+ self.assertTrue(Version("0.2.2.35") >= requirements)
+ self.assertTrue(Version("0.2.1.38") >= requirements)
+ self.assertFalse(Version("0.2.2.37") >= requirements)
+ self.assertFalse(Version("0.2.3.36") >= requirements)
- requirements = stem.version.VersionRequirements()
+ requirements = stem.version._VersionRequirements()
requirements.less_than(Version("0.2.2.36"), False)
- self.assertFalse(Version("0.2.2.37").meets_requirements(requirements))
- self.assertFalse(Version("0.2.2.36").meets_requirements(requirements))
- self.assertTrue(Version("0.2.2.35").meets_requirements(requirements))
+ self.assertFalse(Version("0.2.2.37") >= requirements)
+ self.assertFalse(Version("0.2.2.36") >= requirements)
+ self.assertTrue(Version("0.2.2.35") >= requirements)
def test_requirements_in_range(self):
"""
Checks a VersionRequirements with a single in_range rule.
"""
- requirements = stem.version.VersionRequirements()
+ requirements = stem.version._VersionRequirements()
requirements.in_range(Version("0.2.2.36"), Version("0.2.2.38"))
- self.assertFalse(Version("0.2.2.35").meets_requirements(requirements))
- self.assertTrue(Version("0.2.2.36").meets_requirements(requirements))
- self.assertTrue(Version("0.2.2.37").meets_requirements(requirements))
- self.assertFalse(Version("0.2.2.38").meets_requirements(requirements))
+ self.assertFalse(Version("0.2.2.35") >= requirements)
+ self.assertTrue(Version("0.2.2.36") >= requirements)
+ self.assertTrue(Version("0.2.2.37") >= requirements)
+ self.assertFalse(Version("0.2.2.38") >= requirements)
# rule for 'anything in the 0.2.2.x series'
- requirements = stem.version.VersionRequirements()
+ requirements = stem.version._VersionRequirements()
requirements.in_range(Version("0.2.2.0"), Version("0.2.3.0"))
for index in xrange(0, 100):
- self.assertTrue(Version("0.2.2.%i" % index).meets_requirements(requirements))
+ self.assertTrue(Version("0.2.2.%i" % index) >= requirements)
def test_requirements_multiple_rules(self):
"""
@@ -205,15 +205,15 @@ class TestVersion(unittest.TestCase):
"""
# rule to say 'anything but the 0.2.2.x series'
- requirements = stem.version.VersionRequirements()
+ requirements = stem.version._VersionRequirements()
requirements.greater_than(Version("0.2.3.0"))
requirements.less_than(Version("0.2.2.0"), False)
- self.assertTrue(Version("0.2.3.0").meets_requirements(requirements))
- self.assertFalse(Version("0.2.2.0").meets_requirements(requirements))
+ self.assertTrue(Version("0.2.3.0") >= requirements)
+ self.assertFalse(Version("0.2.2.0") >= requirements)
for index in xrange(0, 100):
- self.assertFalse(Version("0.2.2.%i" % index).meets_requirements(requirements))
+ self.assertFalse(Version("0.2.2.%i" % index) >= requirements)
def assert_versions_match(self, version, major, minor, micro, patch, status, extra):
"""
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits