VirtualMailManager/common.py
branchv0.6.x
changeset 265 3c0173418d5d
parent 263 07fdc93dde9f
child 266 e14c345b44a1
--- a/VirtualMailManager/common.py	Sun Apr 25 04:51:14 2010 +0000
+++ b/VirtualMailManager/common.py	Mon Apr 26 02:15:36 2010 +0000
@@ -16,6 +16,8 @@
      NOT_EXECUTABLE, NO_SUCH_BINARY, NO_SUCH_DIRECTORY
 from VirtualMailManager.errors import VMMError
 
+
+_version_re = re.compile(r'^(\d+)\.(\d+)\.(?:(\d+)|(alpha|beta|rc)(\d+))$')
 _ = lambda msg: msg
 
 
@@ -65,24 +67,24 @@
 
 def version_hex(version_string):
     """Convert a Dovecot version, e.g.: '1.2.3' or '2.0.beta4', to an int.
-    Returns 0 if the *version_string* has the wrong™ format.
+    Raises a `ValueError` if the *version_string* has the wrong™ format.
 
     version_hex('1.2.3') -> 16909296
     hex(version_hex('1.2.3')) -> '0x10203f0'
     """
     version = 0
-    version_re = r'^(\d+)\.(\d+)\.(?:(\d+)|(alpha|beta|rc)(\d+))$'
     version_level = dict(alpha=0xA, beta=0xB, rc=0xC)
-    version_mo = re.match(version_re, version_string)
-    if version_mo:
-        major, minor, patch, level, serial = version_mo.groups()
-        version += int(major) << 24
-        version += int(minor) << 16
-        if patch:
-            version += int(patch) << 8
-        version += version_level.get(level, 0xF) << 4
-        if serial:
-            version += int(serial)
+    version_mo = _version_re.match(version_string)
+    if not version_mo:
+        raise ValueError('Invalid version string: %r' % version_string)
+    major, minor, patch, level, serial = version_mo.groups()
+    version += int(major) << 24
+    version += int(minor) << 16
+    if patch:
+        version += int(patch) << 8
+    version += version_level.get(level, 0xF) << 4
+    if serial:
+        version += int(serial)
     return version
 
 del _