[tor-commits] [stem/master] Merging version constructor and parser
atagar at torproject.org
atagar at torproject.org
Fri Oct 28 16:43:31 UTC 2011
commit 63a9a5fa524e352c94bab7beabd5052e4972c6c4
Author: Damian Johnson <atagar at torproject.org>
Date: Thu Oct 27 20:31:43 2011 -0700
Merging version constructor and parser
The version constructor wasn't really useful so merging it with the get_version
function to make its usage more intuitive.
---
stem/process.py | 4 +-
stem/types.py | 66 ++++++++++++++++++++-----------------------------
test/unit/version.py | 36 +++++++++++++-------------
3 files changed, 47 insertions(+), 59 deletions(-)
diff --git a/stem/process.py b/stem/process.py
index 0e22685..4eaf1f7 100644
--- a/stem/process.py
+++ b/stem/process.py
@@ -18,7 +18,7 @@ from stem.util import system
# number of seconds before we time out our attempt to start a tor instance
DEFAULT_INIT_TIMEOUT = 90
-# cache for the get_version function
+# cache for the get_tor_version function
VERSION_CACHE = {}
def get_tor_version(tor_cmd = "tor"):
@@ -52,7 +52,7 @@ def get_tor_version(tor_cmd = "tor"):
if last_line.startswith("Tor version ") and last_line.endswith("."):
try:
version_str = last_line[12:-1]
- VERSION_CACHE[tor_cmd] = stem.types.get_version(version_str)
+ VERSION_CACHE[tor_cmd] = stem.types.Version(version_str)
except ValueError, exc:
raise IOError(exc)
else:
diff --git a/stem/types.py b/stem/types.py
index 10b120b..b289921 100644
--- a/stem/types.py
+++ b/stem/types.py
@@ -12,7 +12,6 @@ ControlMessage - Message from the control socket.
|- __str__ - content stripped of protocol formatting
+- __iter__ - message components stripped of protocol formatting
-get_version - Converts a version string to a Version instance.
Version - Tor versioning information.
|- __str__ - string representation
+- __cmp__ - compares with another Version
@@ -202,37 +201,6 @@ class ControlMessage:
for _, _, content in self._parsed_content:
yield content
-def get_version(version_str):
- """
- Parses a version string, providing back a types.Version instance.
-
- Arguments:
- version_str (str) - representation of a tor version (ex. "0.2.2.23-alpha")
-
- Returns:
- stem.types.Version instance
-
- Raises:
- ValueError if input isn't a valid tor version
- """
-
- if not isinstance(version_str, str):
- raise ValueError("argument is not a string")
-
- m = re.match(r'^([0-9]+).([0-9]+).([0-9]+)(.[0-9]+)?(-\S*)?$', version_str)
-
- if m:
- major, minor, micro, patch, status = m.groups()
-
- # The patch and status matches are optional (may be None) and have an extra
- # proceeding period or dash if they exist. Stripping those off.
-
- if patch: patch = int(patch[1:])
- if status: status = status[1:]
-
- return Version(int(major), int(minor), int(micro), patch, status)
- else: raise ValueError("'%s' isn't a properly formatted tor version" % version_str)
-
class Version:
"""
Comparable tor version, as per the 'new version' of the version-spec...
@@ -247,12 +215,32 @@ class Version:
'alpha', 'beta-dev', etc (None if undefined)
"""
- def __init__(self, major, minor, micro, patch = None, status = None):
- self.major = major
- self.minor = minor
- self.micro = micro
- self.patch = patch
- self.status = status
+ def __init__(self, version_str):
+ """
+ Parses a valid tor version string, for instance "0.1.4" or
+ "0.2.2.23-alpha".
+
+ Raises:
+ ValueError if input isn't a valid tor version
+ """
+
+ m = re.match(r'^([0-9]+).([0-9]+).([0-9]+)(.[0-9]+)?(-\S*)?$', version_str)
+
+ if m:
+ major, minor, micro, patch, status = m.groups()
+
+ # The patch and status matches are optional (may be None) and have an extra
+ # proceeding period or dash if they exist. Stripping those off.
+
+ if patch: patch = int(patch[1:])
+ if status: status = status[1:]
+
+ self.major = int(major)
+ self.minor = int(minor)
+ self.micro = int(micro)
+ self.patch = patch
+ self.status = status
+ else: raise ValueError("'%s' isn't a properly formatted tor version" % version_str)
def __str__(self):
"""
@@ -292,5 +280,5 @@ class Version:
return cmp(my_status, other_status)
# TODO: version requirements will probably be moved to another module later
-REQ_GETINFO_CONFIG_TEXT = get_version("0.2.2.7-alpha")
+REQ_GETINFO_CONFIG_TEXT = Version("0.2.2.7-alpha")
diff --git a/test/unit/version.py b/test/unit/version.py
index 0c4a34a..3200d05 100644
--- a/test/unit/version.py
+++ b/test/unit/version.py
@@ -12,36 +12,36 @@ class TestVerionFunctions(unittest.TestCase):
def test_parsing(self):
"""
- Tests parsing by the 'get_version' function.
+ Tests parsing by the Version class constructor.
"""
# valid versions with various number of compontents to the version
- version = stem.types.get_version("0.1.2.3-tag")
+ version = stem.types.Version("0.1.2.3-tag")
self.assert_versions_match(version, 0, 1, 2, 3, "tag")
- version = stem.types.get_version("0.1.2.3")
+ version = stem.types.Version("0.1.2.3")
self.assert_versions_match(version, 0, 1, 2, 3, None)
- version = stem.types.get_version("0.1.2-tag")
+ version = stem.types.Version("0.1.2-tag")
self.assert_versions_match(version, 0, 1, 2, None, "tag")
- version = stem.types.get_version("0.1.2")
+ version = stem.types.Version("0.1.2")
self.assert_versions_match(version, 0, 1, 2, None, None)
# checks an empty tag
- version = stem.types.get_version("0.1.2.3-")
+ version = stem.types.Version("0.1.2.3-")
self.assert_versions_match(version, 0, 1, 2, 3, "")
- version = stem.types.get_version("0.1.2-")
+ version = stem.types.Version("0.1.2-")
self.assert_versions_match(version, 0, 1, 2, None, "")
# checks invalid version strings
- self.assertRaises(ValueError, stem.types.get_version, "")
- self.assertRaises(ValueError, stem.types.get_version, "1.2.3.4nodash")
- self.assertRaises(ValueError, stem.types.get_version, "1.2.3.a")
- self.assertRaises(ValueError, stem.types.get_version, "1.2.a.4")
- self.assertRaises(ValueError, stem.types.get_version, "12.3")
- self.assertRaises(ValueError, stem.types.get_version, "1.-2.3")
+ self.assertRaises(ValueError, stem.types.Version, "")
+ self.assertRaises(ValueError, stem.types.Version, "1.2.3.4nodash")
+ self.assertRaises(ValueError, stem.types.Version, "1.2.3.a")
+ self.assertRaises(ValueError, stem.types.Version, "1.2.a.4")
+ self.assertRaises(ValueError, stem.types.Version, "12.3")
+ self.assertRaises(ValueError, stem.types.Version, "1.-2.3")
def test_comparison(self):
"""
@@ -97,8 +97,8 @@ class TestVerionFunctions(unittest.TestCase):
second (also checking the inverse).
"""
- version1 = stem.types.get_version(first_version)
- version2 = stem.types.get_version(second_version)
+ version1 = stem.types.Version(first_version)
+ version2 = stem.types.Version(second_version)
self.assertEqual(version1 > version2, True)
self.assertEqual(version1 < version2, False)
@@ -107,8 +107,8 @@ class TestVerionFunctions(unittest.TestCase):
Asserts that the parsed version of the first version equals the second.
"""
- version1 = stem.types.get_version(first_version)
- version2 = stem.types.get_version(second_version)
+ version1 = stem.types.Version(first_version)
+ version2 = stem.types.Version(second_version)
self.assertEqual(version1, version2)
def assert_string_matches(self, version):
@@ -117,5 +117,5 @@ class TestVerionFunctions(unittest.TestCase):
matches the input.
"""
- self.assertEqual(version, str(stem.types.get_version(version)))
+ self.assertEqual(version, str(stem.types.Version(version)))
More information about the tor-commits
mailing list